第一个Struts验证框架搭建详细功略

开发环境:
    MyEclipse6.0 + Tomcat6.0 
 
首先说Struts中验证框架的使用
1,建一个Web Project用Struts包围,这个就不多说了,注意最好用struts1.2及以上版本
2,手动配置FormBean和Action
A,手动配置FormBean
    在此之前新建一个Jsp页面,如下:
    index.jsp 

Code
<!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> --> 1 <%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
 2 <%@ taglib uri="strutsHtml" prefix="html" %>
 3 <%@ taglib uri="strutsBean" prefix="bean" %>
 4 <%@ taglib uri="strutsLogic" prefix="logic" %>
 5 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
 6 <html>
 7  <head> 
 8    <title>Demo</title>
 9  </head>
10  
11  <body>
12     <logic:messagesPresent>
13        <ul>
14        <html:messages id="error">
15           <li><bean:write name="error"/></li>
16        </html:messages>
17        </ul><hr />
18     </logic:messagesPresent>
19   <center>
20     <form action="check.do" method="post" >
21        UserID:<input type="text" name="userID" />
22        Password:<input type="password" name="userpass" />
23        <input type="submit" name="cmd" value="login" />
24     </form>
25     </center>
26  </body>
27 </html>

在这里使用了struts中的自定义标签,就不介绍了,在web.xml中加入以下代码即可:(加在servlet元素上边比较好)

Code
<!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> --> 1<jsp-config>
 2  <taglib>
 3       <taglib-uri>strutsHtml</taglib-uri>
 4       <taglib-location>/WEB-INF/struts-html.tld</taglib-location>
 5  </taglib>
 6  <taglib>
 7       <taglib-uri>strutsBean</taglib-uri>
 8       <taglib-location>/WEB-INF/struts-bean.tld</taglib-location>
 9  </taglib>
10  <taglib>
11       <taglib-uri>strutsLogic</taglib-uri>
12       <taglib-location>/WEB-INF/struts-logic.tld</taglib-location>
13  </taglib>
14 </jsp-config>

然后在my包下新建一个类,如下:
        注意:要使用验证框架必须让类继承ValidatorForm
在FormBean中只需声明跟他相关联的JSP页面里<form>中的属性,和属性的get/set方法即可,本例中只需声明index.jsp中的userID,userpass。注意,属姓名一定要与jsp中的名字相同。代码如下:

Code
<!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> --> 1package my;
 2 
 3import org.apache.struts.validator.ValidatorForm;
 4 
 5@SuppressWarnings("serial")
 6publicclass TestForm extends ValidatorForm {
 7 
 8    private String userID;
 9    private String userpass;
10   
11    public String getUserID() {
12       returnuserID;
13    }

14   
15    publicvoid setUserID(String userID) {
16       this.userID = userID;
17    }

18   
19    public String getUserpass() {
20       returnuserpass;
21    }

22   
23    publicvoid setUserpass(String userpass) {
24       this.userpass = userpass;
25    }

26}

在struts-config.xml中添加<form-bean>,如下:
注:type的值要与建立的FormBean的名字相同,而且必须标明类所在的包,在此我就不把FormBean和Action分包了,都放在my包下。

<!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> --> < form - beans >
  
< form - bean name = " testForm "  type = " my.TestForm " ></ form - bean >
 
</ form - beans >

B.配置Action
    同样,在my包下新建一个类:名叫TestAction 如下:
    注意,此类继承自Action
在此类中实现execute方法。暂时把返回值设置成null
    代码如下:

Code
<!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> -->package my;
 
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
 
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
 
publicclass TestAction 
extends Action {
 
    
public ActionForward execute(ActionMapping mapping, ActionForm form,
           HttpServletRequest request, HttpServletResponse response)
           
throws Exception {
       
return mapping.findForward("ok");
    }
 
}

在struts-config.xml中配置Action:
    其中path是jsp表单中action的值,name,attribute关联的是FormBean的名字, type是Action完整的类名,scope为作用域,validate在这里要设置成true,因为要做验证,input为当验证出发时,显示错误的页面
<forward>中 name为Action转发的名称,path为转发的路径,此时我们要把Action中刚才返回null的地方替换成:return mapping.findForward("ok");让Action跳转到ok.jsp
 

Code
<!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> --><action-mappings>
  
<action path="/check" name="testForm" attribute="testForm"
          type
="my.TestAction" scope="request" validate="true"
          input
="/index.jsp">
       
<forward name="ok" path="/ok.jsp"></forward>
  
</action>
 
</action-mappings>

1,开始添加验证框架。
验证框架其实就是一个struts得插件,它依赖于validator-rules.xml和validation.xml以及项目本身的资源文件(ApplicationResources.properties)
      
       所以我们第一步先要在struts-config.xml中添加这个插件:
           validator-rules.xml的这段话:

<!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> --> 1 < plug - in className = " org.apache.struts.validator.ValidatorPlugIn " >
2          < set - property property = " pathnames "  value = " /WEB-INF/validator-rules.xml, /WEB-INF/validation.xml " />
3 </ plug - in >

Copystruts-config.xml中。即可完成添加,注意,插件要复制到文件的最后,当然要包含在根元素下.
       Values里边有两个值,说明它需要这两个文件来完成验证。
validator-rules.xml我们已经有了下面我们需要新建一个validation.xml
WEB-INF下新建一个xml文件。
建好之后把validator-rules.xml这段话:

Code
<!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> --><!DOCTYPE form-validation PUBLIC
          
"-//Apache Software Foundation//DTD Commons Validator Rules Configuration 1.1.3//EN"
          
"http://jakarta.apache.org/commons/dtds/validator_1_1_3.dtd">

copyvalidation.xml中。
注:validator_1_1_3.dtd validator-rules.xmlvalidation.xml文件约束文件。约束在xml文件中都有哪些元素和属性及它们之间的包含关系
Ok暂时先不管它了我们关注下ApplicationResources.properties 这个文件
它是整个项目的资源文件,说简单了就是起一个让你可以一劳永逸的作用,我们这里不多做解释了
Ok,我们在validator-rules.xml中找到被注释的部分中如以下标记:

Code
<!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> --> 1 # Struts Validator Error Messages
 2    errors.required={0} is required.
 3    errors.minlength={0} can not be less than {1} characters.
 4    errors.maxlength={0} can not be greater than {1} characters.
 5    errors.invalid={0} is invalid.
 6  
 7    errors.byte={0} must be a byte.
 8    errors.short={0} must be a short.
 9    errors.integer={0} must be an integer.
10    errors.long={0} must be a long.
11    errors.float={0} must be a float.
12    errors.double={0} must be a double.
13  
14    errors.date={0} is not a date.
15    errors.range={0} is not in the range {1} through {2}.
16    errors.creditcard={0} is an invalid credit card number.
17    errors.email={0} is an invalid e-mail address.

这些是用来保存验证框架所提示的错误信息的,将他们copyApplicationResources.properties
关于错误信息的注释:
1, 验证非空
2, 最小长度
3, 最大长度
4, 挂钩正则表达式的验证,(mask)
5, 验证为byte类型
6, 验证为short类型
7, 验证为integer类型
8, 验证为long类型
9, 验证为float类型
10,              验证为时间类型
11,              验证在xx-xx范围之内
12,              验证creditcard格式
13,              验证email格式

注意,ApplicationResources文件要选择打开方法的,用文本编辑器或者properties file editor 打开都可以。
 
 
好了,这里搞定,我们跳回到validation.xml文件,开始手写validation.xml
(程序员么,要有跳跃性思维……)
 
    1.写根元素 <form-validation>
    2.写 <formset> : 包含所有要被验证框架进行验证的表单
    3.写 <form name="testForm" > : name属性对应的值一定要是struts-config.xmlform-bean中声明
4.写 <field property="user" depends="required,mask" > property一定要是form中的属性
         property="user" 属性名
         depends="required,mask" :套用的验证标准这个验证标准必须是在validator-rules.xmlvalidator元素下声明的,套用两个验证标准,就用逗号将两个标准隔开。
       5.写需要传入提示信息的参数
           写法一:<arg key="user.name" position="0" />
           这里key属性里面的”user.name”需要在ApplicationResources文件里边定义一下。
         举个例子:            我们要验证表单中用户ID这个输入框不能为空。  第一步要在validation.xml关联,设定参数。如下:

Code
<!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> --><form-validation>
    
<formset>
       
<form name="testForm">
           
<field property="userID" depends="required">
              
<arg key="user.id" position="0" />
           
</field>
       
</form>
    
</formset>
</form-validation>

第二步要在ApplicationResources文件中定义一个参数:

user.id = User’s ID    这里需要注意,变量名要与validation.xml下的key值相同,User’s ID是要显示在页面上的内容。

发布,运行后的效果:

ApplicationResources文件中显示中文很麻烦,就不在这里介绍了。
 
           再举个例子,假如我们要验证密码不能为空且长度不能小于6位:
           我们所涉及的表单项为userpass,验证标准为minlengthrequired
errors. minlength={0} can not be greater than {1} characters.
正如我们所见,这个验证标准需要两个参数。Ok,我们分两种方法来做一下:
1, ApplicationResources中定义参数1

<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--> < field propert
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值