JSF2.0查看范围&重定向

发布于 2024-10-19 07:05:38 字数 2410 浏览 1 评论 0原文

我在 JSF2.0 中使用新的 ViewScope 时遇到以下问题。

我有一个类注释为带有 @PostConstruct 方法的视图范围 bean

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

    private String text = "myText";

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

在主页(/pages/main.xhtml)上有一个按钮可以导航到我输出的第二页(/pages/user/list.xhtml) UserListController bean 的属性“text”。

按钮是:

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

faces-config.xml 中的导航情况是:

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

当我使用 Firefox/Chrome 单击按钮时,我看到 @ViewScoped bean 的 @PostConstruct 方法被调用两次,而如果我使用 IE8,则调用该方法只有一个。

通过一个简单的相位跟踪器,我看到(在 FF/Chrome 示例中):

DEBUG - PhaseTracker               - 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

我做错了什么吗?

我从这个论坛看到,当包含组件绑定时,ViewScope 存在一个错误,但我的 bean 实际上只是一个字符串(当然问题来自一个非常复杂的示例,我尝试从数据库中加载一些数据) @PostConstruct 方法,但我尝试将示例减少到最低限度)

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

醉生梦死 2024-10-26 07:05:38

首先,导航规则在 JSF 2.0 中是可选的。我现在正在做一个相当大的项目,但我还没有使用它们。看看这对你有什么作用。

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

这就是我进行导航的方式,或者只是让一个方法将其作为字符串返回。

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


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

JSF 中的导航规则还有很多不足之处。如果您确实想要控制,请查找 Spring Web Flow 及其与 JSF 的集成。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文