为什么页面在JSF中更改,而不是URL?

kub*_*a44 7 navigation url jsf jsf-2

我有简单的测试网络应用程序:第一页 - 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>
Run Code Online (Sandbox Code Playgroud)

和下一页 - 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>
Run Code Online (Sandbox Code Playgroud)

当我运行我的应用程序时,我在浏览器上有该URL:

http://localhost:8080/Test/ 
Run Code Online (Sandbox Code Playgroud)

和index.xhtml作为第一页.

然后当我点击"转到下一页"我有网址

http://localhost:8080/Test/index.xhtml
Run Code Online (Sandbox Code Playgroud)

和next.xhtml页面.当我点击"转到第一页"页面更改(到index.xhtml)但是url它

http://localhost:8080/Test/next.xhtml
Run Code Online (Sandbox Code Playgroud)

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>
Run Code Online (Sandbox Code Playgroud)

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

http://localhost:8080/Test/index.xhtml 
Run Code Online (Sandbox Code Playgroud)

而不是

http://localhost:8080/Test/
Run Code Online (Sandbox Code Playgroud)

我使用Tomcat(TomEE)/7.0.37

Bal*_*usC 7

您正在使用命令按钮(POST表单提交按钮)进行纯页面到页面导航.这是完全错误的.您应该使用普通按钮(GET导航按钮).

更换

<h:form>
    <h:commandButton value="Go to Next page" action="next"/>
</h:form>
Run Code Online (Sandbox Code Playgroud)

通过

<h:button value="Go to Next page" outcome="next" />
Run Code Online (Sandbox Code Playgroud)

注意:你根本不需要<h:form>.

您的"一个URL后面"的具体问题是由于您看到<h:form>自身的URL 反映在浏览器地址栏中而不是表单提交的结果页面的URL.在Web浏览器中打开JSF页面,右键单击" 查看源代码"并查看<form action>值.

也可以看看:


Gou*_*der 7

虽然这是一个老线程,如果有人仍在寻找它可以尝试"?faces-redirect = true".喜欢:

<h:commandButton value="Go to Next page" action="next?faces-redirect=true"/>
Run Code Online (Sandbox Code Playgroud)


归档时间:

查看次数:

11375 次

最近记录:

8 年,5 月 前