1. 程式人生 > >Struts2輸入校驗

Struts2輸入校驗

exp cdata tin .get xwork scheme field -1 rac

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輸入校驗