JSF 2 - 存在哪些方法可以找到返回调用页面?
我的 Web 应用程序有两个搜索页面 - search1.jsf 和 search2.jsf - 它们共享同一个 bean,而该 bean 又调用结果页面 - result.jsf。
所以两个搜索页面都有一个 commandButton
<h:commandButton value="Commit" action="#{search.commit}" />
然后“搜索”bean 处理搜索并使用以下命令转发到结果页面
target = "result?faces-redirect=true";
现在 result.jsf 不知道使用了哪个搜索页面。如何传递调用页面,以便被调用页面能够呈现返回调用页面的链接?
使用浏览器后退按钮(或 JavaScript 代码)返回历史记录的缺点是,用户在浏览结果后,只能返回结果中的一页,而不是“向上”进入搜索参数表单。
到目前为止我看到的选项是:
使用
或 action="#{search.commit(name_of_calling_page)} 将调用者页面名称通过链传递到结果页面put 将调用者页面名称通过链传递到结果页面将所有“搜索参数”表单放在一个 JSF 页面中,并有条件地切换内容
或者是否有一个 JSF 函数返回调用 JSF 页面的名称?
My web application has two search pages - search1.jsf and search2.jsf - which share the same bean which in turn calls a result page - result.jsf.
So both search pages have a commandButton
<h:commandButton value="Commit" action="#{search.commit}" />
The "search" bean then processes the search and forwards to the result page using
target = "result?faces-redirect=true";
Now the result.jsf doesn't know which search page has been used. How can I pass the calling page so that the called page is able to render a link back to the calling page?
Using the browser back button (or Javascript code) to go back in the history has the disadvantage that the user, after browsing through the results, only would go back one page in the results instead of going 'up' to the search parameter form.
Options I see so far are:
pass the caller page name through the chain to the result page, using
<f:param>
or action="#{search.commit(name_of_calling_page)}put all 'search parameter' forms in one JSF page and conditionally switch the content
Or is there a JSF functions which returns the name of the calling JSF page?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您正在重定向到结果页面,这表明您正在使用会话范围的 bean 来保存结果(请求/视图范围根本无法在重定向中幸存)。忽略这是一种糟糕/奇怪的方法这一事实,您也可以将搜索类型存储在该 bean 中吗?
无论如何,最简单、最干净的方法是使用单个主视图和单个主视图作用域 bean,并有条件地显示
search1.xhtml
、search2.xhtml
和results.xhtml
在主视图中使用
。search.xhtml
只要您在操作方法中返回
void
或null
,相同的视图作用域 bean 就会在每个后续(以及非-重定向!)请求。如果您打算将其设为 GET 而不是 POST(以便可以添加书签并可通过后退按钮导航,例如 Google),那么您实际上应该更改表单和视图参数以使其成为 GET 而不是 POST。You're redirecting to the result page which suggests that you're using a session scoped bean to hold the results (a request/view scoped simply doesn't survive redirects). Ignoring the fact that this is a poor/odd approach, you could just store the search type in that bean as well?
Anyway, easiest and cleanest is to use a single master view with a single master view scoped bean and conditionally display
search1.xhtml
,search2.xhtml
andresults.xhtml
using<ui:include>
in the master view.search.xhtml
As long as you return
void
ornull
in action methods, the same view scoped bean will be retained on every subsequent (and non-redirected!) request. If you intend to make it a GET instead of POST (so that it's bookmarkable and back-button-navigable such as Google), then you should actually change the forms and view parameters to make it GET instead of POST.