首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何使用faces-redirect进入JSF2.2流

如何使用faces-redirect进入JSF2.2流
EN

Stack Overflow用户
提问于 2014-05-20 03:21:45
回答 3查看 3.4K关注 0票数 10

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

代码语言:javascript
运行
复制
src/main/webapp
|
|- index.xhtml
|- flow1
   |- flow1-flow.xml
   |- flow1.xhtml

index.xhtml有一个简单的表单,它使用一个参数进入流:

代码语言:javascript
运行
复制
<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.xhtml显示参数并允许您在flow作用域中输入值:

代码语言:javascript
运行
复制
<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>

the只是将返回节点定义为"returnFromFlow1“并将其设置为/index.xhtml。

这似乎起作用了。我想在进入流时实现post-redirect-get,以便浏览器地址栏与视图保持同步。所以我很自然地尝试了action="flow1?faces-redirect=true“。此更改会阻止流执行..它只是在单击按钮时重新加载index.xhtml。

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

在做了一些研究后,我发现了一个设置" to - flow -document-id“以强制其初始化流的提示。所以我添加了我的命令按钮。没有变化。

有没有关于如何做到这一点的想法?

EN

回答 3

Stack Overflow用户

发布于 2017-07-22 08:36:14

如果唯一的要求是浏览器地址栏与视图保持同步,那么您可以简单地使用<h:button而不是<h:commandButton。这之所以有效,是因为<h:button使用Javascript生成HTTP GET请求来启动和导航到流。流的ID必须指定为outcome属性的值:

index.xhtml:

代码语言:javascript
运行
复制
<h:form>
    Click to enter flow1
    <h:button outcome="flow1" value="Flow 1">
        <f:param name="testInput" value="hi there" />
    </h:button>
</h:form>

另请参阅:

如果要求在授予启动流程的权限之前必须执行一些检查,则必须手动初始化并导航到流程,如下所示:

index.xhtml:

代码语言:javascript
运行
复制
<h:form>
    Click to enter flow1
    <h:commandButton action="#{backingBean.startFlow1()}" value="Flow 1" />
</h:form>

flow1.xhtml:

代码语言:javascript
运行
复制
<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>

BackingBean:

代码语言:javascript
运行
复制
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);
}

请注意,在这种情况下参数不能添加到按钮中,而必须添加到流程作用域中!另请注意,必须由 而不是 完成对流的重定向,否则流作用域将终止!

另请参阅:

票数 1
EN

Stack Overflow用户

发布于 2014-10-12 04:11:28

如果我没看错的话,您应该能够在JSF-2.2 faces-config schema -config.xml中指定一个<redirect/>指令。因此,使用以下内容,您应该能够实现重定向:

代码语言:javascript
运行
复制
       <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>
票数 0
EN

Stack Overflow用户

发布于 2014-11-24 20:42:58

请运行这个例子,我希望它能帮到你。

代码语言:javascript
运行
复制
<navigation-rule>
    <navigation-case>
        <from-outcome>flow1</from-outcome>
        <to-view-id>flow1.xhtml</to-view-id>
        <redirect></redirect>
    </navigation-case>
</navigation-rule>
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/23745606

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档