struts-ch5.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="ch5" namespace="/ch5" extends="struts-default">
		<interceptors>
			<interceptor name="timer" class="com.ch5.interceptor.TimerInterceptor" />
			<interceptor name="userDao" class="com.ch5.interceptor.UserDaoInterceptor" />
		</interceptors>
		
		<!-- timer 인터셉터 활용 -->
		<action name="helloWorld" class="com.ch4.action.HelloWorld">
			<interceptor-ref name="timer" />
			<interceptor-ref name="logger" />
			<result name="success">/ch4/hello.jsp</result>
		</action>
		
		<!-- UserDaoInterceptor 활용 -->
		<action name="createUser3" class="com.ch5.action.CreateUser3">
			<interceptor-ref name="prepare" />
			<interceptor-ref name="modelDriven" />
			<interceptor-ref name="params" />
			<interceptor-ref name="userDao" />
			<result name="success">createSuccess.jsp</result>
		</action>
	</package>	
</struts>

com.ch5.interceptor/UserDaoAware

package com.ch5.interceptor;

import com.ch4.dao.UserDao;

public interface UserDaoAware {
	public void setUserDao(UserDao userDao);
}

com.ch5.interceptor/UserDaoInterceptor

package com.ch5.interceptor;

import com.ch4.dao.UserDao;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.Interceptor;

public class UserDaoInterceptor implements Interceptor{

	UserDao userDao;
	int cnt;

	@Override
	public void init() {
		userDao = new UserDao();
		System.out.println("cnt = "+(++cnt));
	}
	@Override
	public String intercept(ActionInvocation invocation) throws Exception {
		Object action = invocation.getAction();
		if(action instanceof UserDaoAware){
			UserDaoAware userDaoAware = (UserDaoAware) action;
			userDaoAware.setUserDao(userDao);
		}
		return invocation.invoke();
	}

	@Override
	public void destroy() {
		userDao = null;
	}
}

com.ch5.action/CreateUser3

package com.ch5.action; import com.ch4.dao.UserDao; import com.ch4.domain.User; import com.ch5.interceptor.UserDaoAware; import com.opensymphony.xwork2.Action; import com.opensymphony.xwork2.ModelDriven; import com.opensymphony.xwork2.Preparable; public class CreateUser3 implements Action,Preparable,ModelDriven,UserDaoAware{ User user; UserDao userDao; @Override public String execute() throws Exception { userDao.createUser(user); return SUCCESS; } @Override public void setUserDao(UserDao userDao) { this.userDao = userDao ; } @Override public Object getModel() { return user; } @Override public void prepare() throws Exception { user = new User(); } }

ch5/createSuccess.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>
사용자를 추가했습니다.<br/>
${user}
</body>
</html>

ch5/userForm3.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>
<form action="createUser3.action" method="post">
	이름 : <input type="text" name="name"><br/>
	나이 : <input type="text" name="age"><br/>
	이메일 : <input type="text" name="email"><br/>
	<input type="submit" value="전송">
</form>
</body>
</html>

userDao -> init()메소드 동작이 되면서 intercept() 시작되면서 getAction을 받아오면서 CreateUser3.java를 불러온다

UserDaoAware로 if문으로 캐스팅 가능한지를 보고 불가능하면 빠져나간다


setUserDao가 있는게 확실하면 userDao를 셋팅 


다 완료시 execute() 실행한뒤 return SUCCESS를 하여 result부분에 진입 한뒤 jsp 실행 



실행시 콘솔창









+ Recent posts