开发者

JSF2.0 View Scope & Redirect

开发者 https://www.devze.com 2023-02-14 01:15 出处:网络
I have the follwing problem with the new ViewScope in JSF2.0. I have a class annotated as a view scope bean with a @PostConstruct method

I have the follwing problem with the new ViewScope in JSF2.0.

I have a class annotated as a view scope bean with a @PostConstruct method

@ManagedBean(name = "userListController")
@ViewScoped
public class UserListController {

    private String text = "myText";

    @PostConstruct
    public void init() {
       System.out.println("init") ;
    }
}

On the main page (/pages/main.xhtml) there is a button to navigate to a second page (/pages/user/list.xhtml) where I output the property "text" of the UserListController bean.

The button is:

<h:commandButton value="Manage Users" action="gotoUsers"/>

the Navigation case in faces-config.xml is:

<navigation-rule>
    <from-view-id>/pages/main.xhtml</from-view-id>
    <navigation-case>
        <from-outcome>gotoUsers</from-outcome>
        <to-view-id>/pages/user/list.xhtml</to-view-id>
        <redirect/>
    </navigation-case>
</navigation-rule>

When I click on the button using Firefox/Chrome I see that the @PostConstruct method of the @ViewScoped bean is called twice, while if I use IE8 the method is called just one.

With a simple phase tracker I see (in FF/Chrome example) :

DEBUG - PhaseTra开发者_Go百科cker               - BEFORE - RESTORE_VIEW 1
DEBUG - PhaseTracker               - AFTER - RESTORE_VIEW 1
DEBUG - PhaseTracker               - BEFORE - APPLY_REQUEST_VALUES 2
DEBUG - PhaseTracker               - AFTER - APPLY_REQUEST_VALUES 2
DEBUG - PhaseTracker               - BEFORE - PROCESS_VALIDATIONS 3
DEBUG - PhaseTracker               - AFTER - PROCESS_VALIDATIONS 3
DEBUG - PhaseTracker               - BEFORE - UPDATE_MODEL_VALUES 4
DEBUG - PhaseTracker               - AFTER - UPDATE_MODEL_VALUES 4
DEBUG - PhaseTracker               - BEFORE - INVOKE_APPLICATION 5
DEBUG - PhaseTracker               - AFTER - INVOKE_APPLICATION 5
DEBUG - PhaseTracker               - BEFORE - RESTORE_VIEW 1
DEBUG - PhaseTracker               - AFTER - RESTORE_VIEW 1
DEBUG - PhaseTracker               - BEFORE - RENDER_RESPONSE 6
Init.
DEBUG - PhaseTracker               - AFTER - RENDER_RESPONSE 6
DEBUG - PhaseTracker               - BEFORE - RESTORE_VIEW 1
DEBUG - PhaseTracker               - AFTER - RESTORE_VIEW 1
DEBUG - PhaseTracker               - BEFORE - RENDER_RESPONSE 6
Init.
DEBUG - PhaseTracker               - AFTER - RENDER_RESPONSE 6

Am I doing something wrong?

I see from this forum that there is a bug with ViewScope when a component binding is included, but my bean is really only a string (of course the problem come from a much complex example, where I try to load some data from DB in the @PostConstruct method, but I've tried to reduce the example to the bare minimum)


First of all navigation rules are optional in JSF 2.0. I have a pretty big project I'm working on right now and I have not used them. See what this does for you.

<h:commandButton value="Manage Users" action="list?faces-redirect=true"/>

This is how I do my navigation or simply have a method return that as a string.

<h:commandButton value="Manage Users" action="#{backingBean.doList}"/>


  public String doList() {
 return "list?faces-redirect=true";
}

The navigation rules in JSF leave a lot to be desired. If you really want control, look up Spring Web Flow and it's integration with JSF.

0

精彩评论

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