返回到支持bean中的同一视图,我应该返回null还是空字符串或视图ID? [英] Returning to same view in backing bean, should I return null or empty string or view ID?

查看:112
本文介绍了返回到支持bean中的同一视图,我应该返回null还是空字符串或视图ID?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

signuplogincommandButton在其后备bean中调用其方法时,我想在成功时重定向到index,并在失败时保持相同的视图.一切正常,如果成功,我将返回"index?faces-redirect=true".我现在遇到了失败的情况,我有不同的可能性:我可以返回"signup"(或"login"),""也可以工作,甚至当我返回null时,我也会得到相同的结果.至少我找不到任何差异.

When my commandButtons for signup and login call their methods in their backing beans I would like to redirect to index when succeeded, and stay on same view when fails. That all works fine, I return "index?faces-redirect=true" on success. I case of a fail I realized now, that I have different possibilities: I could return "signup" (or "login"), also "" works, and even when I return null I get the same result. At least I can not detect any differences.

当结果应该是相同的视图时,是否有共同的风格返回什么?

Is there a common style what to return, when the result should be the same view?

推荐答案

我可以返回"signup"(或"login"),""也可以,即使返回null,我也会得到相同的结果.至少我找不到任何差异.

I could return "signup" (or "login"), also "" works, and even when I return null I get the same result. At least I can not detect any differences.

null和非null结果方面实际上存在技术差异. null(或void)结果将重用相同的视图.非null结果将丢弃当前视图并创建一个全新的视图,即使它返回相同的视图标识符也是如此.这里的视图"是UIViewRoot实例,包括其所有状态.空字符串代表当前视图ID".

There is actually a technical difference with regard to null and non-null outcomes. The null (or void) outcome will reuse the same view. A non-null outcome will trash the current view and create a brand new view, even if it returns the same view identifier. The "view" is here the UIViewRoot instance including all of its state. The empty string represents "current view ID".

值得注意的是,@ViewScoped bean受此影响.它们将被丢弃并与视图一起重新创建.但是,如果您使用的是@RequestScoped bean,那么您实际上不会注意到bean行为方面的任何技术差异.当视图被删除时,它不会被删除,而仅在请求结束时才被删除.换句话说,即使在同一请求中重新创建了相同的视图,也将在其上使用相同的@RequestScoped bean.

Notably @ViewScoped beans are affected by this. They will be trashed and recreated along with the view. If you're however using a @RequestScoped bean, then you indeed won't notice any technical difference as to bean behavior. It won't be trashed when the view gets trashed, but only when the request ends. In other words, even when the same view is recreated within the same request, the same @RequestScoped bean will be used on it.

正确的方法取决于具体的功能要求.如果要返回相同的视图,只需返回nullvoid(非空字符串).在这种情况下,通常无需创建新视图.仅当您在当前视图中具有视图构建时间标签(JSTL和好友)时,其模型值已在bean操作方法中进行了更改,才需要通过返回空字符串来强制重建视图.如果要导航到其他视图,请返回所需的视图标识符,最好与faces-redirect=true一起返回,以确保它是否等幂(可标记).

The right approach depends on the concrete functional requirements. If you want to return to the same view, just return null or void (not empty string). It's generally unnecessary to create a new view in such case. Only if you're having view build time tags (JSTL and friends) in current view whose model values have been changed in bean action method, then you'd need to force a rebuild of the view by returning an empty string. If you want to navigate to a different view, return the desired view identifier, preferably along with faces-redirect=true, for sure if it's idempotent (bookmarkable).

  • How to navigate in JSF? How to make URL reflect current page (and not previous one)
  • Difference between View and Request scope in managed beans
  • What's the view build time?

这篇关于返回到支持bean中的同一视图,我应该返回null还是空字符串或视图ID?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆