본문 바로가기

Struts/Struts Programming

[Struts] ActionSupport 클래스를 확장한 액션

반응형

■ ActionSupport 클래스를 확장한 액션

 ⓛ Validateable과 ValidationAware인터페이스는 데이터의 검증을 위한 기능을 제공한다.

     TextPro-vider와 LocaleProvider 인터페이스는 여러 언어를 지원하는 웹 사이트를 개발할 때 필요한

     지역화 및 국제화 기능을 제공한다.


 ② 뿐만 아니라 파일의 경로와 같은 path 또는 데이터베이스 URL을 properties 파일을 작성하여 기재한 후 Ac-tionSupport 클래스가

    제공하는 getText( ) 메소드를 이용하여 쉽게 가져올 수 있다는 장점도 있다.



■ 유효성 검사

 -. 스트럿츠2 프레임워크는 다양한 방법의 유효성 검사를 지원한다.

 -. ActionSupport 클래스는 Validateable 인터페이스의 validate( ) 메서드와, Validation-Aware 인터페이스의 addFieldError( ) 메소드와

    함께 사용하여 유효성을 검사하는 기능을 제공한다.

 UserRegAction.java 
package action;

import com.opensymphony.xwork2.ActionSupport;

public class UserRegAction extends ActionSupport {

    private String userId;
    private String userPw;
    private String userName;
   
    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;
    }
   
    @Override
    public void validate() {
       
        if(userId == null || "".equals(userId)) {
           
            addFieldError("userId", "회원 아이디를 입력해 주십시오.");
        }
       
        if(userPw == null || "".equals(userPw)) {
           
            addFieldError("userPw", "비밀번호를 입력해 주십시오.");
        }
       
        if(userName == null || "".equals(userName)) {
           
            addFieldError("userName", "이름을 입력해 주십시오.");
        }   
    }
}
 userRegForm.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>회원가입</title>
</head>
<body>
<center>
<h2>회원가입</h2><p/>
<form method = "post" action = "UserRegAction.action">
<table cellspacing = "10">
    <tr>
        <td>아이디</td>
        <td><input type = "text" name = "userId" value = "${userId}"></td>
        <td align = "left"><font color = "red">${fieldErrors.userId}</font></td>
    </tr>
    <tr>
        <td>비밀번호</td>
        <td><input type = "password" name = "userPw" value = "${userPw}"></td>
        <td align = "left"><font color = "red">${fieldErrors.userPw}</font></td>
    </tr>
    <tr>
        <td>이름</td>
        <td><input type = "text" name = "userName" value = "${userName}"></td>
        <td align = "left"><font color = "red">${fieldErrors.userName}</font></td>
    </tr>
    <tr>
        <td colspan = "3"><input type = "submit" value = "보내기"></td>
    </tr>
</table>
</form>
</center>
</body>
</html>

 userRegSuccess.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>회원 가입 완료</title>
</head>
<body>
<center>
<body>
<b><font color = "red">회원 가입이 완료되었습니다.</font></b><p/>
아이디 : ${userId}<p/>
비밀번호 : ${userPw}<p/>
이름 : ${userName}<p/>
</body>
</center>
</body>
</html>
 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 = "ch04" extends = "struts-default">
        <action name = "HelloWorld02" class = "action.HelloWorld02">
            <interceptor-ref name = "params"/>
            <result name = "success">/helloWorld.jsp</result>
        </action>
       
        <action name = "UserRegForm">
            <result>/jsp/userRegForm.jsp</result>
        </action>
        <action name = "UserRegAction" class = "action.UserRegAction">
              <interceptor-ref name = "params"/>
             
              <!-- validation, workflow 둘다 SUCCESS가 아니면 Action.INPUT을 리턴하고 -->
              <!-- 액션은 실행하지 않는다. -->
             
              <interceptor-ref name = "validation"/>
              <!-- 만약 액션이 Validateable을 구현했다면 액션의 validate() 메서드를 호출한다. -->
              <!-- 만약 액션이 ValidationAware를 구현했다면 액션의 hasError() 메서드를 호출한다. -->
             
              <interceptor-ref name = "workflow"/>
              <!-- 액션에 정의한 validate() 메소드를 호출한 후에 에러가 있으면 're-trun INPUT;'을 하고, -->
              <!-- 에러가 없으면 액션에 정의한 execute() 메소드를 호출한다. -->
             
              <result name = "input">/jsp/userRegForm.jsp</result>
              <result name = "success">/jsp/userRegSuccess.jsp</result>
          </action>
    </package>
</struts>

 출력화면

 출력화면




반응형