【问题标题】:Why does the page change in JSF, but not the URL? [duplicate]为什么 JSF 中的页面发生了变化,而 URL 却没有? [复制]
【发布时间】:2013-09-10 11:46:18
【问题描述】:

我有简单的测试网络应用程序: 第一页 - index.xhtml:

<?xml version='1.0' encoding='UTF-8' ?>
<html>
 <h:head>
    <title>Facelet Title</title>
 </h:head>
 <h:body>
    First page
    <h:form>
        <h:commandButton value="Go to Next page" action="next"/>
    </h:form>
 </h:body>
</html>

和下一页 - next.xhtml:

<?xml version='1.0' encoding='UTF-8' ?>
<html>
<h:head>
    <title>Facelet Title</title>
 </h:head>
 <h:body>
    Next page
    <h:form>
        <h:commandButton value="Go to First page" action="index"/>
    </h:form>
 </h:body>
</html>

当我运行我的应用程序时,我在浏览器上有那个 url:

http://localhost:8080/Test/ 

并将 index.xhtml 作为第一页。

然后当我点击“转到下一页”时,我有网址

http://localhost:8080/Test/index.xhtml

和 next.xhtml 页面。当我点击“转到第一页”页面更改(到 index.xhtml)但它的 url

http://localhost:8080/Test/next.xhtml

web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app>
 <context-param>
     <param-name>javax.faces.PROJECT_STAGE</param-name>
     <param-value>Development</param-value>
 </context-param>
 <listener>
     <listener-class>org.apache.myfaces.webapp.StartupServletContextListener</listener-class>
 </listener>
 <servlet>
     <servlet-name>Faces Servlet</servlet-name>
     <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
     <load-on-startup>1</load-on-startup>
 </servlet>
 <servlet-mapping>
     <servlet-name>Faces Servlet</servlet-name>
     <url-pattern>*.xhtml</url-pattern>
 </servlet-mapping>
 <session-config>
     <session-timeout>
         30
     </session-timeout>
 </session-config>
 <welcome-file-list>
     <welcome-file>index.xhtml</welcome-file>
 </welcome-file-list>
</web-app>

我应该怎么做才能让我的应用程序的第一页有一个 URL,

http://localhost:8080/Test/index.xhtml 

而不是

http://localhost:8080/Test/

?

我使用 Tomcat (TomEE)/7.0.37

【问题讨论】:

    标签: jsf url jsf-2 navigation


    【解决方案1】:

    虽然这是一个旧线程,但如果有人仍在寻找它,可以尝试“?faces-redirect=true”。喜欢:

    <h:commandButton value="Go to Next page" action="next?faces-redirect=true"/>
    

    【讨论】:

      【解决方案2】:

      您正在使用命令按钮(POST 表单提交按钮)进行简单的逐页导航。这是完全错误的。您应该为此使用普通按钮(GET 导航按钮)。

      替换

      <h:form>
          <h:commandButton value="Go to Next page" action="next"/>
      </h:form>
      

      通过

      <h:button value="Go to Next page" outcome="next" />
      

      注意:您根本不需要&lt;h:form&gt;

      您的“一个 URL 后面”的具体问题是因为您看到 &lt;h:form&gt; 本身的 URL 反映在浏览器地址栏中,而不是表单提交结果页面的 URL。在您的网络浏览器中打开 JSF 页面,右键单击并查看源代码,然后仔细查看 &lt;form action&gt; 值。

      另见:

      【讨论】:

      • 好的,但是当我使用 时,安全约束不起作用?
      • 这个问题是由其他地方引起的。也许你误解了这个问题?安全约束不会应用于&lt;h:commandButton&gt; 转发的页面。也许您忘记将&lt;h:commandButton&gt; 实际替换为&lt;h:button&gt;?如果仍然卡住,请按“”按钮。这与当前的问题完全无关。
      • 好的,这是我的错误,没有使会话无效。最后一个问题:当我使用 &lt;h:button&gt; 时,我无法在托管 bean 中执行任何操作?我必须使用&lt;h:form&gt;&lt;h:commandButton&gt;
      猜你喜欢
      • 1970-01-01
      • 2017-11-09
      • 2014-06-26
      • 1970-01-01
      • 1970-01-01
      • 2012-12-15
      • 1970-01-01
      • 2021-10-12
      • 2016-02-11
      相关资源
      最近更新 更多