- 요소

필터 :URL의 확장자가 action 이면 FilterDispatcher 에 의해서 액션을 실행하기 위한 환경을 구축하도록 한다  (web.xml)

/* web.xml(struts2-core-2.3.4.1.jar) */

<?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>struts2_ActionSupport</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
 
  <!-- Struts2 FilterDispatcher -->
    <filter>
        <filter-name>struts2</filter-name>
        <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
            <!-- Struts2 인코딩 설정(struts-default 참조) -->
            <init-param>
                <param-name>struts.i18n.encoding</param-name>
                <param-value>UTF-8</param-value>
            </init-param>
            <!-- Struts2 인코딩 설정(struts-default 참조) -->
    </filter>
    <filter-mapping>
        <filter-name>struts2</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
 
</web-app>



액션 : 리절트가 필요로 하는 메시지를 제공한다 (action.java)

/* action.java */

package action;

import com.opensymphony.xwork2.ActionSupport;

/* Struts2 ActionSupport 상속 */
public class UserRegAction extends ActionSupport{

    private String userId;
    private String userPw;
    private String userName;

    /* Getter & Setter */
    public String getUserId() {
        return userId;
    }

    public void setUserId(String userId) {
        this.userId = userId;
    }

    public String getUserPw() {
        return userPw;
    }

    public void setUserPw(String userPw) {
        this.userPw = userPw;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    @Override
    public String execute()throws Exception{
        return SUCCESS;
    }//end - execute Method


    @Override
    public void validate() {
        if(userId == null || userId.equals("")) {
            addFieldError("userId", "회원아이디를 입력해 주십시오");
        }
        if(userPw == null || userPw.equals("")) {
            addFieldError("userPw", "비밀번호를 입력해 주십시오");
        }
        if(userName == null || userName.equals("")) {
            addFieldError("userName", "이름을 입력해 주십시오");
        }
    }// end - validate Method()

}// end - class



매핑액션 : 실행 후 결과를 처리할 리절트와의 매피을 설정  (struts.xml)

/* 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="actionSupport" namespace="" extends="struts-default">
        <action name="UserRegForm">
            <result>/userRegForm.jsp</result>
        </action>
        <action name="UserRegAction" class="action.UserRegAction">
            <interceptor-ref name="params"/>
            <interceptor-ref name="validation"/>
            <interceptor-ref name="workflow"/>
            <result name="input">/userRegForm.jsp</result>
            <result name="success">/userRegSuccess.jsp</result>
        </action>
    </package>
</struts>



리절트 : 메시지를 출력하기 위한 화면 처리를 담당  (view.jsp)

/* view.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>회원가입 결과</title>
</head>
<body>
    <center>
    <b><font color="red">회원 가입이 완료되었습니다.</font></b><p>
    아 이 디 : ${userId}<p>
    비밀번호 : ${userPw}<p>
    이 름 : ${userName}<p>
    </center>
</body>
</html>

+ Recent posts