struts2 校驗成功

最近在看struts2, 今天看了檢驗部分。記錄一下。

1 loginValidate.jsp

<%@ page language="java"
         contentType="text/html; charset=EUC-KR"
         pageEncoding="EUC-KR"
         import="java.util.*" %>
<%@ 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=EUC-KR">
<title>데이타 검증</title>
</head>
<body>
 <s:actionerror />  
 <s:actionmessage />
 <s:form action="LoginValidateAction.action" theme="simple">
 입력내용:<s:textfield name="msg" />
     <s:fielderror>    
       <s:param>msg.hello</s:param>   
     </s:fielderror>
     <br />   
     <s:submit value="login" />   
 </s:form>
</body>
</html>


2 LoginValidateAction.java 在struts2demo包裏面

package struts2demo;

import com.opensymphony.xwork2.ActionSupport;

public class LoginValidateAction extends ActionSupport {
 private String msg; 
 public String execute() {  
  System.out.println(SUCCESS);  
  return SUCCESS; 
 }
 public void validate() {  
  if(!msg.equalsIgnoreCase("hello")){   
   System.out.println(INPUT);   
   this.addFieldError("msg.hello", "must be hello!");   
   this.addActionError("error!");  
  }else{   
   this.addActionMessage("success");  
  }
 } 
  public String getMsg() {  
   return msg; 
  } 
  public void setMsg(String msg) {  
   this.msg = msg; 
  }
}


3 struts.xml 在 src下

<?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="struts2demo" extends="struts-default">
    <action name="LoginValidateAction" class="struts2demo.LoginValidateAction">   
     <result name="success">/loginValidate.jsp</result>   
     <result name="input">/loginValidate.jsp</result>  
    </action> 
   </package>
</struts>

4 web.xml在WEB-INF下

<?xml version="1.0" encoding="UTF-8"?>
 <web-app version="3.1" xmlns="
http://xmlns.jcp.org/xml/ns/javaee"
             xmlns:xsi="
http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="
http://xmlns.jcp.org/xml/ns/javaee                      http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd">
     <session-config>
         <session-timeout>30</session-timeout>
     </session-config>
     <welcome-file-list>
         <welcome-file>index.html</welcome-file>
         <welcome-file>index.htm</welcome-file>
         <welcome-file>index.jsp</welcome-file>
     </welcome-file-list>
    
     <!-- struts2 filter -->
  <filter>
        <filter-name>struts2</filter-name>
        <filter-class>
          org.apache.struts2.dispatcher.FilterDispatcher            
        </filter-class>
     </filter>
     <filter-mapping>
        <filter-name>struts2</filter-name>
        <url-pattern>/*</url-pattern>
     </filter-mapping>

 
 </web-app>

5 當然最基本還要導入包

下面附圖

6 運行loginValidate.jsp


可以了 就是這麼簡單。

大家一起加油!!





發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章