방법2) ActionSupport클래스를 상속받아 만들기


1) insert.html
2) insertOk.jsp
3) InsertAction.java
4) struts.xml
5) example.xml (action 매핑. struts.xml에 include됨 )

환경설정은 Struts2 #01과 동일.

------------------------------------------------------------------------------

1) insert.html

<!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=UTF-8">
<title>Insert title here</title>
</head>
<body>
    <form method="post" action="insert.action" >
        이름<input type="text" name="name" /><br/>
        나이<input type="text" name="age" /><br/>
        전화<input type="text" name="phone" /><br/>
        <input type="submit" value="가입" /><br/>
    </form>
</body>
</html>










; 이름,나이,전화번호를 입력하고 가입버튼(submit)하면 insertOk로 결과 응답.



2) insertOk.jsp


<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!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=UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1>회원 정보</h1>
${ result }
</body>
</html>









; action클래스를 통해 받은 내용.


3) InsertAction.java
;방법 2) ActionSupport 이용
; 파라미터 받을 때는 setter
; 값을 응답할때는 getter 만들어 사용한다.


package test.action;

import com.opensymphony.xwork2.ActionSupport;

public class InsertAction extends ActionSupport{
    private String name;
    private int age;
    private String phone;
    private String result;
   
    //setter 메소드가 존재하면 파라미터값들이 자동 지정
    public void setName(String name) {
        this.name = name;
    }
    public void setAge(int age) {
        this.age = age;
    }
    public void setPhone(String phone) {
        this.phone = phone;
    }
    public String getResult() {
        return result;
    }
    @Override
    public String execute() throws Exception {
        result="이름:" + name +
               "나이:" + age +
               "전화:" + phone;
        return SUCCESS;
    }
}



4)  struts.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
    "http://struts.apache.org/dtds/struts-2.3.dtd">

<struts>
    <package name="default" namespace="/" extends="struts-default">
    </package>

    <include file="example.xml"/>
</struts>


5) example.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="example" namespace="/" extends="default">
        <!-- 액션매핑하기 -->
        <action name="test" class="test.action.MyTestAction">
            <result name="success">/view/result.jsp</result>
        </action>
        <action name="insert" class="test.action.InsertAction">
            <result name="success">/view/insertOk.jsp</result>
        </action>
    </package>
</struts>



+ Recent posts