struts2数据校验

本篇博客用 execute()方法,validate()方法,ValidateXxx三种来说明一下stryts2数据校验

我们来看一下jsp页面(以注册为例)

 1 <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
 2 <%
 3 String path = request.getContextPath();
 4 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
 5 %>
 6 <%@ taglib uri="/struts-tags" prefix="s" %>
 7 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
 8 <html>
 9   <head>
10     <base href="<%=basePath%>">
11     
12     <title>My JSP 'regist.jsp' starting page</title>
13    
14 
15   </head>
16   
17   <body>
18    <!-- 获取错误信息 -->
19     <s:fielderror></s:fielderror>
20     <s:form action="execute" method="post">
21     <s:textfield name="user.username" label="用户名"></s:textfield>
22     <br/>
23     <s:textfield name="user.userpwd" label="密码"></s:textfield>
24     <br/>
25     <s:textfield name="okpwd" label="确认密码"></s:textfield>
26     <br/>
27     <s:submit value="注册"></s:submit>
28     </s:form>
29   </body>
30 </html>
regist.jsp
 1 <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
 2 <%
 3 String path = request.getContextPath();
 4 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
 5 %>
 6 
 7 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
 8 <html>
 9   <head>
10     <base href="<%=basePath%>">
11     
12     <title>My JSP 'success.jsp' starting page</title>
13 
14   </head>
15   
16   <body>
17   登录成功 <br/>
18   欢迎你${user.username}<br/>
19  等待数据加载>>>
20   </body>
21 </html>
success.jsp

用到的实体类

 1 package cn.entity;
 2 
 3 public class User {
 4     private String username;
 5     private String userpwd;
 6     public String getUsername() {
 7         return username;
 8     }
 9     public String getUserpwd() {
10         return userpwd;
11     }
12     public void setUsername(String username) {
13         this.username = username;
14     }
15     public void setUserpwd(String userpwd) {
16         this.userpwd = userpwd;
17     }
18 }
User.java

下面是三种实现struts2数据校验的方法:

execute()方法

 1 package cn.action;
 2 
 3 import cn.entity.User;
 4 
 5 import com.opensymphony.xwork2.ActionSupport;
 6 
 7 public class ExecuteAction extends ActionSupport{
 8     private User user;
 9     private String okpwd;
10     
11     @Override
12     public String execute() throws Exception{
13         if(user.getUsername().length()==0){
14             addFieldError("username", "用户名不能为空");
15         }
16         if(user.getUserpwd().length()==0){
17             addFieldError("userpwd", "密码不能为空");
18         }
19         if(!user.getUserpwd().equals(okpwd)){
20             addFieldError("userpwd", "两次密码必须一致");
21         }
22         //捕获错误信息   返回input
23         if(this.hasFieldErrors()){
24             return INPUT;
25         }
26         return SUCCESS;
27     }
28     public User getUser() {
29         return user;
30     }
31     public String getOkpwd() {
32         return okpwd;
33     }
34     public void setUser(User user) {
35         this.user = user;
36     }
37     public void setOkpwd(String okpwd) {
38         this.okpwd = okpwd;
39     }
40 }
ExecuteAction.java

validate()方法

package cn.action;

import cn.entity.User;

import com.opensymphony.xwork2.ActionSupport;

public class ValidateAction extends ActionSupport{
    private User user;
    private String okpwd;
    
    @Override
    public String execute() throws Exception{
        return SUCCESS;
    }
    @Override
    public void validate(){
        if(user.getUsername().length()==0){
            addFieldError("username", "用户名不能为空");
        }
        if(user.getUserpwd().length()==0){
            addFieldError("userpwd", "密码不能为空");
        }
        if(!user.getUserpwd().equals(okpwd)){
            addFieldError("userpwd", "两次密码必须一致");
        }
    }
    public User getUser() {
        return user;
    }
    public String getOkpwd() {
        return okpwd;
    }
    public void setUser(User user) {
        this.user = user;
    }
    public void setOkpwd(String okpwd) {
        this.okpwd = okpwd;
    }
}
ValidateAction.java

ValidateXxx()方法

 1 package cn.action;
 2 
 3 import cn.entity.User;
 4 
 5 import com.opensymphony.xwork2.ActionSupport;
 6 
 7 public class ValidateXxxAction extends ActionSupport{
 8 
 9     private User user;
10     private String okpwd;
11     
12     public void validateRegist(){
13         if(user.getUsername().length()<6){
14             addFieldError("username", "用户长度不能小于6位");
15         }
16     }
17     public String regist(){
18         
19         return SUCCESS;
20     }
21     
22     @Override
23     public void validate(){
24         if(user.getUsername().length()==0){
25             addFieldError("username", "用户名不能为空");
26         }
27         if(user.getUserpwd().length()==0){
28             addFieldError("userpwd", "密码不能为空");
29         }
30         if(!user.getUserpwd().equals(okpwd)){
31             addFieldError("userpwd", "两次密码必须一致");
32         }
33         
34     }
35     public User getUser() {
36         return user;
37     }
38     public String getOkpwd() {
39         return okpwd;
40     }
41     public void setUser(User user) {
42         this.user = user;
43     }
44     public void setOkpwd(String okpwd) {
45         this.okpwd = okpwd;
46     }
47 }
ValidateXxxAction

接下来是编写配置文件

 1 <?xml version="1.0" encoding="UTF-8" ?>
 2 <!DOCTYPE struts PUBLIC
 3     "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
 4     "http://struts.apache.org/dtds/struts-2.3.dtd">
 5 <struts>
 6     <constant name="struts.devMode" value="true"></constant>
 7     <package name="default" namespace="/" extends="struts-default" >
 8     <!-- 测试validate方法 -->
 9         <action name="execute" class="cn.action.ExecuteAction">
10             <result name="success">/success.jsp</result>
11             <result name="input">/regist.jsp</result>
12         </action>
13         <!-- 测试Validate方法 -->
14         <action name="Validate" class="cn.action.ValidateAction">
15             <result name="success">/success.jsp</result>
16             <result name="input">/regist.jsp</result>
17         </action>
18         <!-- validateXx方法 -->
19         <action name="validateXxx" class="cn.action.ValidateXxxAction" method="regist">
20             <result name="success">/success.jsp</result>
21             <result name="input">/regist.jsp</result>
22         </action>
23     
24     
25     </package>
26 </struts>
struts.xml

用到的架包

效果展示:

execute()

当文本未输入值

已输入值

validate()方法

此结果与execute()方法结果相同

ValidateXxx()方法

未输入值

已输入值

 

 

posted @ 2016-10-12 10:35  白兴强  阅读(156)  评论(0编辑  收藏  举报