问题描述
当我的 commandButton
用于 signup
和 login
在他们的支持 bean 中调用他们的方法时,我想重定向到 index
成功时,并在失败时保持在同一视图上.一切正常,我返回 "index?faces-redirect=true"
成功.我现在意识到失败的情况,我有不同的可能性:我可以返回 "signup"
(或 "login"
),也可以返回 ""
有效,即使我返回 null
我也会得到相同的结果.至少我无法发现任何差异.
When my commandButton
s 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?
推荐答案
关于 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.
正确的方法取决于具体的功能要求.如果要返回相同的视图,只需返回 null
或 void
(不是空字符串).在这种情况下,通常不需要创建新视图.仅当您在当前视图中有视图构建时间标记(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).
这篇关于返回支持 bean 中的相同视图,我应该返回 null 或空字符串还是视图 ID?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!