페이지

2015. 2. 23.

[struts2] 폼을 통해 입력하고 출력하는 예제

1. WebContent/WEB-INF/lib에Struts에 필요한 라이브러리 복사

commons-logging-1.0.4.jar
freemarker-2.3.8.jar
ognl-2.6.11.jar
struts2-core-2.0.14.jar
xwork-2.0.4.jar



2.filter, filter-mapping 설정

[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" 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">
   
    <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>*.action</url-pattern>
    </filter-mapping>

</web-app>



3. src에 struts.xml을 생성(컨트롤러)

[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="test" extends="struts-default">
        <action name="hello" class="test.action.HelloAction">
            <result>/0223/hello.jsp</result>
        </action>
        <action name="hello2" class="test.action.HelloWorld">
            <result>/0223/result.jsp</result>
        </action>
    </package>
</struts>

http://localhost:8080/struts/hello2.action를 브라우저에서 요청하면
hello2에 해당하는 액션인 HelloWorld.java를 동작시킴



4 HelloAction.java 액션만들기

[HelloAction.java]

public class HelloAction {
    public String execute(){
        System.out.println("execute");
        return "ok";
    }
}

여기서 ok가 리턴되면 호출된 곳
<action name="hello" class="test.action.HelloAction">으로 돌아간다.

리턴 결과에 따라서 view를 선택할수도 있다.
<action name="hello" class="test.action.HelloAction">
    <result name="ok">/0223/hello.jsp</result>
    <result name="save">/0223/hello2.jsp</result>
</action>



5. view

[hello.jsp]

<%@ page language="java" contentType="text/html; charset=EUC-KR" pageEncoding="EUC-KR"%>

<h3>${result}</h3>

여기선 뷰가 필요 없음 ㅠㅠ

콘솔로 execute가 찍히는지 보기만함













========================================================================

[form.jsp]
<%@ page language="java" contentType="text/html; charset=EUC-KR" pageEncoding="EUC-KR"%>

<form action="/struts/hello2.action">
    name : <input type="text" name="name" />
    <input type="submit" value="전송"/>
</form>

[result.jsp]
<%@ page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR"%>
<h2>${msg}</h2>
<h2>${num}</h2>
<h2>${name}</h2>
<h2>${result}</h2>

[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" 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">
   
    <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>*.action</url-pattern>
    </filter-mapping>

</web-app>

[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="test" extends="struts-default">
        <action name="hello" class="test.action.HelloAction">
            <result>/0223/hello.jsp</result>
        </action>
        <action name="hello2" class="test.action.HelloWorld">
            <result>/0223/result.jsp</result>
        </action>
    </package>
</struts>

[HelloWorld .java]
package test.action;

import com.opensymphony.xwork2.ActionSupport;

public class HelloWorld extends ActionSupport{
    private String name;
    private String msg;
   
    public String getMsg() {
        return msg;
    }
    public int getNum(){
        return 500;
    }
    public String getName(){
        return name;
    }
    public String getResult(){
        return "처리완료";
    }
    public void setName(String name) {
        this.name = name;
        System.out.println("set : " + name);
    }
    public void validate(){
        System.out.println("validate");
    }
    public String execute() throws Exception {
        msg = "Hello," + name;
        System.out.println("execute");
        return SUCCESS;
    }
}