【问题标题】:PrettyFaces url routing from form submit来自表单提交的 PrettyFaces url 路由
【发布时间】:2014-09-24 23:59:30
【问题描述】:

我正在使用 Pretty Faces 进行 URL 重写,以便能够重用一些 xhtml 文件。我想让像'/honda/index.xhtml'和'/toyota/index.xhtml'这样的URL都进入同一个/make/index.xhtml文件,make作为参数进入。这个路由似乎可以在这样的配置中正常工作:

<url-mapping id="carMake">
    <pattern value="/#{make}/index.xhtml"></pattern>
    <view-id value="/make/index.xhtml"/>
</url-mapping>

对于搜索结果类型的页面,我也有此映射:

<url-mapping id="search">
    <pattern value="/#{make}/search/index.xhtml" />
    <view-id value="/search/index.xhtml" />
</url-mapping>

当我手动将 URL 放入浏览器时,这两项工作都按预期工作。

当我尝试将表单放在第一页时遇到问题,我想重定向到第二页。我有一个表单的这个 jsf xhtml 代码:

<h:form>
    <h:messages />
    <h:inputText id="searchTerm"/>
    <h:commandButton value="search" action="/honda/search/index.xhtml?faces-redirect=true"/>
</h:form>

(硬编码/honda这里是为了简化例子)

当我尝试提交此搜索时,它会弹回相同的 /honda/index.xhtml,页面上没有显示任何消息。

日志显示:

09-23 11:39:55 DEBUG PrettyNavigationHandler:57 - Navigation requested: fromAction [/honda/search/index.xhtml?faces-redirect=true], outcome [/honda/search/index.xhtml?faces-redirect=true]
09-23 11:39:55 DEBUG PrettyNavigationHandler:60 - Not a PrettyFaces navigation string - passing control to default nav-handler

我已经尝试不使用 faces-redirect 参数,但得到了相同的结果。

为什么当我直接将 /honda/search/index.xhtml 放入浏览器时它可以工作,而不是作为操作的结果?

【问题讨论】:

    标签: jsf-2 prettyfaces


    【解决方案1】:

    如果你想将 URL 与 View ID 分开,并且你不想在应用程序中引用 view-id,那么你需要使用漂亮的导航字符串:

    <h:commandButton value="search" action="pretty:honda"><f:param name="make" value="honda" /></h:commandLink>

    但这真的只是让它变得比它需要的更复杂。我建议按照@chkal 的建议做,除了他的例子有点错误。应该是:

    <h:commandButton value="search" action="/search/index.xhtml?faces-redirect=true&make=honda"/>

    这应该包含在文档中 :) http://ocpsoft.org/docs/prettyfaces/3.3.3/en-US/html/components.html#components.prettylink 检查该部分(以及它下面的部分),看看是否有帮助!

    【讨论】:

      【解决方案2】:

      您不能以这种方式使用漂亮的 URL 作为 action 属性的值。您必须使用标准的 JSF 结果,并将 make 作为查询参数。

      试试这个:

      <h:commandButton value="search" 
                 action="/honda/search/index.xhtml?faces-redirect=true&make=honda"/>
      

      【讨论】:

      • 这是一个很好的答案,虽然不是我所希望的,因为它违背了我希望摆脱 PrettyFaces 的目的
      • 如果你想将 URL 与 View ID 分开,并且你不想在应用程序中引用 view-id,那么你需要使用漂亮的导航字符串:&lt;h:commandButton value="search" action="pretty:honda"&gt;&lt;f:param name="make" value="honda" /&gt;&lt;/h:commandLink&gt;,但这真的只是让它变得比它需要的更复杂。我建议按照@chkal 的建议做,除了他的例子有点错误。应该是:&lt;h:commandButton value="search" action="/search/index.xhtml?faces-redirect=true&amp;make=honda"/&gt;
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-05-28
      • 1970-01-01
      • 2021-10-02
      • 1970-01-01
      • 2018-02-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多