How to set JSF2 <welcome-file> with faces servlet mapping?

落花浮王杯 提交于 2019-12-18 03:11:29

问题


I have a simple JSF2 app using Facelets. My web.xml is set as follows:-

<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>*.jsf</url-pattern>
</servlet-mapping> 

I want to set the app to default to the welcome page which is /pages/login.xhtml. I can't seem to get the welcome-file setting to pick up this when going to the context root in the browser.

My web.xml welcome setting is as follows:-

<welcome-file-list>
<welcome-file>/pages/login.jsf</welcome-file>
</welcome-file-list>

How do I force the welcome-page to goto the login page and ensure it goes through the faces servlet?

If I use the following the page displays but it hasn't gone through the faces servlet and so faces components aren't rendered:-

<welcome-file-list>
<welcome-file>pages/login.xhtml</welcome-file>
</welcome-file-list>

Any help appreciated!


回答1:


You have to create an empty login.jsf file, adjacent to your login.xhtml file. For some reason this hack is needed to trick the servlet container into thinking your welcome file exists.

Note: The response served will still be the JSF response using your login.xhtml file, not the empty login.jsf file you create.




回答2:


Additional information to original solution.

This is only a backwards compatibility need for servlet containers prior to version 3. Once running under Glassfish 3.1 or Tomcat 7, for instance, it is no longer required to have an empty file like this. I ran tests to verify using Tomcat 6 and Tomcat 7. In the past I had ran tests with Glassfish and assumed a Tomcat 6 issue was at play since we run our software in Tomcat 6 at the current time and it is not 3.0 servlet container. I had been using a JSP page with redirect but after moving over completely to facelets I disliked having one left over JSP page. This is a great solution for the near term until we are running in a 3.0 servlet container. The real reason it works is that all is being done with the WEB.XML entry is a check on the file existence. The real work is handled off to JSF as it catches the request to the faces page thus it never hits that empty page for anything.



来源:https://stackoverflow.com/questions/4616493/how-to-set-jsf2-welcome-file-with-faces-servlet-mapping

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!