web.xml with different files in welcome-file-list

2019-06-14 08:18发布

问题:

i'm starting to use JSF and I'm a bit lost. I'm developping a JSF application and I have two enter points.

Some times I need my browser to open a page named mydata.xhtml and another times I need to open a page named dataexchange.xhtml.

Now I have done the first one, and the application works fine. I think I have to change my web.xml file, isn`t it?

My web.xml is this.

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd">
    <context-param>
        <param-name>javax.faces.PROJECT_STAGE</param-name>
        <param-value>Development</param-value>
    </context-param>
    <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>/faces/*</url-pattern>
    </servlet-mapping>
    <session-config>
        <session-timeout>
            30
        </session-timeout>
    </session-config>
    <welcome-file-list>
        <welcome-file>faces/mydata.xhtml</welcome-file>
    </welcome-file-list>
</web-app></pre>

回答1:

I dont think there is an option of if-else logic in web.xml

   <welcome-file-list>
        <welcome-file>faces/mydata.xhtml</welcome-file>
        <welcome-file>faces/dataexchange.xhtml</welcome-file>
    </welcome-file-list>

If you put both the files in welcome file list as above, the container will first search for mydata.xhtml and only if not found, will the container search and return dataexchange.

Alternatively, you can have a DispatcherServlet (

Either some framework provided or custom servlet with load on startup 1

) and from the servlet you can return the xhtml you want to.