Notice
Recent Posts
Recent Comments
Link
| 일 | 월 | 화 | 수 | 목 | 금 | 토 |
|---|---|---|---|---|---|---|
| 1 | ||||||
| 2 | 3 | 4 | 5 | 6 | 7 | 8 |
| 9 | 10 | 11 | 12 | 13 | 14 | 15 |
| 16 | 17 | 18 | 19 | 20 | 21 | 22 |
| 23 | 24 | 25 | 26 | 27 | 28 | 29 |
| 30 |
Tags
- 에러페이지
- paint
- 안드로이드
- Java
- 국제화
- JSP
- 메소드
- 전화걸기
- 기본
- mybatis
- AWT
- oracle
- JavaScript
- 오버로딩
- Menu
- Graphic
- 배열
- Eclips
- 이클립스
- OGNL
- Spring
- struts2
- HTML
- layout
- 메서드
- 예외처리
- 생성자
- 어노테이션
- 클래스
- Android
Archives
- Today
- Total
note
struts2 커스텀 인터페이스 본문
실행시 콘솔창
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 실행
'JSP > Struts2' 카테고리의 다른 글
| struts2 Redirect (0) | 2012.02.22 |
|---|---|
| struts2 chain (0) | 2012.02.22 |
| struts2 커스텀 인터셉터 (0) | 2012.02.21 |
| struts2 도메인 오브젝트 (0) | 2012.02.21 |
| Struts2 ActionSupport를 사용하여 유효성 검사(Interceptor) (1) | 2012.02.21 |