Struts 2 数据验证的两种方式——手动验证和xml验证

2018-09-20  本文已影响0人  周大侠_2409

1. 手动验证——通过编写action进行验证

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<html>
  <body>
    <form action="dataAction.action" method="post">
         name:<input name="name" type="text"><br>
         age:<input name="age" type="text"><br>
         <input type="submit" value="登陆">
    </form>
  </body>
</html>
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
      <!-- 
                    数据验证,手动验证
       -->
      <package name="checke" namespace="/" extends="struts-default">
           <action name="dataAction" class="actions.dataAction" method="execute">
                 <result name="input">/index.jsp</result>
                 <result name="success">/success.jsp</result>
           </action>
      </package>
</struts>    
package actions;

import java.util.regex.Pattern;

import com.opensymphony.xwork2.ActionSupport;
public class dataAction extends ActionSupport {
    private String name;
    private String age;
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getAge() {
        return age;
    }
    public void setAge(String age) {
        this.age = age;
    }
    public void validate(){
        if (name == null || name.equals("")){
            addFieldError("name","姓名不能为空:"+name);
        }
        if (age == null || name.equals("")){
            addFieldError("age","年龄不能为空:"+age);
        }else if(!isInteger(age)){
            addFieldError("age","年龄输入不合理:"+age);
        }else if(Integer.parseInt(age)>100 && Integer.parseInt(age)<1){
            addFieldError("age","请输入年龄在0-100之间:"+age);
        }
        System.out.println("name:"+name+";age:"+age);
    }
    //判断字符串是否为整数
    public static boolean isInteger(String str) {  
            Pattern pattern = Pattern.compile("^[-\\+]?[\\d]*$");  
            return pattern.matcher(str).matches();  
    }
    public String execute(){
        return "success";
    }

}
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib uri="/struts-tags" prefix="s" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'index.jsp' starting page</title>
    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">    
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">
    <!--
    <link rel="stylesheet" type="text/css" href="styles.css">
    -->
  </head>
  
  <body>
        <s:fielderror>
            <s:param>name</s:param>
        </s:fielderror>
        <s:fielderror>
            <s:param>age</s:param>
        </s:fielderror>
  </body>
</html>
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib uri="/struts-tags" prefix="s" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'success.jsp' starting page</title>
    
    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">    
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">
    <!--
    <link rel="stylesheet" type="text/css" href="styles.css">
    -->

  </head>
  
  <body>
        name:<s:property value="name"/><br>
        age:<s:property value="age"/>
  </body>
</html>

2. XML验证

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<html>
  <body>
     <form action="dataAction1.action" method="post">
         name:<input name="name" type="text"><br>
         age:<input name="age" type="text"><br>
         <input type="submit" value="登陆">
    </form>
  </body>
</html>
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
      <!-- 
                    数据验证,xml验证
       -->
      <package name="checke" namespace="/" extends="struts-default">
           <action name="dataAction1" class="actions.dataAction1" method="execute">
                 <result name="input">/index.jsp</result>
                 <result name="success">/success.jsp</result>
     
           </action>
      </package>
</struts>    
package actions;

import com.opensymphony.xwork2.ActionSupport;

public class dataAction1 extends ActionSupport {
    private String name;
    private String age;
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getAge() {
        return age;
    }
    public void setAge(String age) {
        this.age = age;
    }
    public String execute(){
        return "success";
    }
}
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
      <package name="checke" namespace="/" extends="struts-default">
           <action name="ActionloginUser" class="actions.dataAction" method="User">
                 <result name="input">/index.jsp</result>
                 <result name="success">/success.jsp</result>
           </action>
          <action name="Actionloginadmin" class="actions.dataAction" method="admin">
                 <result name="input">/index.jsp</result>
                 <result name="success">/success.jsp</result>
           </action>
      </package>
</struts>    
  1. 想要验证整个Action对象,xml为: dataAction-validation.xml
  2. 只是验证action里面的admin方法,xml为:dataAction-Actionloginadmin-validation.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE validators PUBLIC "-//Apache Struts//XWork Validator 1.0.3//EN" 
"http://struts.apache.org/dtds/xwork-validator-1.0.3.dtd">
<validators>
    <field name="name">
        <field-validator type="requiredstring">
            <message>
                姓名不能为空
            </message>
        </field-validator>
    </field>
    <field name="age">
        <field-validator type="requiredstring">
            <message>
                年龄不能为空
            </message>
        </field-validator>
    </field>
</validators>
上一篇下一篇

猜你喜欢

热点阅读