JSF将参数传递到另一个页面 [英] JSF Pass parameter to another page

查看:70
本文介绍了JSF将参数传递到另一个页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在JSF中,如何在不使用托管bean的情况下将参数从一页传递到另一页?

In JSF how do you pass a parameter from one page to another without using managed beans?

例如

<h:dataTable value="#{myObjects}" var="object">
<h:column>              
        <h:commandButton value="View Object" action="view-object"/>
</h:column>                                             
</h:dataTable>  

我想传递对象,以便在下一页中我可以简单地执行#{object.displayValue}

I want to pass the object so an the next page I can simply do #{object.displayValue}

感谢.

推荐答案

首先,如果不使用托管bean,就无法做到这一点.

First of all, you can't do this without using a managed bean.

最好的方法是使用 DataModel 作为h:dataTablevalue,这样您就可以通过

The best approach would be using a DataModel as the value of the h:dataTable so that you can retrieve the currently selected row object by DataModel#getRowData(). You only need to ensure that the bean preserves the same model in the subsequent request. In other words, fill the model in the constructor of the bean.

<h:form>
    <h:dataTable value="#{bean.model}" var="item">
        <h:column>              
            <h:commandButton value="View Object" action="#{bean.view}"/>
        </h:column>                                             
    </h:dataTable>
</h:form>

具有如下请求(或会话)范围的Bean:

With a request (or session) scoped bean which look like this:

public class Bean {

    private DataModel model;
    private Item item;

    public Bean() {
        List<Item> list = new ArrayList<Item>();
        list.add(new Item(1, "value1"));
        list.add(new Item(2, "value2"));
        list.add(new Item(3, "value3"));
        model = new ListDataModel(list);
    }

    public String view() {
        item = (Item) model.getRowData();
        return "view";
    }

    public DataModel getModel() {
        return model;
    }

    public Item getItem() {
        return item;
    }

}

我假设您正在使用JSF 1.x而不是2.x,否则@ViewScoped bean是

I assume that you're using JSF 1.x and not 2.x yet, else a @ViewScoped bean was better.

然后在下一页:

<p>#{bean.item.value}</p>

这篇关于JSF将参数传递到另一个页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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