Struts2表单基本验证

Step 1:

创建输入页:index.jsp

 
< html >
< head >
    
< title > Validation - Basic </ title >
    
< s:head />
</ head >

< body >

< b > What is your favorite color? </ b >
< p />

< s:form  method ="post" >
    
< s:textfield  label ="Name"  name ="name" />
    
< s:textfield  label ="Age"  name ="age" />
    
< s:textfield  label ="password"  name ="password" />
    
< s:submit />
</ s:form >
</ body >
</ html >
 
 

<% @ page language = " java "  pageEncoding = " GB18030 " %>
<% @taglib prefix = " s "  uri = " /struts-tags " %>
< html >
< head >   
< title > validation test </ title >
</ head >
< body >     
Your name is :
< s:property value = " name " />      < br  />   
  Your age is :
< s:property value = " age " />      < br  />    
 Your age is :
< s:property value = " password "   />    </ body >
</ html >

Step3:
 
  
 
package  com;
import  com.opensymphony.xwork2.ActionSupport; /** */ /** * Action类 * @author zdw * */ public   class  QuizAction  extends  ActionSupport private static final long serialVersionUID = 3124124L;
 
private String name; private int age; private String password;
 
public String getPassword() {  return password; }
 
public void setPassword(String password) {  this.password = password; }
 
public String getName() {  return name; }
 
public void setName(String name) {  this.name = name; }
 
public int getAge() {  return age; }
 
public void setAge(int age) {  this.age = age; }
 @Override 
public String execute() throws Exception {  return SUCCESS; }
}

Step4:
 
 
<? xml version="1.0" encoding="UTF-8"  ?> <! DOCTYPE struts PUBLIC    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"    "http://struts.apache.org/dtds/struts-2.0.dtd" >
< struts >  
< package  name ="com"  extends ="struts-default" >   
< action  name ="validation"  class ="com.QuizAction" >   
<!--  input & result不能少   -->    
< result  name ="input" > /index.jsp </ result >   
 
< result  name ="success" > /success.jsp </ result >  
 
</ action >
  </ package >
</
struts >   
 
 
 
 
<? xml version="1.0" encoding="UTF-8" ?>
< web-app  version ="2.4"  xmlns ="http://java.sun.com/xml/ns/j2ee"  xmlns:xsi ="http://www.w3.org/2001/XMLSchema-instance"  xsi:schemaLocation ="http://java.sun.com/xml/ns/j2ee  http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" >  
< filter >  
 
< filter-name > struts2 </ filter-name >  
 
< filter-class >    org.apache.struts2.dispatcher.FilterDispatcher  </ filter-class >
 
</ filter >  
< filter-mapping >  
 
< filter-name > struts2 </ filter-name >  
 
< url-pattern > /* </ url-pattern >  
</ filter-mapping >  
< welcome-file-list >   
< welcome-file > index.jsp </ welcome-file >  
</ welcome-file-list >
</
web-app >

 
 
 
 
 
<? xml version="1.0" encoding="UTF-8" ?> <! DOCTYPE validators PUBLIC     "-//OpenSymphony Group//XWork Validator 1.0.2//EN"     "http://www.opensymphony.com/xwork/xwork-validator-1.0.2.dtd" >
<!--  此xml的DTD声明可以到xwork包下的xwork-validator-1.0.2.dtd里面复制  -->
< validators >
 
<!--  对应Action类里面的属性  -->  
< field  name ="name" >  
 
< field-validator  type ="requiredstring" >  
  
< message > name is required </ message >  
 
</ field-validator >
 
</ field >  
< field  name ="age" >  
 
< field-validator  type ="int" >   
 
< param  name ="min" > 2 </ param >   
 
< param  name ="max" > 10 </ param >   
 
< message > between 2 and 10  </ message >   
</ field-validator >   </ field >
 
<!-- Plain validator Syntax -->  
 
<!--  <field name="password"> 
 <field-validator type="stringlength"> 
  <param name="minLength">4</param>   
<param name="maxLength">10</param> 
  <param name="trim">true</param>   
<message>require between 4 and 10</message>  
</field-validator> </field>  
-->  
<!--  Field validator Syntax  -->   
< validator  type ="stringlength" >    
< param  name ="fieldName" > password </ param >    
< param  name ="minLength" > 4 </ param >   
 
< param  name ="maxLength" > 10 </ param >    
< param  name ="trim" > true </ param >    
< message > require between 4 and 10 </ message >  
 
</ validator >
</ validators >

 
162769.html 


々上善若水々 2007-11-24 07:42 发表评论
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值