数据校验DataValiation

struts.xml
<?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>

<constant name="struts.enable.DynamicMethodInvocation" value="false" />
<constant name="struts.devMode" value="false" />
<!--
<include file="example.xml"/>


<package name="default" namespace="/" extends="struts-default">
<default-action-ref name="index" />
<action name="index">
<result type="redirectAction">
<param name="actionName">HelloWorld</param>
<param name="namespace">/example</param>
</result>
</action>
</package>
-->
<!-- Add packages here -->
<package name="default" namespace="/" extends="struts-default">
<action name="register" class="com.demo.struts2.action.RegisterAction">
<result name="success">
/result.jsp
</result>
<result name="input">
/register.jsp
</result>
</action>
</package>
</struts>


RegisterAction.java
package com.demo.struts2.action;
import java.util.Calendar;
import java.util.Date;
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 graduate;
@Override
public String execute() throws Exception {
// TODO Auto-generated method stub
return SUCCESS;
}
@Override
public void validate() {
// TODO Auto-generated method stub
if(username==null || username.length()<4 || username.length()>6)
{
this.addActionError("username invalid");
this.addFieldError("username","username invalid in field");
}
if(password==null || password.length()<4 || password.length()>6)
{
this.addActionError("password invalid");
this.addFieldError("password","password invalid in field");
}
else if(repassword==null || repassword.length()<4 || repassword.length()>6)
{
this.addActionError("repassword invalid");
this.addFieldError("repassword","password invalid in field");
}
else if(!password.equals(repassword))
{
this.addActionError("the passwords not the same");
}
if(age<10 || age>50)
{
this.addActionError("age invalid");
}
if(birthday==null)
{
this.addActionError("birthday invalid");
}
if(graduate==null){
this.addActionError("graduate invalid");
}
if(birthday!=null && graduate!=null)
{
Calendar c1=Calendar.getInstance();
c1.setTime(birthday);
Calendar c2=Calendar.getInstance();
c2.setTime(graduate);
if(!c1.before(c2))
{
this.addActionError("birthday should be before graduate");
}
}
super.validate();
}
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 getGraduate() {
return graduate;
}
public void setGraduate(Date graduate) {
this.graduate = graduate;
}
}


register.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@taglib prefix="s" uri="/struts-tags" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">

<title>My JSP 'register.jsp' starting page</title>

<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->

</head>

<body>
<s:form action="register" method="post">
<s:textfield name="username" label="username"></s:textfield>
<s:password name="password" label="password"/>
<s:password name="repassword" label="repassword"/>
<s:textfield name="age" label="age"/>
<s:textfield name="birthday" label="birthday"/>
<s:textfield name="graduate" label="graduate"/>
<s:submit valude="submit"></s:submit>
</s:form>
<s:actionerror/>
-------------------------------------------------------------------------
<s:fielderror></s:fielderror>
</body>
</html>


result.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<%@taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">

<title>My JSP 'result.jsp' starting page</title>

<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->

</head>

<body>
username:<s:property value="username"/>
password:<s:property value="password"/>
repassword:<s:property value="repassword"/>
age:<s:property value="age"/>
birthday:<s:property value="birthday"/>
graduate:<s:property value="graduate"/>
</body>
</html>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值