上一篇struts2之输入校验介绍了手动完成输入校验,也即依靠重写validate方法和validateXxx方法,指定请求某个方法时对传入的参数进行校验。
本篇介绍基于配置的字段校验。下面是登录的常见校验:
1、在src目录下创建action类ValidateUserAction
package com.action; import com.opensymphony.xwork2.ActionSupport; public class ValidateUserAction extends ActionSupport { private String loginid; private String loginpwd; private String msg; //登录 public String login(){ msg = "登录成功!"; return "success"; } public String getLoginid() { return loginid; } public void setLoginid(String loginid) { this.loginid = loginid; } public String getLoginpwd() { return loginpwd; } public void setLoginpwd(String loginpwd) { this.loginpwd = loginpwd; } public String getMsg() { return msg; } public void setMsg(String msg) { this.msg = msg; } }
2、在src目录下新建struts.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "struts-2.1.dtd" > <struts> <constant name="struts.enable.DynamicMethodInvocation" value="true"></constant> <package name="hello" namespace="/" extends="struts-default"> <action name="validateUser_*" method="{1}" class="com.action.ValidateUserAction"> <result name="success">/index.jsp</result> <result name="input">/login.jsp</result> </action> </package> </struts>
3、在WebContent下新建登录页面login.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <%@ taglib prefix="s" uri="/struts-tags"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <link rel="stylesheet" type="text/css" href="css/list.css"> <style> .bg { width: 658px; height: 220px; background: url(images/login.png); padding-top: 20px; } </style> <title>登录</title> </head> <body> <div style="font-size:25;font-family:微软雅黑"> <b>请输入个人信息</b> </div> <form action="validateUser_login.action" method="post"> <div class="bg"> <center> <p> 账号:<input type="text" name="loginid"> <span style="color:red; font-size:12px;">${fieldErrors[‘loginid‘][0]}</span> </p> <p> 密码:<input type="text" name="loginpwd"> <span style="color:red; font-size:12px;">${fieldErrors[‘loginpwd‘][0] }</span> </p> <input type="image" src="images/sure.jpg"> </center> </div> </form> <body> </html>
4、在action同级目录下新建ValidateUserAction-validateUser_login-validation.xml配置文件。
文件命名规则:被校验Action的类名-struts.xml中配置的该action的name属性-validate.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="loginid"><!--loginid属性--> <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> <field name="loginpwd"><!--loginpwd属性--> <field-validator type="requiredstring"><!--字符串必填校验器--> <message>密码不能为空</message> </field-validator> <field-validator type="stringlength"><!--字符串长度校验器--> <param name="minLength">6</param> <param name="maxLength">15</param> <message>密码长度必须在${minLength}至${maxLength}之间</message> </field-validator> <field-validator type="regex"><!--正则表达式校验器--> <param name="regexExpression"> <![CDATA[(^[^ ]{1,}$)]]> </param> <message>密码中不允许出现空格</message> </field-validator> </field> </validators>
以下的项目目录结构图