HelloWorld
1. web.xml
<?
xml
version
=
"1.0"
encoding
=
"UTF-8"
?>
<
web-app
xmlns
=
"http://java.sun.com/xml/ns/javaee"
xmlns:xsi
=
"http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation
=
"http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
version
=
"2.5"
>
<!-- 1 -->
<
filter
>
<
filter-name
>struts2</
filter-name
>
<
filter-class
>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</
filter-class
>
</
filter
>
<!-- 2 -->
<
filter-mapping
>
<
filter-name
>struts2</
filter-name
>
<
url-pattern
>/*</
url-pattern
>
</
filter-mapping
>
</
web-app
>
1) StrutsPrepareAndExecuteFilter를 서블릿 필터로 설정
2) 서블릿 필터를 적용할 URL 패턴을 지정
- URL 범위를 좁힌다?
- 좁히면 스트럿츠2가 제공하는 정적 콘텐츠에 엑세스 할 때 번거로운 과정이 필요
2. HelloAction.java
package
sample;
public
class
HelloAction {
//1
private
String message;
public
String getMessage(){
return
message;
}
public
void
setMessage(String message){
this
.message = message;
}
//2
public
String execute(){
message =
"World"
;
return
"success"
;
}
}
1) 뷰가 액세스할 데이터
- 인스턴스 변수 message의 값을 Context Map에 참조 할 수고 뷰에서 JavaBeans의 프로퍼티에 엑세스 할 수 있다
2) 액션이 요청에 대응하는 개별 처리를 구현
a. 액션 메소드
- 개별 처리를 액션 메소드라한다
- 인수가 없다, 메소드 명이 "execute"으로 한다
b. Result이름
- "success"라는 문자열이 Result이름이다
- Result 이름으로 지정된 Result가 처리 한다
3. JSP
<%@ page language="java" contentType="text/html; charset=EUC-KR"
pageEncoding="UTF-8"%>
<%@ taglib prefix = "s" uri="/struts-tags" %>
<!DOCTYPE html>
<
head
>
<
meta
http-equiv
=
"Content-Type"
content
=
"text/html; charset=EUC-KR"
>
<
title
>Insert title here</
title
>
</
head
>
Hello, <
s:property
value
=
"%{message}"
/>
1) <%@ taglib prefix = "s" uri="/struts-tags" %>
- Struts-Tags의 태그 라이브러리 선언한다
2) Hello, <s:property value="%{message}"/>
- Context map에 저장된 HelloAction 오브젝트로 부터 OGNL을 사용해 message 값을 취득한다
4. struts.xml
<?
xml
version
=
"1.0"
encoding
=
"UTF-8"
?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
<
struts
>
<!-- 1 -->
<
constant
name
=
"struts.devMode"
value
=
"true"
/>
<
package
name
=
"sample"
namespace
=
"/"
extends
=
"struts-default"
>
<!-- 2 -->
<
action
name
=
"hello"
class
=
"sample.HelloAction"
>
<!-- 3 -->
<
result
name
=
"success"
>/WEB-INF/jsp/hello.jsp</
result
>
</
action
>
</
package
>
</
struts
>
1) 개발 모드를 유효하게 설정
- 스트러츠는 전체 동작에 관한 다양한 항목을 설정할 수 있다
- constant 태그 사용
- struts.xml파일은 기본 패키지에 배치
2) 액션 이름 "hello"와 Action클래스의 완전 수식 클래스 이름 지정
3) result 태그의 name 속성으로 지정한 "success는 액션 메소드가 반환값으로 돌려주는 Result이름
5. pom.xml
<
project
xmlns
=
"http://maven.apache.org/POM/4.0.0"
xmlns:xsi
=
"http://www.w3.org/2001/XMLSchema-instance"
<
modelVersion
>4.0.0</
modelVersion
>
<
groupId
>kyle</
groupId
>
<
artifactId
>HelloWorld</
artifactId
>
<
packaging
>war</
packaging
>
<
version
>0.0.1-SNAPSHOT</
version
>
<
name
>HelloWorld Maven Webapp</
name
>
<
dependencies
>
<
dependency
>
<
groupId
>junit</
groupId
>
<
artifactId
>junit</
artifactId
>
<
version
>3.8.1</
version
>
<
scope
>test</
scope
>
</
dependency
>
<
dependency
>
<
groupId
>org.apache.struts</
groupId
>
<
artifactId
>struts2-core</
artifactId
>
<
version
>2.3.1.2</
version
>
</
dependency
>
<
dependency
>
<
groupId
>org.apache.struts</
groupId
>
<
artifactId
>struts2-spring-plugin</
artifactId
>
<
version
>2.3.1.2</
version
>
</
dependency
>
<
dependency
>
<
groupId
>org.apache.struts</
groupId
>
<
artifactId
>struts2-convention-plugin</
artifactId
>
<
version
>2.3.1.2</
version
>
</
dependency
>
<
dependency
>
<
groupId
>javax.servlet</
groupId
>
<
artifactId
>servlet-api</
artifactId
>
<
version
>2.5</
version
>
</
dependency
>
<
dependency
>
<
groupId
>javax.servlet</
groupId
>
<
artifactId
>jstl</
artifactId
>
<
version
>1.2</
version
>
</
dependency
>
<
dependency
>
<
groupId
>log4j</
groupId
>
<
artifactId
>log4j</
artifactId
>
<
version
>1.2.16</
version
>
</
dependency
>
</
dependencies
>
<
build
>
<
finalName
>HelloWorld</
finalName
>
<
plugins
>
<
plugin
>
<
groupId
>org.apache.maven.plugins</
groupId
>
<
artifactId
>maven-compiler-plugin</
artifactId
>
<
version
>2.3.2</
version
>
<
configuration
>
<
source
>1.7</
source
>
</
configuration
>
</
plugin
>
<
plugin
>
<
groupId
>org.apache.maven.plugins</
groupId
>
<
artifactId
>maven-war-plugin</
artifactId
>
<
version
>2.4</
version
>
<
configuration
>
<
warSourceDirectory
>webapp</
warSourceDirectory
>
</
configuration
>
</
plugin
>
</
plugins
>
</
build
>
</
project
>
FullSource : https://github.com/KyleJeong/Struts2/tree/master/HelloWorld
'Back-End > Struts2' 카테고리의 다른 글
[Struts2] Action(2) - Action Class (0) | 2016.09.25 |
---|---|
[Struts2] Action(1) - Struts package, NameSpace (0) | 2016.09.25 |
[Struts2] Struts2 config file(스트러츠2설정파일) (0) | 2016.09.20 |
[Struts2] Struts2 구성 (0) | 2016.09.20 |
[Struts2] Struts2란 (0) | 2016.09.19 |