在Struts如何使用Validate()方法

Steps to using validate() in ActionForm:
1.rewrite the method validate() in ActionForm file;
2.in resource file ApplicationResources.properties create error marks;
3.in struts-config.xml, set validate property of <action> to TRUE, and set <message-resources> element to indicate where the resource file locates;
4.in related jsp files, add <html:errors>.

Example:
1.
----------------------------------------------------
LoginActionForm.java
------------------------

...
public  ActionErrors validate(ActionMapping mapping, HttpServletRequest request) {
  ActionErrors errors 
=   new  ActionErrors();
  
//  Validate an attribute named "xxx"
   if ( getUsername()  ==   null   ||  getUsername().length()  <   3  ){
   errors.add(
" name " , new  ActionMessage( " error.username.required " ,username));
  }
  
if ( getPassword()  ==   null   ||  getPassword().length()  <   3  ){
   errors.add(
" pwd " , new  ActionMessage( " error.password.required " ,password));
  }
  
return  errors;
 }
...

----------------------------------------------------

2.
----------------------------------------------------
ApplicationResources.properties
---------------------------------

#Username error
error.username.required
= <li>Please input your username again!</li>
#Password error
error.password.required
= <li>Please input your password again!</li>

----------------------------------------------------

3.
----------------------------------------------------
struts-config.xml
-----------------

<? xml version="1.0" encoding="ISO-8859-1" ?>
<!--
     This is a blank Struts configuration file with an example
     welcome action/page and other commented sample elements.

     Tiles and the Struts Validator are configured using the factory defaults
     and are ready-to-use.

     NOTE: If you have a generator tool to create the corresponding Java classes
     for you, you could include the details in the "form-bean" declarations.
     Otherwise, you would only define the "form-bean" element itself, with the
     corresponding "name" and "type" attributes, as shown here.
-->
< struts-config >
  
< form-beans >
    
< form-bean  name ="loginActionForm"  type ="login.loginActionForm" />
  
</ form-beans >
  
< global-exceptions />
  
< global-forwards >
    
< forward  name ="welcome"  path ="/Welcome.do" />
  
</ global-forwards >
  
< action-mappings >
    
< action  forward ="/pages/Welcome.jsp"  path ="/Welcome" />
    
< action  input ="/login/login.jsp"  name ="loginActionForm"  path ="/loginAction"  scope ="request"  type ="login.loginAction"  validate ="true" >
     
< forward  name ="Success"  path ="/login/main.jsp"   />
     
< forward  name ="Fail"  path ="/login/register.jsp"   />
    
</ action >
  
</ action-mappings >
  
< controller  processorClass ="org.apache.struts.tiles.TilesRequestProcessor" />
  
< plug-in  className ="org.apache.struts.tiles.TilesPlugin" >
    
< set-property  property ="definitions-config"  value ="/WEB-INF/tiles-defs.xml" />
    
< set-property  property ="moduleAware"  value ="true" />
  
</ plug-in >
  
< plug-in  className ="org.apache.struts.validator.ValidatorPlugIn" >
    
< set-property  property ="pathnames"  value ="/WEB-INF/validator-rules.xml,/WEB-INF/validation.xml" />
  
</ plug-in >
  
< message-resources  parameter ="ApplicationResources" ></ message-resources >
</ struts-config >

----------------------------------------------------

4.
----------------------------------------------------
login.jsp
----------

...
html:form action="loginAction" method="post">
  Username: 
< html:text  property ="username"  size ="15"   />
 
< font  color ="red" >< html:errors  property ="name"   /></ font >
 
< br />
 
< br />
  Password: 
< html:password  property ="password"  size ="15"   />
 
< font  color ="red" >< html:errors  property ="pwd"   /></ font >
 
< br  />
 
< br  />
 
< html:submit  property ="submit"  value ="Login"   />
</ html:form >
...

---------------------------------------------------- 

评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值