프로젝트 생성시 버전을 2.5로 해줘야함..
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"> <display-name>04.vaildate</display-name> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>index.jsp</welcome-file> <welcome-file>default.html</welcome-file> <welcome-file>default.htm</welcome-file> <welcome-file>default.jsp</welcome-file> </welcome-file-list> <filter> <filter-name>struts2</filter-name> <filter-class> org.apache.struts2.dispatcher.FilterDispatcher </filter-class> <init-param> <description>한글인코딩하기, FilterDispatcher실행중에 struts.i18n.encoding의 값을 찾아서 셋팅합니다 </description> <param-name>struts.i18n.encoding</param-name> <param-value>UTF-8</param-value> </init-param> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> <!-- 사이트.com/abc.jsp --> </filter-mapping> </web-app>
<?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="/" > <!-- 지정된 패키지 내에서 .. 결과가 input 이면 --> <global-results> <result name="input">/LoginForm.jsp</result> <result name="error">/error.jsp </result> </global-results> <action name="LoginForm" > <result>LoginForm.jsp</result> </action> <action name="LoginProcess" class="Service.LoginProcess"> <result name="success">LoginSuccess.jsp</result> </action> <action name="LoginProcess2" class="Service.LoginProcess2"> <result name="input">/LoginForm2.jsp</result> <result name="success">LoginSuccess2.jsp</result> </action> <!-- 맨 아래에 있어야 합니다. 디폴트 와일드카드 맵핑 --> <action name="*"> <result>{1}.jsp</result> </action> </package> </struts>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib prefix="s" uri="/struts-tags" %> <!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> 로그인폼.jsp<br/> 패키지명:Service, 클래스명:LoginProcess.java</br> <form action="LoginProcess.action"> id:<input type="text" name="id">${fieldErrors.id}</br> pw:<input type="text" name="pw"></br> email:<input type="text" name="email"></br> <input type="submit" value="보내기"></br> </form> </body> </html>
package Service; import com.opensymphony.xwork2.ActionSupport; public class LoginProcess extends ActionSupport { String id , pw, email; @Override public void validate() { //setter 메서드 다음에 호출된다 if(id==null || id.length()<2){ addFieldError("id", "아이디를 2자 이상 입력하세요"); // return "input";이 됩니다. } super.validate(); } public String execute(){ if(id.equals("error")){ return "error"; } return "success"; } public String getId() { return id; } public void setId(String id) { this.id = id; } public String getPw() { return pw; } public void setPw(String pw) { this.pw = pw; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } }
<%@ 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>error</title> </head> <body> 에러 페이지 </body> </html>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib prefix="s" uri="/struts-tags" %> <!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>LoginForm2.jsp </title> </head> <body> 로그인폼2.jsp <br /> <s:property value="message"/><br/> s:form 태그를 사용하면 필드에러(~~.java실행할 때 validate()메서드에서 addFiedError());) 를 내가 직접 호출할 필요가 없습니다.<br/> <s:form action="LoginProcess2.action"> <!-- 실행될 ~.java 의 필드 String id= %{id} ; --> <s:textfield label="아이디" name="id" value="%{id}" /><br/> <s:password label="비밀번호" name="pw" value="%{pw}" /> <br/> <s:textfield label="이메일" name="email" value="%{email}" /> <br/> <s:submit /> </s:form> </body> </html>
package Service; import DAO.DBHelper; import com.opensymphony.xwork2.ActionSupport; public class LoginProcess2 extends ActionSupport { String id , pw, email; String message; @Override public void validate() { //setter 메서드 다음에 호출된다 if(id==null || id.length()<2){ addFieldError("id", "아이디를 2자 이상 입력하세요"); // return "input";이 됩니다. message="다시 입력하세요"; } super.validate(); } public String getMessage() { return message; } public String execute(){ if(id.equals("error")){ return "error"; } //db에서 id와 비번을 체크하는 기능을 넣었다고 가정 Boolean check=new DBHelper().check(id, pw); if(check){ message="로그인에 성공했습니다."; } else message="처음 방문했습니다."; return "success"; } public String getId() { return id; } public void setId(String id) { this.id = id; } public String getPw() { return pw; } public void setPw(String pw) { this.pw = pw; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } }
package DAO; public class DBHelper { String myId="abc"; String myPw="123"; public DBHelper(){ } public boolean check(String id,String pw){ if(id.equals(myId) && pw.equals(myPw)){ System.out.println("db조회 결과 계정 맞습니다."); return true; } return false; } }
'Struts2 > 2012.04월 강좌(MySQL)' 카테고리의 다른 글
4일차 chain result type (0) | 2012.06.08 |
---|---|
3일차 Struts Standard Tag Library (0) | 2012.06.08 |
2일차 interface 활용하기 (0) | 2012.06.05 |
2일차 ActionSupport 클래스를 확장한 액션 (0) | 2012.06.05 |
2일차 form (0) | 2012.06.05 |