如何使用faces重定向进入JSF2.2流

如何使用faces重定向进入JSF2.2流,jsf,jsf-2.2,post-redirect-get,faces-flow,Jsf,Jsf 2.2,Post Redirect Get,Faces Flow,我有一个基本的流程示例: src/main/webapp | |- index.xhtml |- flow1 |- flow1-flow.xml |- flow1.xhtml index.xhtml有一个简单的表单,它通过一个参数进入流: <h:form> Click to enter flow1 <h:commandButton action="flow1" value="Flow 1"> <f:param name=

我有一个基本的流程示例:

src/main/webapp
|
|- index.xhtml
|- flow1
   |- flow1-flow.xml
   |- flow1.xhtml
index.xhtml有一个简单的表单,它通过一个参数进入流:

<h:form>
    Click to enter flow1
    <h:commandButton action="flow1" value="Flow 1">
        <f:param name="testInput" value="hi there"/>
    </h:commandButton>
</h:form>

单击以输入flow1
flow1.xhtml显示参数,并允许您在flow scope中输入值:

<h:form>
    Hi this is page 1.
    <h:inputText label="Enter something:" value="#{flowScope.testOutput}"/><br/>
    Request parameter: #{param['testInput']}<br/>
    <h:commandButton action="returnFromFlow1"/>
</h:form>

嗨,这是第一页。

请求参数:#{param['testInput']}
flow1-flow.xml只是将返回节点定义为“returnFromFlow1”,并将其设置为/index.xhtml

这似乎奏效了。我想在输入流时实现post redirect get,以便浏览器地址栏与视图保持同步。所以我自然而然地尝试了action=“flow1?faces redirect=true”。此更改将阻止流执行。。单击按钮后,它只需重新加载index.xhtml

然后我尝试了action=“flow1/flow1.xhtml?faces redirect=true”。这将加载页面并按预期重定向,但流未初始化。当我在流中提交表单时,我得到一个关于flowScope解析为null的错误

在做了一些研究后,我发现了一个技巧,可以设置“to flow document id”,强制它初始化流。所以我添加到我的命令按钮。没有变化

关于如何实现这一点有什么想法吗?

好吧,如果我读对了,您应该能够在faces-config.xml中指定一个
指令。因此,使用以下方法,您应该能够实现重定向:

       <navigation-rule>
           <from-view-id>/pages/test/test.xhtml</from-view-id>
               <navigation-case>
                   <from-outcome>flow1</from-outcome>
                   <to-flow-document-id>flow1.xhtml</to-flow-document-id>
                       <redirect />
              </navigation-case>
       </navigation-rule>

/pages/test/test.xhtml
流程1
flow1.xhtml
好吧,如果我读的正确,您应该能够在faces-config.xml中指定一个
指令。因此,使用以下方法,您应该能够实现重定向:

       <navigation-rule>
           <from-view-id>/pages/test/test.xhtml</from-view-id>
               <navigation-case>
                   <from-outcome>flow1</from-outcome>
                   <to-flow-document-id>flow1.xhtml</to-flow-document-id>
                       <redirect />
              </navigation-case>
       </navigation-rule>

/pages/test/test.xhtml
流程1
flow1.xhtml

请运行此示例,我希望它能帮助您

<navigation-rule>
    <navigation-case>
        <from-outcome>flow1</from-outcome>
        <to-view-id>flow1.xhtml</to-view-id>
        <redirect></redirect>
    </navigation-case>
</navigation-rule>

流程1
flow1.xhtml

请运行此示例,我希望它能帮助您

<navigation-rule>
    <navigation-case>
        <from-outcome>flow1</from-outcome>
        <to-view-id>flow1.xhtml</to-view-id>
        <redirect></redirect>
    </navigation-case>
</navigation-rule>

流程1
flow1.xhtml

如果唯一的要求是浏览器地址栏与视图保持同步,您可以简单地使用
如果唯一的要求是浏览器地址栏与视图保持同步,您可以简单地使用
难道您自己没有找到答案吗?我也在努力解决这个问题。你说的“不改变”是什么意思?流量计还是被破坏了?你自己没找到答案吗?我也在努力解决这个问题。你说的“不改变”是什么意思?flowscope仍然被破坏?不幸的是,它不能工作。我已经尝试了它的几种可能的变体,但没有得到任何结果。此外,如果您创建一个有效的导航规则来流动,并且在导航期间应用它,则很可能会导致访问未初始化bean时出错,因为流不会被初始化。@T.G-这听起来像个bug,因为@T.G-奇怪的是,在实现或超类中没有任何地方可以实际检索流范围bean。定义了一个
getCurrentFlowScope
,但我找不到它的用法。甚至它的定义也是可疑的,因为根据规范和javadoc,期望在进入流时创建一个新实例。也许你应该将stacktrace发布到这里不幸的是,它不起作用。我已经尝试了它的几种可能的变体,但没有得到任何结果。此外,如果您创建一个有效的导航规则来流动,并且在导航期间应用它,则很可能会导致访问未初始化bean时出错,因为流不会被初始化。@T.G-这听起来像个bug,因为@T.G-奇怪的是,在实现或超类中没有任何地方可以实际检索流范围bean。定义了一个
getCurrentFlowScope
,但我找不到它的用法。甚至它的定义也是可疑的,因为根据规范和javadoc,期望在进入流时创建一个新实例
<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>
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);
}