기타 세팅:http://gusfree.tistory.com/947 참조

교재 120page


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>
		
	</package>
</struts>


LoginForm.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="LoginCheck.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>


LoginCheck.java
package service;

import com.opensymphony.xwork2.ActionSupport;

public class LoginCheck extends ActionSupport{
	String userId;
	String userPw;
			
	@Override
	public void validate() {//유효성 검사
		System.out.println("validate");
		if(userId == null || userId.equals("")){
			//addFieldError(키,벨류);
			addFieldError("userId", "아이디를 입력하세요");
			//아이디를 제대로 입력하지 않았다면 필드에러를 만든다.
			//jsp에서 만든 필드 에러를 출력하자 ${fieldError.키값}
		}
		
		if(userPw == null || userPw.equals("")){
			addFieldError("userPw", "비번을 입력하세요");
		}
		
		//super.validate();
	}

	public LoginCheck(){
		System.out.println("LoginCheck 객체 생성");
	}
	
	public String execute(){
		System.out.println("execute");
		return "success";
	}
	
	// 자동으로 호출됩니다 
	public void setUserId(String userId){
		System.out.println("setUserId");
		this.userId=userId;
	}
	public void setUserPw(String userPw) {
		System.out.println("setUserPw");
		this.userPw = userPw;
	}
	public String getUserPw() {
		System.out.println("getUserPw");
		return userPw;
	}
	public String getUserId() {
		System.out.println("getUserId");
		return userId;
	}
}





id와 비번을 체크해서 관리자라면 인사를 하자


LoginCheck.java
package service;

import com.opensymphony.xwork2.ActionSupport;

public class LoginCheck extends ActionSupport{
	String userId;
	String userPw;
	String hello;	
	
	@Override
	public void validate() {//유효성 검사
		System.out.println("validate");
		if(userId == null || userId.equals("")){
			//addFieldError(키,벨류);
			addFieldError("userId", "아이디를 입력하세요");
			//아이디를 제대로 입력하지 않았다면 필드에러를 만든다.
			//jsp에서 만든 필드 에러를 출력하자 ${fieldError.키값}
		}
		
		if(userPw == null || userPw.equals("")){
			addFieldError("userPw", "비번을 입력하세요");
		}
		
		//super.validate();
	}

	public LoginCheck(){
		System.out.println("LoginCheck 객체 생성");
	}
	
	public String execute(){
		System.out.println("execute");
		//id와 비번을 체크해서 관리자라면 인사를 하자
		hello="";
		if(userId.equals("gusfree") && userPw.equals("12345")){
			hello="안녕하세요" +userId+"<img src='https://t1.daumcdn.net/cfile/tistory/125822404F8593850B'>";
		}
		return "success";
	}
	
	
	public String getHello() {//jsp에서 ${hello}로 호출
		return hello;
	}

	// 자동으로 호출됩니다 
	public void setUserId(String userId){
		System.out.println("setUserId");
		this.userId=userId;
	}
	public void setUserPw(String userPw) {
		System.out.println("setUserPw");
		this.userPw = userPw;
	}
	public String getUserPw() {
		System.out.println("getUserPw");
		return userPw;
	}
	public String getUserId() {
		System.out.println("getUserId");
		return userId;
	}
}


LoginSuccess.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/>
	
	객체.getUserId() 메서드가 호출됩니다. <br/>
	user Id : ${userId} / userPw : ${userPw} / <br/>
	${hello}
</body>
</html>



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

3일차 validate()  (0) 2012.06.07
2일차 interface 활용하기  (0) 2012.06.05
2일차 form  (0) 2012.06.05
1일차 interceptor  (0) 2012.06.04
1일차 한글 깨짐 현상  (0) 2012.06.04

+ Recent posts