呆呆向前冲的blog

  BlogJava :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理 ::
  78 随笔 :: 43 文章 :: 5 评论 :: 74 Trackbacks

步骤:

1 环境配置:首先要为struts加入Validator支持
      lib包:加入validator所需的jar包
      配置文件:struts-config.xml里加入

  <plug-in className="org.apache.struts.validator.ValidatorPlugIn">
    
<set-property property="pathnames" value="/WEB-INF/validator-rules.xml,/WEB-INF/validation.xml" />
  
</plug-in>

      这里所需的validator-rules.xml和validation.xml也需要放到/WEB-INF/下

2 配置actionForm
      2.1 修改Form基类:将actionForm继承的基类改为ValidatorForm      
      2.2 配置struts-config.xml文件:把要验证的form的validator属性设为true

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.1//EN" "http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd">
<struts-config>
  
<form-beans>
    
<form-bean name="userForm" type="com.satesoft.test.UserForm" />
  
</form-beans>
  
<action-mappings>
    
<action input="/index.jsp" name="userForm" path="/show" scope="request" type="com.satesoft.test.ShowAction" validate="true">
      
<forward name="forward" path="/show.jsp" />
    
</action>
  
</action-mappings>
  
<message-resources parameter="ApplicationResources" />
  
<plug-in className="org.apache.struts.validator.ValidatorPlugIn">
    
<set-property property="pathnames" value="/WEB-INF/validator-rules.xml,/WEB-INF/validation.xml" />
  
</plug-in>
</struts-config>

      注意:一定要配置对应的Action里的input属性,否则struts找不到要返回的路径    

      2.3  配置validator.xml文件:在validation.xml里加入对要验证的actionform的定义

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE form-validation PUBLIC "-//Apache Software Foundation//DTD Commons Validator Rules Configuration 1.0//EN" "http://jakarta.apache.org/commons/dtds/validator_1_0.dtd">
<form-validation>
  
<formset>
    
<form name="userForm">
      
<field property="name" depends="required">
        
<arg0 key="user.name.required"/>
      
</field>
    
</form>
  
</formset>
</form-validation>

      这里同时要配置的还有消息资源。在资源文件中加入上述配置文件中所需的消息

errors.header=<H3><font color="blue">errors</font><br>
errors.footer=
<p></p>
errors.required={0} is required.
errors.minlength={0} cannot be less than {1} characters.
errors.maxlength={0} cannot be greater than {1} characters.
errors.invalid={0} is invalid.
errors.byte={0} must be a byte.
errors.short={0} must be a short.
errors.integer={0} must be an integer.
errors.long={0} must be a long.
errors.float={0} must be a float.
errors.double={0} must be a double.
errors.date={0} is not a date.
errors.range={0} is not in the range {1} through {2}.
errors.creditcard={0} is not a valid credit card number.
errors.email={0} is an invalid email address
user.name.required=user name

 2.4 配置作为input的jsp页面: 

<%@ page contentType="text/html; charset=GBK" %>
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
<html>
<head>
<title>
index
</title>
</head>
<body bgcolor="#ffffff">
<html:form action="show.do" method="post">
  
<html:text property="name"/>
  
<html:submit title="确定"/>
  
<html:cancel title="取消">
  
</html:cancel>
</html:form>
<html:errors/>

</body>
</html>

      如果需要加入javascript支持,则:

<%@ page contentType="text/html; charset=GBK" %>
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
<html>
<head>
<title>
index
</title>
</head>
<body bgcolor="#ffffff">
<html:form action="show.do" method="post" onsubmit="return validateUserForm(this);">
  
<html:text property="name"/>
  
<html:submit title="确定"/>
  
<html:cancel title="取消">
  
</html:cancel>
</html:form>
<html:javascript formName="userForm"/>
</body>
</html>

      注意:onsubmit里的validatorUserForm要和下面的formName里的名字一致,只不过首字母大写


最后要注意:这只是客户端自动生成Javascript的验证。还要在服务器端进行验证。

posted on 2005-08-27 22:37 呆呆向前冲的blog 阅读(540) 评论(0)  编辑  收藏 所属分类: 工作:Java技术

只有注册用户登录后才能发表评论。


网站导航: