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="hi" extends="struts-default" 
		namespace="/"	>
		<action name="LoginForm"   >
			<result>/login/LoginForm.jsp</result>
		</action>	
		<!--  LoginCheck.action?userId=xxx&userPw=yyy -->
		<action name="LoginCheck"  class="service.LoginCheck">
			<result name="input">/login/LoginForm.jsp</result>
			<result>/login/LoginSuccess.jsp</result>
		</action>
		
		<action name="LoginCheck2"  class="service.LoginCheck2" method="login">
			<result name="input">/login/LoginForm2.jsp</result>
			<result>/login/LoginSuccess2.jsp</result>
		</action>
	</package>
</struts>


index.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>Index Page</title>
</head>
<body>
	인덱스 페이지
	
	<jsp:forward page="/login/LoginForm2.jsp"></jsp:forward>
</body>
</html> 


LoginForm2.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> LoginForm.jsp  </title>
</head>
<body>
	LoginForm.jsp <br />
	<form action="LoginCheck2.action" method="get">
		userId : <input type="text" name="user.userId"> ${fieldErrors.userId} <br/>
		userPw : <input type="text" name="user.userPw"> ${fieldErrors.userPw} <br/>
		<input type="submit" value="보내기" >
	</form>
	
</body>
</html>


LoginSuccess2.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>LoginSuccess</title>
</head>
<body>
	LoginSuccess.jsp  <br/>
	
	객체.getUser.getId() 메서드가 호출됩니다. <br/>
	user Id : ${user.userId} / userPw : ${user.userPw} / <br/>
	${hello} <br/>
	${a.b.c.d.e} == getA().getB().getC().getD().getE();
</body>
</html>


LoginCheck.java
package service;

import model.User;

import com.opensymphony.xwork2.ActionSupport;

public class LoginCheck2 extends ActionSupport{
	User user;
	
	public String login(){
		System.out.println("LoginCheck2 - login");
		//db에 있는 사용자 정보를 체크하자
		new CheckUserService().check(user);
		
		//체크성공했다
		return "success";
	}

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}
}


User.java
package model;

//한명의 회원정보를 저장하는 클래스 - 필드, 생성자,set,get
public class User {
	String userId;
	String userPw;
	
	public User(){
		System.out.println("기본 생성자 User");
		
	}
	
	public User(String userId, String userPw) {
		super();
		this.userId = userId;
		this.userPw = userPw;
	}

	public String getUserId() {
		System.out.println("getUserId");
		return userId;
	}

	public void setUserId(String userId) {
		System.out.println("setUserId");
		this.userId = userId;
	}

	public String getUserPw() {
		System.out.println("getUserPw");
		return userPw;
	}

	public void setUserPw(String userPw) {
		System.out.println("setUserPw");
		this.userPw = userPw;
	}
	
	
}


CheckUserService.java
package service;

import model.User;

//db를 조회해서 id와 pw가 맞는지를 확인하는 클래스
public class CheckUserService {
	
	public void check(User user){
		//db 조회 했다.
		System.out.println("CheckUserServive - check 성공");
	}
}





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="hi" extends="struts-default" 
		namespace="/"	>
		<action name="LoginForm"   >
			<result>/login/LoginForm.jsp</result>
		</action>	
		<!--  LoginCheck.action?userId=xxx&userPw=yyy -->
		<action name="LoginCheck"  class="service.LoginCheck">
			<result name="input">/login/LoginForm.jsp</result>
			<result>/login/LoginSuccess.jsp</result>
		</action>
		
		<action name="LoginCheck2"  class="service.LoginCheck2" method="login">
			<result name="input">/login/LoginForm2.jsp</result>
			<result>/login/LoginSuccess2.jsp</result>
		</action>
		
		<action name="LoginCheck3"  class="service.LoginCheck3">
			<result name="input">/login/LoginForm3.jsp</result>
			<result>/login/LoginSuccess3.jsp</result>
		</action>
	</package>
</struts>


index.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>Index Page</title>
</head>
<body>
	인덱스 페이지
	
	<jsp:forward page="/login/LoginForm3.jsp"></jsp:forward>
</body>
</html> 


LoginForm3.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> LoginForm.jsp  </title>
</head>
<body>
	LoginForm.jsp <br />
	<form action="LoginCheck3.action" method="get">
		userId : <input type="text" name="userId"> ${fieldErrors.userId} <br/>
		userPw : <input type="text" name="userPw"> ${fieldErrors.userPw} <br/>
		<input type="submit" value="보내기" >
	</form>
	
</body>
</html>


LoginCheck3.java
package service;


import model.User;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.opensymphony.xwork2.Preparable;

public class LoginCheck3 extends ActionSupport 
					implements ModelDriven,Preparable{
	User user;
	
	//2번째
	public Object getModel() {
		System.out.println("getModel");
		return user;
	}
	
	//1번째 prepare는 execute직전에  호출되는 메서드로서 
	//execute를 하기 위한 준비작업 기능을 넣는다.
	public void prepare() throws Exception {
		System.out.println("prepare");
		user=new User();
	}
	public String execute(){
		return "success";
	}
	
	//3번째 jsp에서 실행됨
	public User getUser() {
		return user;
	}
}


LoginSuccess3.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>LoginSuccess3</title>
</head>
<body>
	LoginSuccess3.jsp  <br/>
	
	객체.getUser.getId() 메서드가 호출됩니다. <br/>
	user Id : ${user.userId} / userPw : ${user.userPw} / <br/>
	${hello} <br/>
	${a.b.c.d.e} == getA().getB().getC().getD().getE();
</body>
</html>




소스

03.form.zip




index.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>Index Page</title>
</head>
<body>
	인덱스 페이지
	
	<jsp:forward page="/select.jsp"></jsp:forward>
</body>
</html> 


select.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>Insert title here</title>
</head>
<body>
	<ul>
		<li>
			<a href="First.action">   첫번째 예제 </a>
		</li> 
		<li>
			<a href="Second.action">   두번째 예제 </a>
		</li>
		<li>
			<a href="Third.action">   세번째 예제 </a>
		</li>
	</ul>
</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="hi" extends="struts-default" 
		namespace="/"	>
		<action name="LoginForm"   >
			<result>/login/LoginForm.jsp</result>
		</action>	
		<!--  LoginCheck.action?userId=xxx&userPw=yyy -->
		<action name="LoginCheck"  class="service.LoginCheck">
			<result name="input">/login/LoginForm.jsp</result>
			<result>/login/LoginSuccess.jsp</result>
		</action>
		
		<action name="LoginCheck2"  class="service.LoginCheck2" method="login">
			<result name="input">/login/LoginForm2.jsp</result>
			<result>/login/LoginSuccess2.jsp</result>
		</action>
		
		<action name="LoginCheck3"  class="service.LoginCheck3">
			<result name="input">/login/LoginForm3.jsp</result>
			<result>/login/LoginSuccess3.jsp</result>
		</action>
		
		<action name="First">
		    <result type="dispatcher">/login/LoginForm.jsp</result>
		</action>
		<action name="Second">
		    <result type="dispatcher">/login/LoginForm2.jsp</result>
		</action>
		<action name="Third">
		    <result type="redirect">/login/LoginForm3.jsp</result>
		</action>
	
	</package>
</struts>





03.form.zip


index.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>Index Page</title>
</head>
<body>
	인덱스 페이지
	
	<jsp:forward page="/select.jsp"></jsp:forward>
</body>
</html> 


select.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>Insert title here</title>
</head>
<body>
	<ul>
		<li>
			<a href="First.action">   첫번째 예제 </a>
		</li> 
		<li>
			<a href="Second.action">   두번째 예제 </a>
		</li>
		<li>
			<a href="Third.action">   세번째 예제 </a>
		</li>
		<li>
			<a href="Hello.action">   Hello </a>
		</li>
		<li>
			<a href="Hello2.action">   Hello2 </a>
		</li>
	</ul>
</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="hi" extends="struts-default" 
		namespace="/"	>
		<action name="LoginForm"   >
			<result>/login/LoginForm.jsp</result>
		</action>	
		<action name="LoginForm2"   >
			<result>/login/LoginForm2.jsp</result>
		</action>
		
		<!--  LoginCheck.action?userId=xxx&userPw=yyy -->
		<action name="LoginCheck"  class="service.LoginCheck">
			<result name="input">/login/LoginForm.jsp</result>
			<result>/login/LoginSuccess.jsp</result>
		</action>
		
		<action name="LoginCheck2"  class="service.LoginCheck2" 
				method="login">
			<result name="input">/login/LoginForm2.jsp</result>
			<result>/login/LoginSuccess2.jsp</result>
		</action>
		
		<!--  Hello.action 요청이 오면
			  service.Hello.class 객체생성후 execute 실행
			  return success/fail 결과에 따라 다른 액션 실행
		-->
		<action name="Hello" class="service.Hello">
			<result name="success" type="redirectAction">
				<param name="actionName">LoginForm</param>
			</result>
			<result name="fail" type="redirectAction">
				<param name="actionName">LoginForm2</param>
			</result>
		</action>
		
		<!-- Hello2.action요청이 오면 LoginCheck.action을 실행해
			 LoginCheck.action?userId=gusfree&userPw=12345
			 처럼 파라미터가 붙어서 실행이 된다
		 -->
		<action name="Hello2">
			<result type="redirectAction">
				<param name="actionName">LoginCheck</param>
				<param name="userId">gusfree</param>
				<param name="userPw">12345</param>
			</result>
		</action>
		
		<action name="LoginCheck3"  class="service.LoginCheck3" >
			<result name="input">/login/LoginForm3.jsp</result>
			<result>/login/LoginSuccess3.jsp</result>
		</action>
		
		<action name="First">
			<result type="dispatcher">/login/LoginForm.jsp</result>
		</action>
		<action name="Second">
			<result type="dispatcher">/login/LoginForm2.jsp</result>
		</action>
		<action name="Third">
			<result type="redirect">/login/LoginForm3.jsp</result>
		</action>
	</package>
</struts>


Hello.java
package service;

import java.util.Random;

public class Hello {
	public String execute(){
		System.out.println("Hello - execute");
		if(new Random().nextInt(100)<50){
			return "fail";
		}
		return "success";
	}
}



'Struts2 > 2012.04월 강좌(MySQL)' 카테고리의 다른 글

3일차 Struts Standard Tag Library  (0) 2012.06.08
3일차 validate()  (0) 2012.06.07
2일차 ActionSupport 클래스를 확장한 액션  (0) 2012.06.05
2일차 form  (0) 2012.06.05
1일차 interceptor  (0) 2012.06.04

+ Recent posts