1. 程式人生 > >Struts2 Validation 驗證

Struts2 Validation 驗證

一個Struts2簡單驗證例項,eclipse。

其他檔案見上篇簡單登入實現。就改了下Action和login.jsp然後加了個validation.xml罷遼。

結構

Action中以Action方法驗證User登入

package com.struts2;
import java.util.regex.Pattern;

import com.opensymphony.xwork2.ActionSupport;

public class LoginAction extends ActionSupport{

	private String account;
	private String passWord;

	   public String getPassWord() {
	    return passWord;
	}
	   
	   public void setPassWord(String passWord) {
	    this.passWord = passWord;
	}
	   
	   public void setAccount(String account) {
		      this.account = account;
		   }
	   
	   public String getAccount() {
		      return account;
		   }

	  
	public String Administrator() {
		System.out.println("Administrator登入資訊");
		System.out.println("賬戶:"+account);
		System.out.println("密碼:"+passWord);
		return "adminsuccess";
	   }
	public String User() {
		System.out.println("User登入資訊");
		System.out.println("賬戶:"+account);
		System.out.println("密碼:"+passWord);
		return "usersuccess";

	   }
	


		//只對user方法有效的validate
		public void validateUser() {
			if (account==null||"".equals(account.trim()) ){
				
				this.addFieldError("account", "姓名不能為空");
			}
			if (passWord==null||"".equals(passWord.trim())){
				this.addFieldError("passWord", "密碼不能為空");
			} else if(!Pattern.matches("^\\w+$", passWord)){
				this.addFieldError("passWord", "密碼必須由數字、字母或下劃線組成");
			}
			// TODO Auto-generated method stub
			super.validate();
		}
	}

Struts同時更改

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
   "-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
   "http://struts.apache.org/dtds/struts-2.5.dtd">
<struts>
<constant name="struts.devMode" value="true" />
<constant name="struts.enable.DynamicMethodInvocation" value="false" />
 <!-- 使用!方法時需要開啟DMI功能 -->
 <!-- <constant name="struts.enable.DynamicMethodInvocation" value="true" /> --> 

   <package name="struts2" extends="struts-default">

      <global-allowed-methods>regex:.*</global-allowed-methods>
      
<!-- 這個{1}代表的是前面的name屬性中的第一個* -->    
      <action name="Login*" class="com.struts2.LoginAction"  method="{1}">
            <result name="usersuccess">/UserWelcome.jsp</result>
            <result name="adminsuccess">/AdministratorWelcome.jsp</result>
            <result name="input">/Login.jsp</result>
      </action>
   </package>
</struts>

單獨增加validation檔案以驗證Administrator登入方法

LoginAction-LoginAdministrator-validation.xml,命名方式:類名-Action名-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="account">
		<field-validator type="requiredstring">
			<param name="trim">true</param>
			<message>使用者名稱不能為空</message>
		</field-validator>
	</field>
		
	<field name="passWord">
		<field-validator type="requiredstring">
			<param name="trim">true</param>
			<message>密碼不能為空</message>
		</field-validator>
		
		<field-validator type="regex">
		<!-- <![CDATA[]]>這個標記所包含的內容將表示為純文字 -->
			<param name="regex"> <![CDATA[^\w+$]]> </param>
			<message>密碼格式錯誤,僅由數字、字母及下劃線組成</message>
		</field-validator>
		
		<field-validator type="stringlength">
			<param name="minLength">4</param>
			<message>密碼長度在4位及以上</message>
		</field-validator>
	</field>	
</validators>

最後login.jsp

加入如下標籤顯示驗證結果

<s:fielderror/>

就行了,OJBK。

如果驗證檔案沒反應,可能是開頭dtd的版本號不匹配。