Chinaunix首页 | 论坛 | 博客
  • 博客访问: 336883
  • 博文数量: 96
  • 博客积分: 2041
  • 博客等级: 大尉
  • 技术积分: 1080
  • 用 户 组: 普通用户
  • 注册时间: 2012-01-20 14:08
文章分类

全部博文(96)

文章存档

2015年(2)

2013年(1)

2012年(93)

分类: 系统运维

2012-01-20 14:19:05

首先需要struts2标签<%@taglib prefix = "s" uri = "/struts-tags" %>

       有两种用于校验的错误提示标签: ,二者表示两个不同的范围。

 

register.jsp

<%@ page language="java" pageEncoding="gbk"%>

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

<html> 

<body>

    <s:actionerror/>

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

    <s:fielderror cssStyle = "color:blue">s:fielderror>

   

       

    <s:form action = "register" >

    <s:textfield name = "username" label = "username">s:textfield>

    <s:password name = "password" label = "password">s:password>

    <s:password name = "repassword" label = "repassword">s:password>

    <s:textfield name = "birthday" label = "birthday">s:textfield>

    <s:textfield name = "graduation" label = "graduation">s:textfield>

   

    <s:submit value = "submit">s:submit>

   

   

   

    s:form>

   

   

body>

html>

说明:validate()方法在execute()方法之前执行(不论编码位置谁前谁后)。

 

 

struts2.xml部分:

 

       <action name = "register" class = "com.shengsiyuan.struts2.RegisterAction">

       <result name = "SUCCESS">/registerResult.jspresult>

       <result name = "input">/register.jspresult>

       action>

说明:ActionSupport类中有:public static final String INPUT = “input” ;(如果输入不合法,则自动跳转到input页面再次输入。通常跳转回用户原先输入信息的页面)

若未设置 <result name = "input">/register.jspresult>,则会出现如图:

 


 

 

RegisterAction.java

package com.shengsiyuan.struts2;

 

import java.util.Calendar;

import java.util.Date;

 

import com.opensymphony.xwork2.ActionSupport;

import com.sun.faces.taglib.html_basic.GraphicImageTag;

 

public class RegisterAction extends ActionSupport

{

       private String username ;

       private String password  ;

       private String repassword ;

       private int age ;

       private Date birthday ;

       private Date graduate ;

      

      

       public Date getGraduate()

       {

              return graduate;

       }

       public void setGraduate(Date graduate)

       {

              this.graduate = graduate;

       }

       public String getUsername()

       {

              return username;

       }

       public void setUsername(String username)

       {

              System.out.println("00");

              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;

       }

 

 

       @Override

       public String execute() throws Exception

       {

              return "SUCCESS" ;

       }

       public void validate()

       {

              if(null == username || username.length() < 4 || username.length() >6)//取不合法的情况进行处理

              {

                     this.addActionError("username invalid") ;

                     this.addFieldError("username", "username invalid in field") ;

              }

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

              {

                     this.addActionError("password invalid") ;

              }

              else if(null == repassword ||repassword.length() <4 ||repassword.length() >6)

              {

                     this.addActionError("repassword invalid") ;

              }

              else if(!password.equals(repassword))

              {

                     this.addActionError("2ci buyi zhi") ;

              }

              if(age < 10 || age >50)

              {

                     this.addActionError("age is not..") ;

              }

              if(null == birthday)

              {

                     this.addActionError("birthday..") ;

                    

              }

              if(null == graduate)

              {

                     this.addActionError("graduate...") ;

              }

              if(null != birthday && null !=graduate)

              {

                     Calendar c1 = Calendar.getInstance() ;

                     c1.setTime(birthday) ;

                    

                     Calendar c2 = Calendar.getInstance() ;

                     c2.setTime(graduate) ;

                     if(!c1.before(c2)) //正确的取反,即找错误的

                     {

                            this.addActionError("birthday...") ;

                     }

              }

             

       }

}

 

 

struts2.xml

       <result name = "SUCCESS">/registerResult.jspresult>

 

registerResult.jsp

<%@ page language="java" pageEncoding="ISO-8859-1"%>

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

<html>

 

<body>

    username :<s:property value = "username" /><br>

    password :<s:property value = "password" /><br>

    age<s:property value = "age" /><br>

    birthday:<s:property value = "birthday" /><br>

    graduate:<s:property value = "graduate" />   

   

   

   

body>

html>

 

 

 

阅读(588) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~