开发者

JSF move / redirect to other page with data

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 goi开发者_StackOverflow社区ng from one page to another in JSF (with data)?


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜