tags:

views:

35

answers:

2

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)?

A: 

There can be 2 good way i think:

  • Either pass a param to second page via url encoding like id=1&name="abc"

OR

  • Set a the value of selected item from page one into managed bean and use this item while populating datatable for page2 .
org.life.java
When you say backing bean, is that the same as "managed bean" ?
Jojje
yes its same just a different term.
org.life.java
You're wrong, it's not just a different term.
BalusC
@BalusC let me know the difference. I am sorry i will edit it after knowing it.
org.life.java
Basically: a backing bean is a **class** which is tied to a JSF page (view). A managed bean is **an instance** of a backing bean whose instantiation is managed by JSF. You can have multiple managed beans out of the same backing bean, one per request, session or application and even with different names. Both terms can generally be exchanged, but they definitely don't have the same meaning.
BalusC
@BalusC Thanks :)
org.life.java
+1  A: 

In JSF 1.x, the normal way is to return a String as navigation case outcome.

public String displayItems() throws IOException {
    MyClass theClass = (MyClass) dataTable.getRowData();
    String theId = theClass.getIdentityNumber();
    return "page2";
}

in combination with the following entry in faces-config.xml:

<navigation-rule>
    <navigation-case>
        <from-outcome>page2</from-outcome>
        <to-view-id>/page2.jsf</to-view-id>
    </navigation-case>
</navigation-rule>

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.

public class ControllerBean1 {
    private DataBean dataBean;

    public String submit() {
        MyClass theClass = (MyClass) dataTable.getRowData();
        String theId = theClass.getIdentityNumber();
        dataBean.setTheId(theId);
        return "page2";
    }

    // ...
}

And

public class ControllerBean2 {
    private DataBean dataBean;

    // ...
}

You can access it in page2 like follows:

<h:outputText value="#{controllerBean2.dataBean.theId}" />

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.

BalusC
And how can page 2 get "theId" ?
Jojje
Just by accessing the bean property the usual JSF way like `#{bean.theId}`.
BalusC
It's just that page2 uses a different "ManagedBean" (or Controller) so that might be hard I think.
Jojje
You're thinking it the hard way, yes. Don't do that. Just access the same bean. If necessary just split the shared data out into another bean and inject it as managed property in the both "controller" beans.
BalusC
thanks for the nice suggestion/guide! I will try it shortly...
Jojje
Works nicely, thanks once again!
Jojje
You're welcome.
BalusC