如果唯一的要求是浏览器地址栏与视图保持同步,您可以简单地使用<h:button
代替<h:commandButton
。这有效是因为<h:button
使用 Javascript 生成 HTTP GET 请求来启动并导航到流程。流的 ID 必须指定为outcome
属性:
索引.xhtml:
<h:form>
Click to enter flow1
<h:button outcome="flow1" value="Flow 1">
<f:param name="testInput" value="hi there" />
</h:button>
</h:form>
也可以看看:
- h:button 和 h:commandButton 之间的区别
如果要求在授予启动流程的权限之前必须完成一些检查,则必须手动初始化并导航到流程,如下所示:
索引.xhtml:
<h:form>
Click to enter flow1
<h:commandButton action="#{backingBean.startFlow1()}" value="Flow 1" />
</h:form>
流程1.xhtml:
<h:form>
Hi this is page 1.
<h:inputText label="Enter something:" value="#{flowScope.testOutput}"/><br/>
Request parameter: #{flowScope.testInput}<br/>
<h:commandButton action="returnFromFlow1"/>
</h:form>
支持Bean:
public void startFlow1() {
if (!permissionGranted) {
// show "permission denied"
return;
}
final FacesContext facesContext = FacesContext.getCurrentInstance();
final Application application = facesContext.getApplication();
// get an instance of the flow to start
final String flowId = "flow1";
final FlowHandler flowHandler = application.getFlowHandler();
final Flow targetFlow = flowHandler.getFlow(facesContext,
"", // definingDocumentId (empty if flow is defined in "faces-config.xml")
flowId);
// get the navigation handler and the view ID of the flow
final ConfigurableNavigationHandler navHandler = (ConfigurableNavigationHandler) application.getNavigationHandler();
final NavigationCase navCase = navHandler.getNavigationCase(facesContext,
null, // fromAction
flowId);
final String toViewId = navCase.getToViewId(facesContext);
// initialize the flow scope
flowHandler.transition(facesContext,
null, // sourceFlow
targetFlow,
null, // outboundCallNode
toViewId); // toViewId
// add the parameter to the flow scope
flowHandler.getCurrentFlowScope()
.put("testInput", "hi there2!");
// navigate to the flow by HTTP GET request
final String outcome = toViewId + "?faces-redirect=true";
navHandler.handleNavigation(facesContext,
null, // from action
outcome);
}
请注意,在这种情况下,参数不能添加到按钮,而必须添加到流程范围!
另请注意,必须通过以下方式重定向到流程NavigationHandler#handleNavigation()代替ExternalContext#redirect()因为否则流作用域就会终止!
也可以看看:
- 可以从 Servlet 内启动 faces 流吗?
- 以编程方式获取导航案例
faces-config.xml 按结果
- Omnifaces Faces.redirect 失去对话范围