-
手动验证(以注册为例)
步骤:
- 编写action类继承ActionSupport然后重写父类的validate方法
- 在struts.xml配置好Action和验证不通过(返回input)的跳转页面
- 在jsp页面引入
<%@taglib uri="/struts-tags" prefix="s"%> |
并且在相应的输入框附近添加Actionerror
<s:actionerror/> |
注意:这里如果Action类中使用的是addActionError才能使用<s:actionerror/>
如果使用的是addFieldError则要使用<s:fielderror/>
代码:“
Action类
public class RegisterAction extends ActionSupport { private String name; private String pwd; private int age;
public String execute(){
return "success"; }
//重写validate方法,validate会在执行execute之前执行,不通过则不会执行execute public void validate() { System.out.println("validate"); if(age>110||age<1) { //添加验证不通过的提示信息 this.addActionError("年龄范围为1~110");
} super.validate(); } //get set略 |
Struts.xml配置
<package name="registerdemo" namespace="/" extends="struts-default"> <action name="register" class="com.action.RegisterAction"> <result name="success">/register.jsp</result> <!-- 验证不通过则会返回input --> <result name="input">/index.jsp</result> </action> </package> |
Index.jsp
<!--验证不通过时返回到此页面时显示validate方法里的写好错误信息 --> <s:actionerror/> <form action="register.action" method="post"> 用户名: <input type="text" name="name"><br> 密码:<input type="password" name="pwd"><br> 年龄:<input type="text"name="age"><br> <input type="submit"> </form> |
测试:
提交
这里跳转回来了并且显示了错误信息
注意:如果打开index.jsp页面时后出现
系统错误:HTTP Status 500
异常信息:The Struts dispatcher cannot be found. This is usually caused by using Struts tagswithout the associated filter. Struts tags are only usable when the request haspassed through its servlet filter, which initializes the Struts dispatcherneeded for this tag. - [unknown location] |
提示<s:actionerror/>标签错误的时候原因时是<filter-mapping>标签,该标签下的“<url-pattern>”标签指明了监听对象的url,此标签的值为“*.action”,意思是指定监听器监听后缀为.action的文件,即Struts 2框架下的action文件。
此时问题已经很明了了。由于监听器只监听action文件,故jsp文件中的Struts标签无法被系统所识别,所以产生500错误。
参考:https://blog.csdn.net/u010287342/article/details/38061459
解决方法在web.xml添加如下
<filter-mapping> <filter-name>struts2</filter-name> <url-pattern>*.jsp</url-pattern> </filter-mapping> |
或者将原来的*.action改成/*
-
验证框架
- 使用验证框架的步骤
- 在需要验证的Action类的包下添加一个新的xml文件 文件名格式为:ActionClassName-validation.xml其中ActionClassName为需要验证的action的类名
如下所选的2个文件:
- Action类需要继承ActionSupport.
以上面的注册为例
Struts.xml
<package name="registerdemo" namespace="/" extends="struts-default"> <action name="register" class="com.action.Registervalidate"> <result name="success">/register.jsp</result> <result name="input">/index.jsp</result> </action> </package> |
Registervalidate.java
//使用验证框架 public class Registervalidate extends ActionSupport { private String name; private String pwd; private int age;
public String execute(){ System.out.println("execute"); return "success"; }
Get set略 |
验证规则Registervalidate-validation.xml
<!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"> <!-- 去空格 --> <param name="trim">true</param> <message>用户名不能为空</message> </field-validator> <!-- 限制输入字符长度 --> <field-validator type="stringlength"> <param name="maxLength">8</param> <param name="minLength">2</param> <message>name需要在2~8个字符长度内</message> </field-validator> </field> <field name="age"> <!-- 限制int的最大值与最小值 --> <field-validator type="int"> <param name="max">120</param> <param name="min">1</param> <message>年龄范围为1~120</message> </field-validator> </field> </validators> |
具体的param的参数可以在下面对应的class中找到
另外doctype可以在在 下的xwork-validator-1.0.3.dtd文件中找到。
验证注册器的类型:
/com/opensymphony/xwork2/validator/validators/default.xml下可以找到
<validators> <validator name="required" class="com.opensymphony.xwork2.validator.validators.RequiredFieldValidator"/> <validator name="requiredstring" class="com.opensymphony.xwork2.validator.validators.RequiredStringValidator"/> <validator name="int" class="com.opensymphony.xwork2.validator.validators.IntRangeFieldValidator"/> <validator name="long" class="com.opensymphony.xwork2.validator.validators.LongRangeFieldValidator"/> <validator name="short" class="com.opensymphony.xwork2.validator.validators.ShortRangeFieldValidator"/> <validator name="double" class="com.opensymphony.xwork2.validator.validators.DoubleRangeFieldValidator"/> <validator name="date" class="com.opensymphony.xwork2.validator.validators.DateRangeFieldValidator"/> <validator name="expression" class="com.opensymphony.xwork2.validator.validators.ExpressionValidator"/> <validator name="fieldexpression" class="com.opensymphony.xwork2.validator.validators.FieldExpressionValidator"/> <validator name="email" class="com.opensymphony.xwork2.validator.validators.EmailValidator"/> <validator name="url" class="com.opensymphony.xwork2.validator.validators.URLValidator"/> <validator name="visitor" class="com.opensymphony.xwork2.validator.validators.VisitorFieldValidator"/> <validator name="conversion" class="com.opensymphony.xwork2.validator.validators.ConversionErrorFieldValidator"/> <validator name="stringlength" class="com.opensymphony.xwork2.validator.validators.StringLengthFieldValidator"/> <validator name="regex" class="com.opensymphony.xwork2.validator.validators.RegexFieldValidator"/> <validator name="conditionalvisitor" class="com.opensymphony.xwork2.validator.validators.ConditionalVisitorFieldValidator"/> </validators> |
Jsp页面
<form action="register.action" method="post"> <s:fielderror></s:fielderror> 用户名: <input type="text" name="name"><br> 密码:<input type="password" name="pwd"><br> 年龄:<input type="text"name="age"><br> <input type="submit"> </form> |
测试:
- 提交空白
- 提交1个字符的用户名
- 提交不再1~120范围的年龄
附常用的验证规则
常用的验证规则: <field name="username"> <field-validator type="fieldexpression"> <!--指定逻辑表达式 -->
<field-validator type="stringlength">
|
参考:https://www.cnblogs.com/focusj/archive/2010/11/15/2057674.html