I have a JSP page (page1.jsp) showing a data table. There are also buttons in the table like this:
<h:column>
<f:facet name="header" >
<h:outputText value=""/>
</f:facet>
<h:commandButton value="Show items" action="#{firstBean.displayItems}" immediate="true" />
</h:column>
The bean:
public void displayItems() throws IOException {
MyClass theClass = (MyClass) dataTable.getRowData();
String theId = theClass.getIdentityNumber();
// ...
}
When we click on the button I want to move to another JSP page (page2.jsp). On page 2, there is also a data table. This table is created via a call to a bean called “facade” and a parameter (String – id). I.e when the button is pressed, I want to be moved to JSP page 2, and this page will display a datatable based on a call like this:
myList = facade.getDeliveriesById(theId);
So page 2 is dependent on stuff from page 1, either a string id, or if one can somehow set a list?
I guess the question is:
- Should I in “firstBean.displayItems” make a redirect to jsp page 2 with a “get” paramater, after extracting this id (see above) ?
- Is there a way of setting the list to be used on page 2 there in “firstBean.displayItems”?
What’s the normal way of going from one page to another in JSF (with data)?
In JSF 1.x, the normal way is to return a
Stringas navigation case outcome.in combination with the following entry in
faces-config.xml:It will then go to
page2.jsf.On JSF 2.x, you don’t need the
faces-config.xml. Just return the exact filename without extension, e.g."page2"and JSF will then automatically locate the right view. This is called implicit navigation.Update: you seem to have a single “controller” bean per page and you’d like to share the data between those beans without referencing the other bean in the page. Very reasonable. This is doable by splitting the data out into another managed bean which is to be injected as managed property in the both “controller” beans.
E.g.
And
You can access it in
page2like follows:In JSF 1.x you need to inject it by
<managed-property>in faces-config. You can find an example in this article. In JSF 2.x you can just annotate the managed property using@ManagedProperty. In future questions, please mention the JSF version you’re using. This way we can give more detailed suited answers without noise. JSF 2.x has namely pretty a lot of differences (improvements) in how things needs to be approached.