表单的重复提交

本文介绍了一款为Tapestry 4.0设计的扩展库,该库提供了一种新的属性持久化策略——“闪存”(Flash)。通过这种策略,临时消息等属性可以短暂地保存在会话中,直到页面被再次访问。文章还详细说明了如何使用注解和XML配置来实现闪存属性,并结合重定向后提交模式来避免重复提交表单。

地址:http://howardlewisship.com/tapestry-javaforge/tapestry-flash/

Description

This library is an extension to Tapestry 4.0 that provides a new type of property persistence strategy, "flash". A flashed property is stored in the session only briefly ... until the next request that accesses that page.

Flashed properties are most often used to store temporary messages. They are extremely useful when combined with the redirect-after-post pattern for handling form submissions.

The name and the idea have been borrowed from Ruby On Rails.

Usage

Any property of a page or component may be marked as persisting in the flash. Using the @Persist annotation:

public abstract class MyPage extends BasePage
{
  @Persist("flash")
  public abstract String getMessage();
  
  public abstract void setMessage(String message);
  

Alternately, you may use the <property> element in a page or component specification:

<property name="message" persist="flash"/>

Flashed properties make the most sense when combined with the redirect-after-post pattern (otherwise, the flashed properties may appear to live too long: once for the initial render, then once more on the next access to the page). The redirect-after-post approach is very easy to implement in Tapestry 4.0. From the form's submit listener method:

  @InjectObject("engine-service:page")
  public abstract IEngineService getPageService();
  
  public ILink doFormSubmit()
  {
    // ... write data to a database
    
    setMessage("Your changes have been saved.");
    
    return getPageService().getLink(false, getPageName());
  }   
  

Returning an ILink from a listener method causes Tapestry to send a redirect reponse to the client web browser; the use of the page engine service will cause the current page to be redisplayed, with the message set in the listener method ("Your changes ...").

If the user refreshes the page, the form will not be resubmitted, instead, the page will simply be re-rendered. The message will, however, not appear (that is the nature of the flash; values stick around only until used the first time).

Availability

Version 1.0.0 is now available. If you don't care for Maven, you may access the binary and source distribution files at http://howardlewisship.com/downloads/tapestry-javaforge/.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值