struts2学习笔记七(第7讲.Struts2的输入校验续二)

[b][size=xx-large]Struts2的输入校验续二[/size][/b]
[b]主要介绍[color=red]addActionError[/color]的用法:[/b]
一、修改com.test.action下的RegisterAction.java类(把addFieldError改成addActionError):

package com.test.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 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;
}

@Override
public String execute() throws Exception {

return SUCCESS;
}

@Override
public void validate() {

if(null == username || username.length() < 6 || username.length() > 10){
this.addActionError("username invalid");
}
if(null == password || password.length() <6 || password.length() > 10){
this.addActionError("password invalid");
}
else if(null == repassword || repassword.length() < 6 || repassword.length() > 10){
this.addActionError("repassword invalid");
}
else if(!password.equals(repassword)){
this.addFieldError("password", "two passwords not the same");
}
if(age <= 0 || age > 150){
this.addActionError("age should be between 1 and 150");
}
// if(null == birthday){
// this.addFieldError("birthday", "birthday invalid");
// }
// if(null == graduation){
// this.addFieldError("graduation", "graduation invalid");
// }
if(null != birthday && null != graduation){
Calendar c1 = Calendar.getInstance();
c1.setTime(birthday);

Calendar c2 = Calendar.getInstance();
c2.setTime(graduation);

if(!c1.before(c2)){
this.addActionError("birthday should be before graduation");
}
}
}
}

[color=red]struts2标签能自动的显示错误信息,但是只局限于addFieldError级别的,当使用addActionError的时候就不能自动的显示错误的提示信息了。[/color]
二、需要在标签页面register2.jsp里面添加一个标签<s:actionerror/>:

<body>

<s:actionerror cssStyle="color:red"/>

<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="age" label="age"></s:textfield>
<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>

结果如下:

[img]http://dl.iteye.com/upload/attachment/388767/80a47a11-468e-345f-a58f-42b3600e45ee.jpg[/img]

当年龄输入的为字母型的字符串时,验证如下图:

[img]http://dl.iteye.com/upload/attachment/388775/48d2a030-1f47-317d-90f3-d9f46a053b0f.jpg[/img]

会出现验证重复的现象:
三、要修改注册页面register.jsp的标签格式:

<body>

<table align="center" width="40%">
<tr>
<td>
<s:actionerror cssStyle="color:red"/>
</td>
</tr>
</table>

<s:form action="register" theme="simple">
<table align="center" width="40%" border="1">
<tr>
<td>username
</td>
<td>
<s:textfield name="username" label="username"></s:textfield>
</td>
</tr>
<tr><td>password</td>
<td>
<s:password name="password" label="password"></s:password>
</td>
</tr>
<tr><td>repassword</td>
<td>
<s:password name="repassword" label="repassword"></s:password>
</td>
</tr>
<tr><td>age</td>
<td>
<s:textfield name="age" label="age"></s:textfield>
</td>
</tr>
<tr><td>birthday</td>
<td>
<s:textfield name="birthday" label="birthday"></s:textfield>
</td>
</tr>
<tr><td>graduation</td>
<td>
<s:textfield name="graduation" label="graduation"></s:textfield>
</td>
</tr>
<tr><td><s:submit value=" submit "></s:submit></td>
<td>
<s:reset value=" reset "></s:reset>
</td>
</tr>
</table>
</s:form>

</body>

效果如下图所示:

[img]http://dl.iteye.com/upload/attachment/388790/f8bd1e06-521f-3b09-8cc8-b05954ac1650.jpg[/img]
[color=red]问题:当页面出现多个form表单时,如何用不同的方法来进行验证?[/color]
四、修改struts.xml中RegisterAction的配置,添加一个method属性,指定acton要执行的方法abc:

<action name="register" class="com.test.action.RegisterAction" method="abc">
<result name="success">/success.jsp</result>
<result name="input">/register2.jsp</result>
</action>

五、在RegisterAction中创建Action要执行的abc方法:

public String abc() throws Exception {

System.out.println("abc method invoked");
return SUCCESS;
}

[color=red]问题:当在RegisterAction类中有多个的表单的action需要验证该怎么办?[/color]
六、在struts.xml中添加一个需要用到xyz方法的Action:

。。。
<action name="register" class="com.test.action.RegisterAction" method="abc">
<result name="success">/success.jsp</result>
<result name="input">/register2.jsp</result>
</action>

<action name="zly" class="com.test.action.RegisterAction" method="xyz">
<result name="success">/success.jsp</result>
<result name="input">/register2.jsp</result>
</action>

七、在RegisterAction类中创建对应的action验证方法:

public String abc() throws Exception {

System.out.println("abc() method invoked");
return SUCCESS;
}

public void validateAbc(){

System.out.println("validateAbc() invoked");
}

控制台输出效果:

[img]http://dl.iteye.com/upload/attachment/388801/ea43fe52-5be4-368c-b455-435eb112279b.jpg[/img]
[color=red]说明:[/color]在RegisterAction中如果自定义了验证的方法如validateAbc(),但是同样会执行validate验证的方法,这是不科学的,所以可以不重写validate方法,但是execute方法有没有验证了,所以validate方法可以写成validateExexute()方法。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值