[英]JSF 2 ViewScope questions
引用这篇好文章 ,
新的视图范围应该正好解决这些问题。 只要您一次又一次地将表单提交到同一视图,@ ViewScoped bean就会存在。 换句话说,只要action方法返回null或甚至void,bean就会在下一个请求中出现。 导航到不同的视图后,bean将被删除。
我想到了这些问题:
index.xhtml
,并且如果我明确指定return "index";
或者<h:commandButton action="index.xhtml" ..>
,它基本上返回到相同的视图,bean将再次重新创建..为什么? return "?faces-redirect=true"
或<h:commandButton action="?faces-redirect=true" ..>
,是的,我想跳过定义<h:commandButton action="?faces-redirect=true" ..>
的导航faces-config.xml
使用<redirect/>
。 如果我当前的视图是index.xhtml,并且如果我明确指定返回“index”; 或者,基本上返回到相同的视图,bean将再次重新创建..为什么?
如果明确指定结果(读取:视图),则将创建新视图。 您必须从操作方法返回null或void(或者只是省略命令组件的action
属性)。
我必须承认,我理解你的困惑,并且“视图”一词可以根据具体情况进行不同的解释。 我想我迟早会修改链接文章中的措辞。
Viewscoped bean可以在重定向中存活吗?
不可以。只有会话范围的bean可以和flash范围内的对象(一旦重定向完成就会立即结束,这可能是你在提出这个问题时想到的功能要求的实际需要)。
如果提供了javax.faces.ViewState,则视图状态可以继续重定向。 我发现目前最简单的解决方案是实现导航处理程序。
在faces-config.xml中:
<faces-config>
<application>
...
<navigation-handler>com.intersult.jsf.util.RedirectNavigationHandler</navigation-handler>
</application>
...
</faces-config>
Java类:
public class RedirectNavigationHandler extends ConfigurableNavigationHandler {
private NavigationHandler parent;
public RedirectNavigationHandler(NavigationHandler parent) {
this.parent = parent;
}
@Override
public void handleNavigation(FacesContext context, String from, String outcome) {
if (!context.getPartialViewContext().isPartialRequest()) {
if (outcome == null)
outcome = context.getViewRoot().getViewId();
if (!outcome.endsWith("?faces-redirect=true"))
outcome += "?faces-redirect=true";
String viewState =
context.getExternalContext().getRequestParameterMap().get(ResponseStateManager.VIEW_STATE_PARAM);
outcome += "&javax.faces.ViewState=" + viewState;
}
parent.handleNavigation(context, from, outcome);
}
}
这会产生一些影响:每个重定向都不再是一个独立的请求。 它取决于视图状态,它不能被必然恢复。 所以有一个后备策略,如果它已过期,例如。 重定向视图过期的异常。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.