如何在JSF中导航?如何使URL反映当前页面(而不是上一页) [英] How to navigate in JSF? How to make URL reflect current page (and not previous one)

查看:71
本文介绍了如何在JSF中导航?如何使URL反映当前页面(而不是上一页)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在学习JSF,当我意识到无论何时我们使用<h:form>时,JSF的标准行为都是总是向我显示 previous 页面的URL,这让我感到惊讶和困惑.浏览器,而不是 current 页面的URL.

I am currently learning JSF and was rather amazed and puzzled when I realized that whenever we use <h:form>, the standard behavior of JSF is to always show me the URL of the previous page in the browser, as opposed to the URL of the current page.

我了解这与JSF始终将表单发布到同一页面,然后仅将控制器提供给它的任何页面呈现给不知道页面位置已更改的浏览器的方式有关.

I understand that this has to do with the way JSF always posts a form to the same page and then just renders whatever page the controller gives it back to the browser which doesn't know the page location has changed.

似乎JSF已经存在了很长时间,因此必须有一种干净,可靠的方法来解决这个问题.如果是这样,您愿意分享吗?

我找到了各种解决方法,但遗憾的是,没有什么是真正可靠的解决方案.

I have found various workarounds, but sadly nothing that seems like a real solid solution.

  • 仅接受该网址具有误导性.
  • "?faces-redirect=true"附加到每个 bean动作的返回值,然后
    • 弄清楚如何用其他内容(Flash Scope,CDI对话,@ SessionScoped等)替换@RequestScoped.
    • 接受每个用户操作的两次HTTP往返.
    • Simply accept that the URL is misleading.
    • Append "?faces-redirect=true" to the return value of every bean's action and then
      • figure out how to replace @RequestScoped with something else (Flash Scopes, CDI conversation, @SessionScoped, ...).
      • accept to have two HTTP round trips for every user action.

      如果"?faces-redirect=true"很好,是否有办法配置整个应用程序以这种方式处理所有请求?

      If "?faces-redirect=true" is as good as it gets, is there a way do configure an entire application to treat all requests this way?

      推荐答案

      实际上,JSF作为基于表单的面向应用程序的MVC框架,将POST表单提交到与请求带有<h:form>的页面相同的URL.形式.您可以通过查看生成的HTML输出的<form action> URL进行确认.这是Web开发术语,称为 postback .默认情况下,回发导航不会引起对新URL的新请求,而是将目标页面加载为响应的内容.当您只想要页面到页面的导航时,这确实令人困惑.

      Indeed, JSF as being a form based application targeted MVC framework submits the POST form to the very same URL as where the page with the <h:form> is been requested form. You can confirm it by looking at the <form action> URL of the generated HTML output. This is in web development terms characterized as postback. A navigation on a postback does by default not cause a new request to the new URL, but instead loads the target page as content of the response. This is indeed confusing when you merely want page-to-page navigation.

      通常,有关导航/重定向的正确方法取决于业务需求和幂等 (请参阅:可标记性")(注意:有关具体代码示例,请参见下面的另请参阅"链接).

      Generally, the right approach as to navigation/redirection depends on the business requirements and the idempotence (read: "bookmarkability") of the request (note: for concrete code examples, see the "See also" links below).

      • 如果请求是幂等的,只需使用GET表单/链接而不是POST表单(即,使用<a><form><h:link><h:button>代替<h:form><h:commandXxx> ).
        例如,页面到页面导航,类似Google的搜索表单等.

      • If the request is idempotent, just use a GET form/link instead of POST form (i.e. use <a>, <form>, <h:link> or <h:button> instead of <h:form> and <h:commandXxx>).
        For example, page-to-page navigation, Google-like search form, etc.

      如果请求不是幂等的,则仅在同一视图中有条件地显示结果(即从操作方法返回nullvoid并使用例如<h:message(s)>和/或rendered) .
      例如,页内数据输入/编辑,多步向导,模式对话框,确认表单等.

      If the request is non-idempotent, just show results conditionally in the same view (i.e. return null or void from action method and make use of e.g. <h:message(s)> and/or rendered).
      For example, in-page data entry/edit, multi-step wizard, modal dialog, confirmation form, etc.

      如果请求不是等幂的,但是目标页面是等幂的,则仅在POST之后发送重定向(即从action方法返回?faces-redirect=true的结果,或者手动调用ExternalContext#redirect()或放入(在旧版XML导航的情况下).
      例如,成功编辑后显示所有数据的列表,登录后重定向等.

      If the request is non-idempotent, but the target page is idempotent, just send a redirect after POST (i.e. return outcome with ?faces-redirect=true from action method, or manually invoke ExternalContext#redirect(), or put <redirect/> in legacy XML navigation case).
      For example, showing list of all data after successful editing, redirect after login, etc.

      请注意,纯页面到页面的导航通常是幂等的,这是许多JSF初学者由于滥用命令链接/按钮而失败,然后抱怨URL不变的地方.还要注意,导航用例很少用于针对SEO/UX开发的现实应用程序中,而这正是许多JSF教程由于让读者不相信而失败的地方.

      Note that pure page-to-page navigation is usually idempotent and this is where many JSF starters fail by abusing command links/buttons for that and then complain afterwards that URLs don't change. Also note that navigation cases are very rarely used in real world applications which are developed with respect to SEO/UX and this is where many JSF tutorials fail by letting the readers believe otherwise.

      还要注意,使用POST绝对不比GET更安全",因为请求参数在URL中不是立即可见的.它们仍然在HTTP请求正文中可见并且仍然可以操作.因此,出于安全性"的考虑,绝对没有理由选择POST进行幂等请求.真正的安全性在于使用HTTPS而非HTTP并检查业务服务方法(如果当前登录的用户被允许查询实体X或操纵实体X等).一个不错的安全框架为此提供了注释.

      Also note that using POST is absolutely not "more secure" than GET because the request parameters aren't immediately visible in URL. They are still visible in HTTP request body and still manipulatable. So there's absolutely no reason to prefer POST for idempotent requests for the sake of "security". The real security is in using HTTPS instead of HTTP and checking in business service methods if currently logged-in user is allowed to query entity X, or to manipulate entity X, etc. A decent security framework offers annotations for this.

      • What is the difference between redirect and navigation/forward and when to use what?
      • JSF implicit vs. explicit navigation
      • What URL to use to link / navigate to other JSF pages
      • Bookmarkability via View Parameters feature
      • What can <f:metadata>, <f:viewParam> and <f:viewAction> be used for?
      • When should I use h:outputLink instead of h:commandLink?
      • Creating master-detail pages for entities, how to link them and which bean scope to choose
      • Retaining GET request query string parameters on JSF form submit
      • Pass an object between @ViewScoped beans without using GET params

      这篇关于如何在JSF中导航?如何使URL反映当前页面(而不是上一页)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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