码迷,mamicode.com
首页 > 其他好文 > 详细

Struts2输入校验

时间:2017-08-25 21:03:28      阅读:270      评论:0      收藏:0      [点我收藏+]

标签:submit   校验   private   software   ror   exp   erro   res   ase   

regist.jsp

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<%
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>
    <form action="userAction_regist" method="post"> 
        用户名:<input type="text" name="username" value="${username }"/><span style="color:red"><s:property value="fieldErrors[‘username‘][0]"/></span><br/>
        密码:<input type="text" name="pwd" value="${pwd }"/><span style="color:red"><s:property value="fieldErrors[‘pwd‘][0]"/></span><br/>
        <input type="submit" value="注册"/>
    </form>
  </body>
</html>

2、UserAction.java

package com.cn.action;

import com.opensymphony.xwork2.ActionSupport;


public class UserAction extends ActionSupport {

    private String username;
    private String pwd;
    
    public String login(){
        System.out.println("login ... ");
        return "welcome";
    }
    
    public String regist(){
        System.out.println("regist...");
        return "welcome";
    }
    //validateLogin表示要验证login方法
    public void validateRegist() {
        
        boolean u = username.matches("\\w{2,12}");
        boolean p = pwd.matches("\\w{6,12}");
        
        if(!u){
            this.addFieldError("username", "用户名必须在2-12位之间");
        }
        
        if(!p){
            this.addFieldError("pwd", "密码必须在6-12位之间");
        }

    }
    
    public String getUsername() {
        return username;
    }
    public void setUsername(String username) {
        this.username = username;
    }
    public String getPwd() {
        return pwd;
    }
    public void setPwd(String pwd) {
        this.pwd = pwd;
    }
    
}

3、重写validateXxx或者RegistUserAction-registUserAction_regist-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="username"><!--用户名-->
        <field-validator type="requiredstring"><!--字符串必填校验器-->
               <message>账号不能为空</message>
        </field-validator>
        <field-validator type="stringlength"><!--字符串长度校验器-->
               <param name="minLength">2</param>
               <param name="maxLength">12</param>
               <message>账号长度必须在${minLength}至${maxLength}之间</message>
        </field-validator>
        <field-validator type="regex"><!--正则表达式校验器-->
              <param name="regexExpression"> 
                     <![CDATA[(^[^ ]{1,}$)]]>
              </param>
               <message>账号中不允许出现空格</message>
        </field-validator>
    </field>
    <field name="pwd"><!--pwd属性-->
        <field-validator type="requiredstring"><!--字符串必填校验器
               --><message>密码不能为空</message>
        </field-validator>
        <field-validator type="stringlength"><!--字符串长度校验器-->
               <param name="minLength">6</param>
               <param name="maxLength">12</param>
               <message>密码长度必须在${minLength}至${maxLength}之间</message>
        </field-validator>
        <field-validator type="regex"><!--正则表达式校验器-->
              <param name="regexExpression"> 
                     <![CDATA[(^[^ ]{1,}$)]]>
              </param>
               <message>密码中不允许出现空格</message>
        </field-validator>
    </field>
    
</validators>

4、struts.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
    "http://struts.apache.org/dtds/struts-2.3.dtd">

<struts>

    <package name="test" namespace="/" extends="struts-default">

        <action name="userAction_*" class="com.cn.action.UserAction" method="{1}">
            <result name="welcome">/welcome.jsp</result>
            <result name="input">/index.jsp</result>
        </action>
        
        <action name="registUserAction_*" class="com.cn.action.RegistUserAction" method="{1}">
            <result name="ok">/welcome.jsp</result>
            <result name="input">/registUser.jsp</result>
        </action>
    </package>

</struts>

Struts2输入校验

标签:submit   校验   private   software   ror   exp   erro   res   ase   

原文地址:http://www.cnblogs.com/Vito-Yan/p/7429590.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!