web.xml
web.xml 파일에 Struts 2 Framework 을 연동하는 내용을 기술합니다. 아래의 내용과 같이 web.xml 을 구성합니다.
web.xml 에 FilterDispatcher 를 등록하여 request 가 발행하면 Struts 2 Framework 가 그 request 를 받도록 설정합니다.
/WEB-INF/web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"> <display-name>basic</display-name>
<filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
</web-app> |
Struts.xml
Struts 에서 작성한 어플리케이션이 정상적인 동작을 하기 위해서 필요한 환경설정 내용을 포함하고 있습니다.
/WEB-INF/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> <constant name="struts.devMode" value="true"/>
<include file="greeting.xml"/> </struts> |
Struts.xml에서 인클루드 한 Greeting.xml도 작성해줍니다. 이 Greeting.xml에 액션 클래스가 설정되어있습니다.
/WEB-INF/greeting.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="greeting" extends="struts-default" namespace="/greeting"> <default-action-ref name="noPage"/>
<action name="greeting_*" method="{1}" class="struts2.hello.GreetingAction"> <result name="success">/hello.jsp</result> </action>
<action name="noPage"> <result>/defaultNoPage.jsp</result> </action> </package>
</struts> |
와일드카드 매핑을 사용하여 greeting_hello.action 처럼 요청 Acrion명에 따라 다른 메소드를 실행하도록 하였습니다.
Action Class
위의 greeting.xml에 설정된 액션 클래스를 작성해줍니다.
GreetingAction.java
package struts2.hello;
public class GreetingAction { public String message;
public String hello() throws Exception { message = "Hello..."; return "success"; }
public String goodbye() throws Exception { message = "Goodbye..."; return "success"; }
public String getMessage() { return message; }
} |
ViewPage
사용자에게 보여줄 뷰페이지를 작성합니다. EL 표현식을 사용해서 메시지를 출력해보았습니다.
hello.jsp
<%@ page language="java" contentType="text/html; charset=EUC-KR" pageEncoding="EUC-KR"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=EUC-KR"> <title>Insert title here</title> </head> <body> <h1>${message }</h1> </body> </html> |
요청결과
http://localhost/namespace/greeting/greeting_hello.action 로 요청한 결과는 다음과 같습니다.
'개발자 센터 > Struts2' 카테고리의 다른 글
struts2에서 세션(session) 사용하기 (0) | 2009.12.14 |
---|---|
Struts2 Action : 액션 (0) | 2009.12.11 |
Struts2 Framework 아키텍처 (0) | 2009.12.11 |
Struts2 Framework의 특징 (0) | 2009.12.11 |
Struts2 Framework 환경설정 방법 (0) | 2009.12.11 |