首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 软件管理 > 软件架构设计 >

Struts2(五):Struts2的输入校验

2012-08-26 
Struts2(5):Struts2的输入校验1,Register.jsp,一个注册页面,包括用户名,密码,确认密码,年龄,生日,毕业时间

Struts2(5):Struts2的输入校验

1,Register.jsp,一个注册页面,包括用户名,密码,确认密码,年龄,生日,毕业时间这六个信息。

<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%><%@ taglib prefix="s" uri="/struts-tags"%><html>  <body>  <table align="center" width="40%">  <tr>  <td style="color:red"><s:fielderror></s:fielderror></td>  </tr>  </table>  <s:form action="register" theme="simple">  <table align="center" width="40%">    <tr>    <td>username:</td>    <td><s:textfield name="username"></s:textfield></td>    </tr>    <tr>    <td>password:</td>    <td><s:password name="password"></s:password></td>    </tr>    <tr>    <td>re-password:</td>    <td><s:password name="repassword"></s:password></td>    </tr>    <tr>    <td>age:</td>    <td><s:textfield name="age"></s:textfield></td>    </tr>    <tr>    <td>birthday:</td>    <td><s:textfield name="birthday"></s:textfield></td>    </tr>    <tr>    <td>graduation:</td>    <td><s:textfield name="graduation"></s:textfield></td>    </tr>    <tr>    <td><s:submit></s:submit></td>    <td><s:reset></s:reset></td>    </tr>    </table>  </s:form>  </body></html>

?

2,RegisterAction.java,在validate方法中进行输入校验

package com.test.action;import java.sql.Date;import java.util.Calendar;import com.opensymphony.xwork2.ActionSupport;public class RegisterAction extends ActionSupport {private String username;private String password;private String repassword;private int age;private Date birthday;private Date graduation;public String getUsername() {return username;}public void setUsername(String username) {this.username = username;}public String getPassword() {return password;}public void setPassword(String password) {this.password = password;}public String getRepassword() {return repassword;}public void setRepassword(String repassword) {this.repassword = repassword;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}public Date getBirthday() {return birthday;}public void setBirthday(Date birthday) {this.birthday = birthday;}public Date getGraduation() {return graduation;}public void setGraduation(Date graduation) {this.graduation = graduation;}public void validate() {//年龄不能小于0if(age < 0){ this.addFieldError("age", "your age is error");}//用户名不能为空if(null == username || username.equals("")){ this.addFieldError("username", "please input your username");}//密码不能为空,且两次输入的密码必须相同if(null == password || null == repassword || !password.equals(repassword)){this.addFieldError("password", "your password is invalid");}//生日与毕业时间不能为空,且毕业时间不能早于生日if(null != birthday && null != graduation){Calendar c1 =Calendar.getInstance();c1.setTime(birthday);Calendar c2 =Calendar.getInstance();c2.setTime(graduation);if(c1.after(c2)){this.addFieldError("birthday", "birthday should be before graduation");}}}}

?3,struts.xml配置

<action name="register" name="code"><%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%><%@ taglib prefix="s" uri="/struts-tags" %><html>  <body>    <table align="center" width="40%">    <tr>    <td>username:</td>    <td>${requestScope.username}</td>    </tr>    <tr>    <td>password:</td>    <td>${requestScope.password}</td>    </tr>    <tr>    <td>age:</td>    <td>${requestScope.age}</td>    </tr>    <tr>    <td>birthday:</td>    <td>${requestScope.birthday} </td>    </tr>    <tr>    <td>graduation:</td>    <td>${requestScope.graduation} </td>    </tr>    <tr>    <td><input type="submit" name="submit"> </td>    <td><input type="reset" name="reset"> </td>    </tr>    </table>  </body></html>

?

热点排行