struts2 2 장 기본 예제를 통해 스트럿츠 2 와 친해지기. 부록 a1. 개발 환경...
Post on 19-Dec-2015
280 views
TRANSCRIPT
Struts2
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 파일에서 한글을 사용할 수 있게 설정
추가
Eclipse Tomcat Plugin 설치 \EclipsePlugin\tomcatPluginV31.zip 압축해제
com.sysdeo.eclipse.tomcat_3.1.0 폴더 통째로
이클립스폴더 \plugins 에 복사
Eclipse Tomcat Plugin 설정 Window – Preferences – Tomcat 항목 선택
Tomcat version : Version 5.x Tomcat home : 톰캣폴더 Context declaration mode : Context files
Window-Preferences-Tomcat-Advanced 항목 선택 Tomcat base : 톰캣폴더
Eclipse Property Editor Plugin 설치 \EclipsePlugin\
jp.gr.java_conf.ussiy.app.propedit_4.8.1_for_eclipse3.0.zip 압축해제
features, plugins 폴더 통째로 이클립스 폴더에 복사
메시지 번들을 좀 더 편리하게 편집 가능 한글로 작성한 메시지 번들이 자동으로 유니코드로 번역되는 기능을
사용할 수 있음
Eclipse Perspective 버튼 추가 Debug Perspective 추가
Resource Perspective 추가
디버그 작업 , 자원을 찾아볼 때 유용
Struts2 설치 struts2.0.9 버전
Struts2\struts-2.0.9-all.zip ( 부록 CD) http://struts.apache.org/download.cgi#struts209
폴더 명 폴더 및 파일 설명strut-s2.0.9
apps 샘플 어플리케이션docs 스트럿츠 2.0.9 용 문서libs 스트럿츠 2.0.9 용 라이브러리src 스트럿츠 2.0.9 용 소스
Project 생성 New Project – Java – Tomcat Project 선택
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 에 복사
이클립스 프로젝트 속성창에서 라이브러리 추가
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/j2eehttp://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.FilterDispatche
r</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\we-b.xml 생성
struts.xml 생성
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Soft-ware Foundation//DTD Struts Configura-tion 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
struts.properties 생성 스트럿츠 2 프레임워크가 사용하는 환경 설정 값들을
정의 struts2/WEB-INF\src\struts.properties
struts.i18n.reload=truestruts.devMode=falsestruts.configuration.xml.reload=truestruts.continuations.package=org.apache.struts2.showcasestruts.custom.i18n.resources=globalMessages#struts.action.extension=jspastruts.url.http.port=8080#struts.freemarker.manager.classname=customFreemarkerManagerstruts.serve.static=truestruts.serve.static.browserCache=falsestruts.multipart.maxSize=2097252
Encoding 설정 프로젝트 Properties – Resource – Text file en-
coding UTF-8 선택 한글 처리를 위한 문자셋
세팅 끝 ~
예제 1. 문자열 출력 액션 클래스 (PrintStringAction.java)
String 타입의 프로퍼티 greetings 를 정의 execute() 에서 greetings 프로퍼티에 “반갑다 !
스트럿츠 2.” 문자열 대입
리절트 페이지 (printString.jsp) <s:property /> 태그를 사용하여 greetings 값을 화면에
출력
예제 1. 문자열 출력액션 클래스 [PrintStringAction.java]
private String greetings;
스트럿츠 2밸류 스택 [PrintStringAction 객체 ]
String greetings;
리절트 페이지 [printString.jsp]
<s:property value=“greetings” />
예제 1. 문자열 출력 액션 클래스 이름
/webapp/WEB-INF/src/example/chapter2/PrintStringAction.java
리절트 페이지 이름 /webapp/chapter2/printString.jsp
관련 태그 <s:property />
작업 순서 액션 클래스 작성 리절트 페이지 작성 struts.xml 에 액션과 리절트 정의 결과 테스트
예제 1. 문자열 출력• 액션 클래스 작성
/webapp/WEB-INF/src/example/chapter2/PrintStringAction.java
예제 1. 문자열 출력• 리절트 페이지 작성
/webapp/chapter2/printString.jsp
예제 1. 문자열 출력• struts.xml 에 액션과 리절트 정의
/webapp/WEB-INF/src/struts.xml
결과 테스트예제 1. 문자열 출력
PrintStringAction 클래스의 execute() 메소드 호출
execute() 에서 프로퍼티 greetings 에 “반갑다 ! 스트럿츠 2.” 문자열 값 대입 후 “ success” 문자열 값 반환
밸류 스택에 저장되어 있는 프로퍼티 greetings 의 값을<s:property value=“greetings” /> 태그를 사용하여 출력
클라이언트
프레임워크
액션
리절트 결과
OGNL(Object Graph Navigation Language) <s:property value=“greetings” />
예제 1. 문자열 출력
OGNL 표현식으로 해석
getGreetings() 호출
예제 1. 문자열 출력
예제 2. 문자열 목록 출력 액션 클래스 (PrintStringListAction.java)
List 타입의 프로퍼티 listString 을 정의 execute() 에서 listString 에 “ MP3 플레이어” , “ 노트북
PC”, “PDA”, “ 휴대폰” 값을 추가
리절트 페이지 (printStringList.jsp) <s:iterator /> 태그와 <s:property /> 태그를 통해
listString 의 문자열 목록을 출력
예제 2. 문자열 목록 출력 액션 클래스 이름
/webapp/WEB-INF/src/example/chapter2/PrintStringListAction.-java
리절트 페이지 이름 /webapp/chapter2/printStringList.jsp
관련 태그 <s:property />, <s:iterator />
작업 순서 액션 클래스 작성 리절트 페이지 작성 struts.xml 에 액션과 리절트 정의 결과 테스트
예제 2. 문자열 목록 출력• 액션 클래스 작성
/webapp/WEB-INF/src/example/chapter2/PrintStringListAction.java
예제 2. 문자열 목록 출력• 리절트 페이지 작성
/webapp/chapter2/printStringList.jsp
예제 2. 문자열 목록 출력• struts.xml 에 액션과 리절트 정의
/webapp/WEB-INF/src/struts.xml
결과 테스트예제 2. 문자열 목록 출력
PrintStringListAction 클래스의 execute() 메소드 호출
execute() 에서 프로퍼티 listString 에 “ MP3 플레이어” , “ 노트북 PC”, “PDA”, “ 휴대폰” 4 개의 문자열 추가한 후 “ success” 문자열 값 반환
밸류 스택에 저장되어 있는 List 타입의 프로퍼티 listString 의 아이템을 <s:iterator /> 태그를 이용하여 아이템의 수만큼 반복하며 <s:property /> 태그를 사용하여 아이템 값 출력
클라이언트
프레임워크
액션
리절트 결과
예제 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 /> 태그를 사용
예제 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 에 액션과 리절트 정의 결과 테스트
예제 3. 객체 출력• 모델 클래스 작성
/webapp/WEB-INF/src/example/model/Product.java
예제 3. 객체 출력• 액션 클래스 작성
/webapp/WEB-INF/src/example/chapter2/PrintObjectAction.java
예제 3. 객체 출력• 리절트 페이지 작성
/webapp/chapter2/printObject.jsp
예제 3. 객체 출력• struts.xml 에 액션과 리절트 정의
/webapp/WEB-INF/src/struts.xml
결과 테스트예제 3. 객체 출력
PrintObjectAction 클래스의 execute() 메소드 호출
execute() 에서 Product 타입 프로퍼티 product 의 name 프로퍼티에 “ MP3 플레이어” , modelNo 프로퍼티에 “ MP3-070701” 값을 저장한 후 “ success” 문자열 값 반환
밸류 스택에 저장되어 있는 Product 타입의 프로퍼티 product의 name, modelNo 프로퍼티를 <s:property /> 태그를 사용하여 출력
클라이언트
프레임워크
액션
리절트결과
예제 4. 객체 목록 출력 모델 클래스 (Product.java)
예제 3 과 동일 액션 클래스 (PrintObjectListAction.java)
Product 객체 타입의 List 를 액션의 프로퍼티로 정의 execute() 에서 프로퍼티 listProduct 에 4 개의 Product 객체를
삽입
리절트 페이지 (printObjectList.jsp) listProduct 의 내용을 테이블 형태로 출력 프로퍼티 listProduct 의 아이템들을 <s:iterator /> 와 <s:property />
태그를 사용하여 출력 <s:if> 태그를 사용하여 홀수 행마다 테이블 행 배경을 lightgrey 색으로
출력
예제 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 에 액션과 리절트 정의 결과 테스트
예제 4. 객체 목록 출력• 액션 클래스 작성
/webapp/WEB-INF/src/example/chapter2/PrintObjectListAction.java
예제 4. 객체 목록 출력• 리절트 페이지 작성
/webapp/chapter2/printObjectList.jsp
예제 4. 객체 목록 출력• struts.xml 에 액션과 리절트 정의
/webapp/WEB-INF/src/struts.xml
결과 테스트예제 4. 객체 목록 출력
PrintObjectListAction 클래스의 execute() 메소드 호출
execute() 에서 java.util.List 타입의 프로퍼티 listProduct 에 example.model.Product 타입의 객체 4 개를 아이템으로 저장한 후 , “success” 문자열 값 반환
밸류 스택에 저장되어 있는 listProduct 프로퍼티의 Product타입의 아이템 수만큼 <s:iterator/> 태그를 이용하여 반복 처리하면서 , 각 아이템의 name, modelNo 프로퍼티를 <s:property /> 태그를 사용하여 출력
클라이언트
프레임워크
액션
리절트 결과
예제 5. 날짜 출력 액션 클래스 (PrintDateAction.java)
java.util.Date 타입의 프로퍼티 currDate 를 정의 execute() 에서 프로퍼티 currDate 에 현재 날짜 정보를
대입
리절트 페이지 (printDate.jsp) currDate 의 날짜 정보를 출력 <s:property /> 태그와 <s:date /> 태그를 이용해 각각
다른 형식으로 날짜 출력
예제 5. 날짜 출력 액션 클래스 이름
/webapp/WEB-INF/src/example/chapter2/PrintDateAction.-java
리절트 페이지 이름 /webapp/chapter2/printDate.jsp
관련 태그 <s:property />, <s:date />
작업 순서 액션 클래스 작성 리절트 페이지 작성 struts.xml 에 액션과 리절트 정의 결과 테스트
예제 5. 날짜 출력• 액션 클래스 작성
/webapp/WEB-INF/src/example/chapter2/PrintDateAction.java
예제 5. 날짜 출력• 리절트 페이지 작성
/webapp/chapter2/printDate.jsp
예제 5. 날짜 출력• struts.xml 에 액션과 리절트 정의
/webapp/WEB-INF/src/struts.xml
결과 테스트예제 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-dd hh:mm:ss” /> 3 가지 형식으로 출력
클라이언트
프레임워크
액션
리절트 결과
예제 정리 문자열 출력 문자열 목록 출력 객체 출력 객체 목록 출력 날짜 출력
스트럿츠 개발 환경 설정에 대해 다루었고 , 액션 클래스와 리절트 페이지 작성 , 설정 및 태그에 대한 기본적인 개념을 익혀 보았다 .
정리
끝