输入校验(1)——struts2第三讲

输入校验 表单提交

注:本文系作者在看了浪曦的风中叶老师的struts2视频的个人总结,希望能帮助广大struts2的初学者。 

第一步: 
(这一步和其他一样,这里从简)依旧是新建一个web project,命名为shuruxiaoayn,导入struts2必须的包。在src目录下新建struts.xml,修改web.xml文件。 

第二步: 
将index.jsp改名为regt.jsp(这个不是必须的,事实上也没有必要,此处只是为了便于称呼)。reg.jap的代码如下 

Jsp代码  收藏代码

  1. <%@ page language="java" import="java.util.*" pageEncoding="GB18030"%>  

  2. <%  

  3. String path = request.getContextPath();  

  4. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";  

  5. %>  

  6. <%@ taglib prefix="s"  uri="/struts-tags"%>  

  7. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">  

  8. <html>  

  9.   <head>  

  10.     <base href="<%=basePath%>">  

  11.       

  12.     <title>My JSP 'index.jsp' starting page</title>  

  13.     <meta http-equiv="pragma" content="no-cache">  

  14.     <meta http-equiv="cache-control" content="no-cache">  

  15.     <meta http-equiv="expires" content="0">      

  16.     <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">  

  17.     <meta http-equiv="description" content="This is my page">  

  18.     <!--  

  19.     <link rel="stylesheet" type="text/css" href="styles.css">  

  20.     -->  

  21.   </head>  

  22.     

  23.   <body>  

  24.             <s:form action="reg" >  

  25.                     <s:textfield  name="username" label="username"></s:textfield>  

  26.                     <s:password name="password" label="password"></s:password>  

  27.                     <s:password name="repassword" label="repassword"></s:password>  

  28.                     <s:textfield name="age" label="age"></s:textfield>  

  29.                     <s:textfield name="birthday" label="birthday"></s:textfield>  

  30.                     <s:textfield name="graduation" label="graduation"></s:textfield>  

  31.                       

  32.                     <s:submit name="submit"></s:submit>  

  33.                     <s:reset name="reset"></s:reset>  

  34.             </s:form>  

  35.   

  36.   </body>  

  37. </html>  


第二步:action 
在src目录下新建新建包com.action 在其中新建一个RegAction.java文件 
代码如下 

Java代码  收藏代码

  1. package com.action;  

  2.   

  3. import java.util.Calendar;  

  4. import java.util.Date;  

  5.   

  6. import com.opensymphony.xwork2.ActionSupport;  

  7.   

  8. public class RegAction  extends ActionSupport  

  9.   

  10. {  

  11.         private String username;  

  12.         private String password;  

  13.         private String repassword;  

  14.         private int age;  

  15.         private Date birthday;  

  16.         private Date graduation;  

  17.           

  18.           

  19.         public String getUsername() {  

  20.             return username;  

  21.         }  

  22.         public void setUsername(String username) {  

  23.             this.username = username;  

  24.         }  

  25.         public String getPassword() {  

  26.             return password;  

  27.         }  

  28.         public void setPassword(String password) {  

  29.             this.password = password;  

  30.         }  

  31.         public String getRepassword() {  

  32.             return repassword;  

  33.         }  

  34.         public void setRepassword(String repassword) {  

  35.             this.repassword = repassword;  

  36.         }  

  37.         public int getAge() {  

  38.             return age;  

  39.         }  

  40.         public void setAge(int age) {  

  41.             this.age = age;  

  42.         }  

  43.         public Date getBirthday() {  

  44.             return birthday;  

  45.         }  

  46.         public void setBirthday(Date birthday) {  

  47.             this.birthday = birthday;  

  48.         }  

  49.         public Date getGraduation() {  

  50.             return graduation;  

  51.         }  

  52.         public void setGraduation(Date graduation) {  

  53.             this.graduation = graduation;  

  54.         }  

  55.           

  56.         public String execute() throws Exception  

  57.         {  

  58.             return  SUCCESS;  

  59.         }  

  60.           

  61.         public void validate()  

  62.         {  

  63.             System.out.println("已经调用了效验方法");  

  64.             if(null == username || username.length() <6 || username.length() >10)  

  65.             {  

  66.                 this.addFieldError("username","username needs 6 to 10 chars");  

  67.             }  

  68.             if(null == password || password.length() <6 || password.length() >10)  

  69.             {  

  70.                 this.addFieldError("password""password needs 6 to 10 chars");  

  71.             }  

  72.             if(null == repassword || repassword.length() <6 || repassword.length() >10)  

  73.             {  

  74.                 this.addFieldError("repassword""repassword needs 6 to 10 chars");  

  75.             }  

  76.             ifnull != password || null !=repassword)  

  77.             {  

  78.                  if(  !password.equals(repassword) )  

  79.                  {  

  80.                      this.addFieldError("repassword""two pasword should be same");  

  81.                  }  

  82.             }  

  83.               

  84.             if(age <=0 || age >150)  

  85.             {  

  86.                 this.addFieldError("age""age should between 1 to 149");  

  87.             }  

  88.             if(null == birthday )  

  89.             {  

  90.                 this.addFieldError("birthday""birthday required");  

  91.             }  

  92.             if(null == graduation)  

  93.             {  

  94.                 this.addFieldError("graduation""graduation required");  

  95.             }  

  96.             if(null != birthday && null != graduation)  

  97.             {  

  98.                   Calendar c1 =Calendar.getInstance();  

  99.                    c1.setTime(birthday);  

  100.                      

  101.                    Calendar c2 = Calendar.getInstance();  

  102.                    c2.setTime(graduation);  

  103.                      

  104.                    if( !c1.before(c2))  

  105.                    {  

  106.                        this.addFieldError("birthday""birthday should be after the graduation");  

  107.                    }  

  108.                          

  109.             }  

  110.               

  111.               

  112.         }  

  113.           

  114.           

  115.           

  116.           

  117.           

  118. }  




第四步: 
在WebRoot目录下新建另一个视图 success.jsp (此文件在这个效验中基本没有什么作用) 

第五步:修改struts.xml文件 代码惹下 

Xml代码  收藏代码

  1. <?xml version="1.0" encoding="utf-8" ?>  

  2. <!DOCTYPE struts PUBLIC  

  3.     "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"   

  4.     "struts.apache.org/dtds/struts-2.0.dtd">  

  5. <struts>  

  6.         <package name="shuruxiaoyan" extends="struts-default">  

  7.                 <action name="reg" class="com.action.RegAction">  

  8.                         <result name="success">/success.jsp</result>  

  9.                         <result name="input">/reg.jsp</result>  

  10.                 </action>  

  11.         </package>  

  12.   

  13. </struts>  



ok,到此,已经完成了最最基本的输入校验工作; 
运行界面会是如下: 
图1 

13211632_DaoY.bmp 



图2 

13211632_TBBM.bmp 


显然。上面中图2中有些信息不是我们制定的,这是由于输入的数据没有通过类型转换造成的; 
当然,如果想让出错提示变得更友好些,在进行如下操作: 

第六步:配置属性文件 
在对应的action目录中新建一个RegAction.properties文件 过程如下 

1.新建这个文件 
2.打开jdk安装目录下的native2ascii应用程序 路径一般类似 作者的目录是 
O:\Program Files\Java\jdk1.6.0_10\bin\native2ascii 
在这个环境中,输入以下三行 分别按回车 

invalid.fieldvalue.age=年龄格式有问题或类型转化错误 
invalid.fieldvalue.birthday=出生日期格式有问题或类型转换错误 
invalid.fieldvalue.graduation=毕业日期格式有问题或类型转换错误 

会出现三行对应的Unicode代码 放入刚刚建立的属性文件即可: 
附图 

13211632_xyd3.bmp 


13211633_tuz7.bmp




好了 现在是弄好了较简单的输入校验了 
预览一下: 
当填写如下表单时: 


13211633_Ozqw.bmp 

完成后 

13211633_uSAI.bmp 

struts2的类型转换——Struts2第二讲 | Struts2输入校验2(框架效验)———stru ...


转载于:https://my.oschina.net/u/1014520/blog/279628

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值