1 / 49

Struts2

Struts2. 2 장 기본 예제를 통해 스트럿츠 2 와 친해지기. 부록 A1. 개발 환경 구축. J2SE 설치 J2SE 5.0 이상 Tomcat 설치 Tomcat 5.5.23 버전 http://tomcat.apache.org/download-55.cgi#5.5.23 톰캣설치폴더 confserver.xml 파일에서 JSP 파일에서 한글을 사용할 수 있게 설정. 추가. Eclipse Tomcat Plugin 설치.

mio
Télécharger la présentation

Struts2

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Struts2 2장 기본 예제를 통해 스트럿츠2와 친해지기

  2. 부록 A1. 개발 환경 구축 • J2SE 설치 • J2SE 5.0 이상 • Tomcat 설치 • Tomcat 5.5.23 버전 • http://tomcat.apache.org/download-55.cgi#5.5.23 • 톰캣설치폴더\conf\server.xml 파일에서 • JSP 파일에서 한글을 사용할 수 있게 설정 추가

  3. Eclipse TomcatPlugin 설치 • \EclipsePlugin\tomcatPluginV31.zip 압축해제 • com.sysdeo.eclipse.tomcat_3.1.0 폴더 통째로 • 이클립스폴더\plugins에 복사

  4. Eclipse TomcatPlugin 설정 • Window – Preferences – Tomcat 항목 선택 • Tomcat version : Version 5.x • Tomcat home : 톰캣폴더 • Context declaration mode : Context files • Window-Preferences-Tomcat-Advanced 항목 선택 • Tomcat base : 톰캣폴더

  5. Eclipse PropertyEditorPlugin 설치 • \EclipsePlugin\jp.gr.java_conf.ussiy.app.propedit_4.8.1_for_eclipse3.0.zip 압축해제 • features, plugins폴더 통째로 이클립스 폴더에 복사 • 메시지 번들을 좀 더 편리하게 편집 가능 • 한글로 작성한 메시지 번들이 자동으로 유니코드로 번역되는 기능을 사용할 수 있음

  6. Eclipse Perspective 버튼 추가 • Debug Perspective 추가 • Resource Perspective 추가 • 디버그 작업 , 자원을 찾아볼 때 유용

  7. Struts2 설치 • struts2.0.9 버전 • Struts2\struts-2.0.9-all.zip (부록CD) • http://struts.apache.org/download.cgi#struts209

  8. Project 생성 • New Project – Java – Tomcat Project 선택

  9. Build Path 설정 • \struts2.0.9\lib • antlr-2.7.2.jar • commons-beanutils-1.6.jar • common-chain-1.1.jar • commons-logging-1.0.4.jar • commons-logging-api-1.1.jar • commons-validator-1.3.0.jar • freemarkder-2.3.8.jar • ognl-2.6.11.jar • oro-2.0.8 • struts2-core-2.0.9.jar • struts-core-1.3.5.jar • xwork-2.0.4.jar \eclipse\workspace\struts2\webapp\WEB-INF\lib 에 복사 이클립스 프로젝트 속성창에서 라이브러리 추가

  10. web.xml 생성 <?xml version="1.0" encoding="UTF-8" ?> <web-app id="WebApp_9" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> <display-name>struts sample</display-name> <filter> <filter-name>struts</filter-name> <filter-class> org.apache.struts2.dispatcher.FilterDispatcher </filter-class> </filter> <filter-mapping> <filter-name>struts</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <welcome-file-list> <welcome-file>index.jsp</welcome-file> <welcome-file>default.jsp</welcome-file> <welcome-file>index.html</welcome-file> </welcome-file-list> </web-app> • 웹 어플리케이션의 배치 디스크립터 • struts2\webapp\WEB-INF\web.xml • 생성

  11. struts.xml 생성 <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"> <struts> <package name="default" extends="struts-default" namespace=""> </package> </struts> • Struts 환경설정 파일 • struts2/webapp/WEB-INF/src/struts.xml

  12. struts.properties생성 • 스트럿츠2 프레임워크가 사용하는 환경 설정 값들을 정의 • struts2/WEB-INF\src\struts.properties struts.i18n.reload=true struts.devMode=false struts.configuration.xml.reload=true struts.continuations.package=org.apache.struts2.showcase struts.custom.i18n.resources=globalMessages #struts.action.extension=jspa struts.url.http.port=8080 #struts.freemarker.manager.classname=customFreemarkerManager struts.serve.static=true struts.serve.static.browserCache=false struts.multipart.maxSize=2097252

  13. Encoding 설정 • 프로젝트 Properties – Resource – Text file encoding • UTF-8 선택 • 한글 처리를 위한 문자셋 세팅 끝~

  14. 예제1. 문자열 출력 • 액션 클래스(PrintStringAction.java) • String 타입의 프로퍼티greetings를 정의 • execute() 에서 greetings 프로퍼티에“반갑다! 스트럿츠2.” 문자열 대입 • 리절트 페이지(printString.jsp) • <s:property /> 태그를 사용하여 greetings값을 화면에 출력

  15. 예제1. 문자열 출력 액션 클래스 [PrintStringAction.java] private String greetings; 스트럿츠2 밸류스택[PrintStringAction객체] String greetings; 리절트 페이지[printString.jsp] <s:property value=“greetings” />

  16. 예제1. 문자열 출력 • 액션 클래스이름 • /webapp/WEB-INF/src/example/chapter2/PrintStringAction.java • 리절트 페이지 이름 • /webapp/chapter2/printString.jsp • 관련 태그 • <s:property /> • 작업 순서 • 액션 클래스 작성 • 리절트 페이지 작성 • struts.xml에 액션과 리절트 정의 • 결과 테스트

  17. 예제1. 문자열 출력 • 액션 클래스 작성 /webapp/WEB-INF/src/example/chapter2/PrintStringAction.java

  18. 예제1. 문자열 출력 • 리절트 페이지 작성 /webapp/chapter2/printString.jsp

  19. 예제1. 문자열 출력 • struts.xml에 액션과 리절트 정의 /webapp/WEB-INF/src/struts.xml

  20. 예제1. 문자열 출력 • 결과 테스트 클라이언트 PrintStringAction클래스의 execute() 메소드 호출 프레임워크 execute()에서 프로퍼티 greetings에 “반갑다! 스트럿츠2.” 문자열 값 대입 후 “success” 문자열 값 반환 액션 밸류스택에 저장되어 있는 프로퍼티greetings의 값을 <s:property value=“greetings” /> 태그를 사용하여 출력 리절트 결과

  21. 예제1. 문자열 출력 • OGNL(Object Graph Navigation Language) • <s:property value=“greetings” /> OGNL 표현식으로 해석 getGreetings() 호출

  22. 예제1. 문자열 출력

  23. 예제2. 문자열 목록 출력 • 액션 클래스(PrintStringListAction.java) • List타입의 프로퍼티listString을 정의 • execute()에서 listString에 “MP3 플레이어”, “노트북 PC”, “PDA”, “휴대폰” 값을 추가 • 리절트 페이지(printStringList.jsp) • <s:iterator /> 태그와 <s:property /> 태그를 통해 listString의 문자열 목록을 출력

  24. 예제2. 문자열 목록 출력 • 액션 클래스이름 • /webapp/WEB-INF/src/example/chapter2/PrintStringListAction.java • 리절트 페이지 이름 • /webapp/chapter2/printStringList.jsp • 관련 태그 • <s:property />, <s:iterator /> • 작업 순서 • 액션 클래스 작성 • 리절트 페이지 작성 • struts.xml에 액션과 리절트 정의 • 결과 테스트

  25. 예제2. 문자열 목록 출력 • 액션 클래스 작성 /webapp/WEB-INF/src/example/chapter2/PrintStringListAction.java

  26. 예제2. 문자열 목록 출력 • 리절트 페이지 작성 /webapp/chapter2/printStringList.jsp

  27. 예제2. 문자열 목록 출력 • struts.xml에 액션과 리절트 정의 /webapp/WEB-INF/src/struts.xml

  28. 예제2. 문자열 목록 출력 • 결과 테스트 클라이언트 PrintStringListAction클래스의 execute() 메소드 호출 프레임워크 execute()에서 프로퍼티 listString에 “MP3 플레이어”, “노트북 PC”, “PDA”, “휴대폰” 4개의 문자열 추가한 후 “success” 문자열 값 반환 액션 밸류스택에 저장되어 있는 List 타입의 프로퍼티listString의 아이템을 <s:iterator />태그를 이용하여 아이템의 수만큼 반복하며 <s:property /> 태그를 사용하여 아이템 값 출력 리절트 결과

  29. 예제3. 객체 출력 • 모델 클래스(Product.java) • String 타입의 프로퍼티(name, modelNo) 2개를 정의 • name, modelNo에 대한 각각의 세터,게터메소드를 정의 • 액션 클래스(PrintObjectAction.java) • Product 객체 타입을 액션의 프로퍼티로 정의 • execute()에서 프로퍼티 product의 name에 “MP3 플레이어” 값을 대입하고 modelNo에 “MP3-070701”값을 대입 • 리절트 페이지(printObject.jsp) • 프로퍼티product의 내용을 <s:property />태그를 사용하여 출력 • pruduct의 각 프로퍼티를 출력할 때 <s:label />태그를 사용

  30. 예제3. 객체 출력 • 모델 클래스 이름 • /webapp/WEB-INF/src/example/model/Product.java • 액션 클래스이름 • /webapp/WEB-INF/src/example/chapter2/PrintObjectAction.java • 리절트 페이지 이름 • /webapp/chapter2/printObject.jsp • 관련 태그 • <s:property />, <s:label /> • 작업 순서 • 모델 클래스 작성 • 액션 클래스 작성 • 리절트 페이지 작성 • struts.xml에 액션과 리절트 정의 • 결과 테스트

  31. 예제3. 객체 출력 • 모델 클래스 작성 /webapp/WEB-INF/src/example/model/Product.java

  32. 예제3. 객체 출력 • 액션 클래스 작성 /webapp/WEB-INF/src/example/chapter2/PrintObjectAction.java

  33. 예제3. 객체 출력 • 리절트 페이지 작성 /webapp/chapter2/printObject.jsp

  34. 예제3. 객체 출력 • struts.xml에 액션과 리절트 정의 /webapp/WEB-INF/src/struts.xml

  35. 예제3. 객체 출력 • 결과 테스트 클라이언트 PrintObjectAction클래스의 execute() 메소드 호출 프레임워크 execute()에서 Product 타입 프로퍼티product의 name 프로퍼티에“MP3 플레이어”, modelNo프로퍼티에“MP3-070701” 값을 저장한 후 “success” 문자열 값 반환 액션 밸류스택에 저장되어 있는 Product 타입의 프로퍼티product의 name, modelNo프로퍼티를<s:property /> 태그를 사용하여 출력 리절트 결과

  36. 예제4. 객체 목록 출력 • 모델 클래스(Product.java) • 예제3과 동일 • 액션 클래스(PrintObjectListAction.java) • Product 객체 타입의 List를 액션의 프로퍼티로 정의 • execute()에서 프로퍼티 listProduct에 4개의 Product 객체를 삽입 • 리절트 페이지(printObjectList.jsp) • listProduct의 내용을 테이블 형태로 출력 • 프로퍼티listProduct의 아이템들을 <s:iterator />와<s:property /> 태그를 사용하여 출력 • <s:if>태그를 사용하여 홀수 행마다 테이블 행 배경을 lightgrey색으로 출력

  37. 예제4. 객체 목록 출력 • 모델 클래스 이름 • /webapp/WEB-INF/src/example/model/Product.java • 액션 클래스이름 • /webapp/WEB-INF/src/example/chapter2/PrintObjectListAction.java • 리절트 페이지 이름 • /webapp/chapter2/printObjectList.jsp • 관련 태그 • <s:property />, <s:iterator />, <s:if /> • 작업 순서 • 액션 클래스 작성 • 리절트 페이지 작성 • struts.xml에 액션과 리절트 정의 • 결과 테스트

  38. 예제4. 객체 목록 출력 • 액션 클래스 작성 /webapp/WEB-INF/src/example/chapter2/PrintObjectListAction.java

  39. 예제4. 객체 목록 출력 • 리절트 페이지 작성 /webapp/chapter2/printObjectList.jsp

  40. 예제4. 객체 목록 출력 • struts.xml에 액션과 리절트 정의 /webapp/WEB-INF/src/struts.xml

  41. 예제4. 객체 목록 출력 • 결과 테스트 클라이언트 PrintObjectListAction클래스의 execute() 메소드 호출 프레임워크 execute()에서 java.util.List타입의 프로퍼티listProduct에 example.model.Product타입의 객체 4개를 아이템으로 저장한 후, “success” 문자열 값 반환 액션 밸류스택에 저장되어 있는listProduct프로퍼티의Product타입의 아이템 수만큼 <s:iterator/> 태그를 이용하여 반복 처리하면서, 각 아이템의 name, modelNo프로퍼티를<s:property />태그를 사용하여 출력 리절트 결과

  42. 예제5. 날짜출력 • 액션 클래스(PrintDateAction.java) • java.util.Date타입의 프로퍼티currDate를 정의 • execute()에서 프로퍼티 currDate에 현재 날짜 정보를 대입 • 리절트 페이지(printDate.jsp) • currDate의 날짜 정보를 출력 • <s:property /> 태그와 <s:date />태그를 이용해 각각 다른 형식으로 날짜 출력

  43. 예제5. 날짜 출력 • 액션 클래스이름 • /webapp/WEB-INF/src/example/chapter2/PrintDateAction.java • 리절트 페이지 이름 • /webapp/chapter2/printDate.jsp • 관련 태그 • <s:property />, <s:date /> • 작업 순서 • 액션 클래스 작성 • 리절트 페이지 작성 • struts.xml에 액션과 리절트 정의 • 결과 테스트

  44. 예제5. 날짜 출력 • 액션 클래스 작성 /webapp/WEB-INF/src/example/chapter2/PrintDateAction.java

  45. 예제5. 날짜 출력 • 리절트 페이지 작성 /webapp/chapter2/printDate.jsp

  46. 예제5. 날짜 출력 • struts.xml에 액션과 리절트 정의 /webapp/WEB-INF/src/struts.xml

  47. 예제5. 날짜 출력 • 결과 테스트 클라이언트 PrintDateAction클래스의 execute() 메소드 호출 프레임워크 execute()에서 java.util.Date타입의 프로퍼티currDate에 현재 시간을 저장한 후 “success” 문자열 값 반환 액션 밸류스택에 저장되어 있는프로퍼티currDate의 값을 <s:property value=“currDate”/>, <s:date name=“currDate” /> , <s:date name=“currDate” format=“yyyy-MM-ddhh:mm:ss” /> 3가지 형식으로 출력 리절트 결과

  48. 정리 • 예제 정리 • 문자열 출력 • 문자열 목록 출력 • 객체 출력 • 객체 목록 출력 • 날짜 출력 • 스트럿츠 개발 환경 설정에 대해 다루었고, 액션 클래스와 리절트 페이지 작성, 설정 및 태그에 대한 기본적인 개념을 익혀 보았다.

More Related