从 JSF2 中的操作导航时如何传递视图参数? [英] How do you pass view parameters when navigating from an action in JSF2?

查看:13
本文介绍了从 JSF2 中的操作导航时如何传递视图参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从我的 bean 中的一个操作,我试图重定向到另一个需要视图参数的页面.在 JSF2 中推荐的方法是什么?

From an action in my bean, I'm trying to redirect to another page expecting a view parameter. What is the recommended way to do this in JSF2?

例如,假设我的源页面是:http://localhost/page1.xhtml

E.g., say my source page is: http://localhost/page1.xhtml

它有一个调用动作的命令按钮:

it has a commandButton that calls an action:

<h:commandButton value="submit" action="#{myBean.submit}" />

我的豆子长什么样子:

@ManagedBean
@RequestScoped
public class MyBean {

private int id;

public String submit() {
    //Does stuff
    id = setID();
    return "success";
}

现在,我希望提交"操作的返回导航到http://localhost/page2.xhtml?id=2

And now, I want the 'submit' action's return to navigate to http://localhost/page2.xhtml?id=2

我尝试在导航案例中使用视图参数来执行此操作,但结果很奇怪.faces-config 片段如下所示:

I've tried to do this with a view-param in my navigation case, but with odd results. The faces-config snippet looks like the following:

<navigation-rule>
    <from-view-id>/page1.xhtml</from-view-id>
    <navigation-case>
        <from-outcome>success</from-outcome>
        <to-view-id>/page2.xhtml</to-view-id>
        <redirect>
            <view-param>
                <name>id</name>
                <value>#{myBean.id}</value>
            </view-param>
        </redirect>
    </navigation-case>
</navigation-rule>

奇怪的行为是,即使 myBean 设置为请求范围,它也只在我第一次加载应用程序时调用 myBean.getId(),并在所有后续调用中重用相同的值,为 page2 生成错误的视图参数.

The weird behaviour being, even though myBean is set to request scoped, it only calls myBean.getId() the first time I load my application, and reuses that same value for all subsequent calls, producing incorrect view parameters for page2.

所以我正在寻找更好的方法来做到这一点,或者为什么每次都没有从我的 bean 请求视图参数的原因/解决方案.

So I'm looking for either a better way to do this, or a reason/solution for why the view-param is not being requested from my bean each time.

推荐答案

在 JSF 中传递参数的不直观之处在于,您不决定发送什么(在操作中),而是决定您希望接收什么(在操作中)目标页面).

The unintuitive thing about passing parameters in JSF is that you do not decide what to send (in the action), but rather what you wish to receive (in the target page).

当您执行以重定向结束的操作时,将加载目标页面元数据,并读取所有必需的参数并将其作为参数附加到 url.

When you do an action that ends with a redirect, the target page metadata is loaded and all required parameters are read and appended to the url as params.

请注意,这与任何其他 JSF 绑定的机制完全相同:您无法从一处读取 inputText 的值并将其写入其他地方.viewParam 中定义的值表达式用于读取(重定向前)和写入(重定向后).

Note that this is exactly the same mechanism as with any other JSF binding: you cannot read inputText's value from one place and have it write somewhere else. The value expression defined in viewParam is used both for reading (before the redirect) and for writing (after the redirect).

有了你的 bean,你就可以:

With your bean you just do:

@ManagedBean
@RequestScoped
public class MyBean {

private int id;

public String submit() {
    //Does stuff
    id = setID();
    return "success?faces-redirect=true&includeViewParams=true";
}

// setter and getter for id

如果接收方有:

    <f:metadata>
        <f:viewParam name="id" value="#{myBean.id}" />
    </f:metadata>

它会做你想做的事.

这篇关于从 JSF2 中的操作导航时如何传递视图参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆