JSF 在 commandButton 操作后重新加载 ViewParam
JSF reloading ViewParam after commandButton action
我正在使用 JSF 和 Primefaces。我有一个 edit.xhtml 页面,其中 f:viewParam 接收实体 ID:
<f:viewParam name="id" value="#{backingBean.entity}" converter="entityConverter" />
我有两个命令按钮,一个用于提交和保存实体:
<p:commandButton ajax="false" value="#{bundle.save}"
action="#{backingBean.save()}"/>
另一种将项目添加到实体集合的方法:
<p:commandButton ajax="true" process="@this" value="#{bundle.add}"
actionListener="#{backingBean.addItem()}" />
这是我的 BackingBean:
@ViewScoped
@Named("backingBean")
public class BackingBean {
@EJB
MyDAO myDAO;
private Entity entity; //with getters and setters
public void addItem() {
entity.getData().add(new Item()); //another entity object
}
public void save(){
myDAO.save(entity);
}
...
}
我还有一个 EntityConverter class 调用 DAO 并加载对象:
@Override
public Object getAsObject(FacesContext context, UIComponent component, String value) {
try {
return myDAO.findById(Entity.class, Long.valueOf(value));
} catch (Exception e) {
return null;
}
}
如果我尝试添加多个项目或单击保存按钮,BackingBean class 中的实体将通过调用转换器 class 的 getAsObject 方法重新加载。
我做错了什么?
谢谢!
为清楚起见,正常的 f:param 将始终以这种方式运行。我在我所有的项目中都使用 OmniFaces ViewParam 来修复这些问题。
无状态模式可避免在回发时进行不必要的转换、验证和模型更新
The standard UIViewParameter implementation calls the model setter
again after postback. This is not always desired when being bound to a
view scoped bean and can lead to performance problems when combined
with an expensive converter. To solve this, this component by default
stores the submitted value as a component property instead of in the
model (and thus in the view state in case the binding is to a view
scoped bean).
The standard UIViewParameter implementation calls the converter and
validators again on postbacks. This is not always desired when you
have e.g. a required="true", but the parameter is not retained on form
submit. You would need to retain it on every single command
link/button by . To solve this, this component doesn't call
the converter and validators again on postbacks.
我正在使用 JSF 和 Primefaces。我有一个 edit.xhtml 页面,其中 f:viewParam 接收实体 ID:
<f:viewParam name="id" value="#{backingBean.entity}" converter="entityConverter" />
我有两个命令按钮,一个用于提交和保存实体:
<p:commandButton ajax="false" value="#{bundle.save}"
action="#{backingBean.save()}"/>
另一种将项目添加到实体集合的方法:
<p:commandButton ajax="true" process="@this" value="#{bundle.add}"
actionListener="#{backingBean.addItem()}" />
这是我的 BackingBean:
@ViewScoped
@Named("backingBean")
public class BackingBean {
@EJB
MyDAO myDAO;
private Entity entity; //with getters and setters
public void addItem() {
entity.getData().add(new Item()); //another entity object
}
public void save(){
myDAO.save(entity);
}
...
}
我还有一个 EntityConverter class 调用 DAO 并加载对象:
@Override
public Object getAsObject(FacesContext context, UIComponent component, String value) {
try {
return myDAO.findById(Entity.class, Long.valueOf(value));
} catch (Exception e) {
return null;
}
}
如果我尝试添加多个项目或单击保存按钮,BackingBean class 中的实体将通过调用转换器 class 的 getAsObject 方法重新加载。
我做错了什么? 谢谢!
为清楚起见,正常的 f:param 将始终以这种方式运行。我在我所有的项目中都使用 OmniFaces ViewParam 来修复这些问题。
无状态模式可避免在回发时进行不必要的转换、验证和模型更新
The standard UIViewParameter implementation calls the model setter again after postback. This is not always desired when being bound to a view scoped bean and can lead to performance problems when combined with an expensive converter. To solve this, this component by default stores the submitted value as a component property instead of in the model (and thus in the view state in case the binding is to a view scoped bean).
The standard UIViewParameter implementation calls the converter and validators again on postbacks. This is not always desired when you have e.g. a required="true", but the parameter is not retained on form submit. You would need to retain it on every single command link/button by . To solve this, this component doesn't call the converter and validators again on postbacks.