简体   繁体   English

Tapestry 5:持久性页面数据的初始化

[英]Tapestry 5: initialization of persistent page data

Consider following scenario. 考虑以下情形。 Have a page with some persistent object obj : 有一个包含一些持久对象obj的页面:

public class SomePage {
   @Persistent
   @Property
   SomeBean obj;

   @Property
   @PageActivationContext
   private SomeActivation actObj;

   ...

   void onValidateFromForm() { ... }
}

This obj is edited on a page: 在页面上编辑此obj

<t:textfield t:id="value" t:value="obj.value"/>

Lets consider initialization procedure of obj state like this: 让我们考虑这样的obj状态初始化过程:

(*) obj.value = actObj.value; (*) obj.value = actObj.value;

(I strongly need to unbind value from actObj , please don't ask why). (我强烈需要从actObj取消绑定value ,请不要问为什么)。

Have also a validation method. 也有一种验证方法。 When validation fails, I'd like to show errors list on page and keep also all values, which user filled on form and which were stored into obj . 当验证失败时,我想在页面上显示错误列表,并保留所有值,这些值由用户填写在表单上,​​并且存储在obj

So: 所以:

  1. when page firstly initialized with specific actObj I'd like to initialize obj 当页面首先使用特定的actObj初始化时,我想初始化obj
  2. when page validation fails, I don't want to reinitialize obj , because I want to keep its values and show them user with errors list. 当页面验证失败时,我不想重新初始化obj ,因为我想保留其值并向用户显示错误列表。

The question is: where I should place initialization block (*) ? 问题是:我应该在哪里放置初始化块(*)

You should be able to use onActivate() as this will occur before the request parameters are applied. 您应该能够使用onActivate(),因为这将在应用请求参数之前发生。 So your submit event will first set the value from the activation context and then overwrite it with the value from the text field. 因此,您的Submit事件将首先在激活上下文中设置值,然后用文本字段中的值覆盖它。

Note that tapestry only does a redirect after post when validation succeeds. 请注意,验证成功后,tapestry仅在发布后进行重定向。 When validation fails tapestry renders the errors in the POST response. 验证失败时,Tapestry将在POST响应中呈现错误。 For this reason, you might find that you don't need @Persist at all and can go stateless via @PageActivationContext / onActivate() / onPassivate() 因此,您可能会发现根本不需要@Persist,并且可以通过@PageActivationContext / onActivate()/ onPassivate()进入无状态状态

You can use activation handler instead of annotation, so: 您可以使用激活处理程序代替注释,因此:

@Persistent
@Property
private SomeBean obj;

@Property
private SomeActivation actObj;

@OnEvent(EventConstants.ACTIVATE)
void activatePage(SomeActivation actObj) {
  this.actObj = actObj;
  if (obj == null) {
    obj = // initialize
    obj.value = actObj.value
  }
}

@OnEvent(EventConstants.PASSIVATE)
Object passivatePage() {
  return actObj;
}

@OnEvent(value = EventConstants.SUCCESS, component = "form")
void success() {
  // do some staff

  // reset obj
  obj = null;
}

@OnEvent(value = EventConstants.FAILURE, component = "form")
void failure() {
  // do some staff
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM