JSF Pass parameter to another page
In JSF how do you pass a parameter from one page to another without using managed beans?
e.g.
<h:dataTable value="#{myObjects}" var="object">
<h:column> 开发者_高级运维
<h:commandButton value="View Object" action="view-object"/>
</h:column>
</h:dataTable>
I want to pass the object so an the next page I can simply do #{object.displayValue}
Thxs.
First of all, you can't do this without using a managed bean.
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>
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;
}
}
I assume that you're using JSF 1.x and not 2.x yet, else a @ViewScoped
bean was better.
And in the next page:
<p>#{bean.item.value}</p>
精彩评论