■ ModelDriven 인터페이스
 -. ModelDriven 액션 클래스를 생성하기 위해서는 ModelDriven 인터페이스를 구현해야 한다.
-. ModelDriven 인터페이스를 구현한 클래스에는 getModel() 메소드를 오버라이딩 해야 한다.
-. 이 메소드는 새롭게 생성된 도메인 오브젝트 인스턴스를 반환한다.
public class UserRegAction3 implements Action, Preparable, ModelDriven {  | 
-. ModelDriven 액션에 적용되도록 하기 위해서 struts.xml에 modelDriven 인터셉터를 추가해야 한다.
          <action name = "UserRegForm3">  | 
-. modelDriven 인터셉터 덕분에 userRegForm.jsp 입력 폼에서는 다음과 같이 userId만으로도
도메인 인스턴스 프로퍼티에 값을 저장할 수 있게 된다.
  userRegForm3.jsp  | 
 <input type = "text" name = "userId">  | 
■ Preparable 인터페이스
-. Preparable 인터페이스는 액션의 메소드(execute)를 실행하기 전에 다른 로직을 실행하고 싶을 때 사용한다.
-. Preparable 인터페이스를 구현한 클래스에는 prepare() 메소드를 오버라이딩 해야 한다.
-. prepare() 메소드는 액션 메소드가 실행하기 전에 수행되어야 하는 로직을 기술한다.
public class UserRegAction3 implements Action, Preparable ……ⓐ                                          , ModelDriven {     @Override      | 
 ① Preparable 인터페이스(ⓐ)는 주로 ModelDriven 인터페이스와 함께 사용한다.
 ② modelDriven 인터셉터가 사용할 도메인 오브젝트를 prepare( ) 메소드(ⓑ)에서 미리 인스턴스화하는 로직을 기술한다.
③ prepare 인터셉터가 prepare( ) 메소드를 호출하여 도메인 오브젝트를 인스턴스화해 놓으면 modelDriven 인터셉터의
    getModel( ) 메소드(ⓒ)는 미리 사용할 준비가 되어있는 도메인 오브젝트 인스턴스를 반환할 수 있게 된다.
-. Preparable 액션에 적용되도록 하기 위해서 struts.xml에 prepare 인터셉터를 추가해야 한다.
|           <action name = "UserRegForm3"> <result>/jsp/userRegForm3.jsp</result> </action> <action name = "UserRegAction3" class = "action.UserRegAction3"> <interceptor-ref name = "prepare"/> <interceptor-ref name = "modelDriven"/> <interceptor-ref name = "params"/> <result name = "success">/jsp/userRegSuccess3.jsp</result> </action>  | 
UserRegAction3.java  | 
| package action; import com.opensymphony.xwork2.Action; import com.opensymphony.xwork2.ModelDriven; import com.opensymphony.xwork2.Preparable; import model.User; import dao.UserDao; public class UserRegAction3 implements Action, Preparable, ModelDriven { User user; public User getUser() { return user; } @Override public String execute() throws Exception { UserDao uerDao = new UserDao(); uerDao.create(user); return SUCCESS; } @Override public void prepare() throws Exception { user = new User(); } public Object getModel() { return user; } }  | 
| struts.xml | 
<?xml version="1.0" encoding="UTF-8"?>  | 
| userRegForm3.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 = "UserRegAction3.action"> <table cellspacing = "10"> <tr> <td>아이디</td> <td><input type = "text" name = "userId"></td> </tr> <tr> <td>비밀번호</td> <td><input type = "password" name = "userPw"></td> </tr> <tr> <td>이름</td> <td><input type = "text" name = "userName"></td> </tr> <tr> <td colspan = "2"><input type = "submit" value = "보내기"></td> </tr> </table> </form> </center> </body> </html>  | 
| userRegSuccess3.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> <b><font color = "red">회원 가입이 완료되었다.</font></b><p/> 아이디 : ${userId}<p/> 비밀번호 : ${userPw}<p/> 이름 : ${userName}<p/> </center> </body> </html>  | 
출력화면  | 
■ 액션 클래스 비교 정리
-. POJO 객체는 의존성이 없으므로 독립적인 작업에 좋다. 또한 Action 인터페이스나 ActionSupport 클래스를 상속받았다면
스트럿츠 2 환경에서 제공되는 몇 가지 이점을 그대로 누릴 수 있다는 장점이 있다.
'Struts > Struts Programming' 카테고리의 다른 글
| [Struts] 인터셉터란? (0) | 2012.09.11 | 
|---|---|
| [Struts] 스트럿츠 2 아키텍처 (0) | 2012.09.06 | 
| [Struts] 도메인 오브젝트 (0) | 2012.09.03 | 
| [Struts] ActionSupport 클래스를 확장한 액션 (1) | 2012.09.03 | 
| [Struts] 스트럿츠 흐름과 파라미터 값 전달하기 (2) | 2012.09.03 |