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

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

问题描述

从我的bean中的一个动作,我试图重定向到另一个需要view参数的页面.在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}" />

我的豆子看起来像什么:

where my bean looks like:

@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

在导航示例中,我尝试使用view-param来执行此操作,但结果却很奇怪. 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).

您只需用豆子做

@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>

它将完全按照您的要求进行.

It will do exactly what you want.

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

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