Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1199140
  • 博文数量: 89
  • 博客积分: 10546
  • 博客等级: 上将
  • 技术积分: 1510
  • 用 户 组: 普通用户
  • 注册时间: 2004-10-16 01:24
文章分类

全部博文(89)

文章存档

2012年(7)

2011年(4)

2010年(5)

2009年(52)

2008年(21)

分类: Java

2009-03-23 18:37:31

Struts 1 内置了多步提交表单的方案,在 ActionForm 添加一个 page 属性,指定当前是第几步,将 ActionForm 放入 Session 中,或者在下一步表单中使用隐藏属性存放其上一步的表单数据,保证多步表单数据不丢失。Stripes 也提供了一种有效的方式,来处理多步提交。

回到注册程序,在填写注册信息之前,要求用户先阅读相关协议。

创建一个阅读协议 JSP 页面。

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@taglib prefix="stripes" uri="" %>
"">




JSP Page


Read User License!




License,License,License,License,License,License,
License,License,License,License,License,License,
License,License,License,License,License,License,
License,License,License,License,License,License,
License,License,License,License,License,License,


I have read the license and gree with the content.




RegisterActionBean中添加一个属性 acceptLicense ,类型为 boolean ,并添加相应的 event 方法。

@Wizard(startEvents = "readLicense")
public class RegisterActionBean extends BaseActionBean {

@Validate(required = true, mask = "[0-9a-zA-Z]{6,20}")
private String username;
@Validate(required = true, minlength = 6, maxlength = 20, mask = "[0-9a-zA-Z]+")
private String password;
@Validate(required = true, converter = EmailTypeConverter.class)
private String email;
@Validate(required = true, expression = "this eq password")
private String confirmPassword;
@Validate(converter = DateTypeConverter.class)
Date birthDate;
@Validate(converter = BooleanTypeConverter.class)
boolean subscriptionEnabled;
@ValidateNestedProperties({
@Validate(field = "zipcode", required = true),
@Validate(field = "addressLine1", required = true),
@Validate(field = "addressLine2", required = true)
})
private Address address;


private boolean acceptLicense = false;

public boolean isAcceptLicense() {
return acceptLicense;
}

public void setAcceptLicense(boolean acceptLicense) {
this.acceptLicense = acceptLicense;
}

public Address getAddress() {
return address;
}

public void setAddress(Address address) {
this.address = address;
}

public Date getBirthDate() {
return birthDate;
}

public void setBirthDate(Date birthDate) {
this.birthDate = birthDate;
}

public boolean isSubscriptionEnabled() {
return subscriptionEnabled;
}

public void setSubscriptionEnabled(boolean subscriptionEnabled) {
this.subscriptionEnabled = subscriptionEnabled;
}

public String getConfirmPassword() {
return confirmPassword;
}

public void setConfirmPassword(String confirmPassword) {
this.confirmPassword = confirmPassword;
}

public String getEmail() {
return email;
}

public void setEmail(String email) {
this.email = email;
}

public String getPassword() {
return password;
}

public void setPassword(String password) {
this.password = password;
}

public String getUsername() {
return username;
}

public void setUsername(String username) {
this.username = username;
}

@DefaultHandler
public Resolution register() {
return new ForwardResolution("/success.jsp");
}

@ValidationMethod(on = "register")
public void userExsited(ValidationErrors errors) {
if ("testuser".equals(username)) {
errors.add("username", new SimpleError("This username is taken , please select a different one."));
}
if (!errors.isEmpty()) {
errors.addGlobalError(new SimpleError("Error ocurs on save. Please fix it firstly."));
}
}

public Resolution readLicense() {
return new ForwardResolution("/readlicense.jsp");
}

@DontValidate
public Resolution back() {
return new ForwardResolution("/readlicense.jsp");
}

@ValidationMethod(on = "prepareRegister")
public void mustAcceptLicense(ValidationErrors errors) {
if (this.acceptLicense == false) {
errors.add("acceptLicense", new SimpleError("You must agree with the content of the license."));
}
if (!errors.isEmpty()) {
errors.addGlobalError(new SimpleError("Error ocurs on save. Please fix it firstly."));
}
}

public Resolution prepareRegister() {
return new ForwardResolution("/register.jsp");
}
}

Stripes 提供了 Wizard Annotation 保证多个页面的表单处理起来像在个页面一样。

register.jsp 页面添加一个回退到第一步按钮。






现在运行这个程序来体验一下。

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