Question

I am working on a web project that both Spring MVC 3.1.1 and Primefaces 3.4.2 are mixed together. Spring MVC is used to serve REST services(using URL annotations) and PrimeFaces is used for user interfaces. So naturally, I have one configuration file for both of these components.

Here is my problem:

index.xhtml is already set as welcome file in web.xml but I can't access the home page through http://localhost:8080/SampleWebApplication/

But I can access the home page through http://localhost:8080/SampleWebApplication/index.xhtml

What I want to achieve is I want to set index.xhtml file as the project's welcome file so that when the user enters http://localhost:8080/SampleWebApplication/ the user must be directed to the welcome page.

This is my web.xml file:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">

    <context-param>
        <param-name>javax.faces.PROJECT_STAGE</param-name>
        <param-value>Development</param-value>
    </context-param>

    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
        </param-value>
    </context-param>
    <context-param>
        <param-name>primefaces.THEME</param-name>
        <param-value>cupertino</param-value>
    </context-param>

    <servlet>
        <servlet-name>dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/config/applicationContext.xml</param-value>
        </init-param>
        <load-on-startup>2</load-on-startup>
    </servlet>

    <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>dispatcher</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
    <servlet-mapping>
        <servlet-name>Faces Servlet</servlet-name>
        <url-pattern>*.xhtml</url-pattern>
    </servlet-mapping>
    <session-config>
        <session-timeout>
            11
        </session-timeout>
    </session-config>
    <welcome-file-list>
        <welcome-file>index.xhtml</welcome-file>
    </welcome-file-list>
    <error-page> 
        <error-code>404</error-code> 
        <location>/error.jsp</location> 
    </error-page>
    <error-page> 
        <error-code>500</error-code> 
        <location>/error.jsp</location> 
    </error-page>
</web-app>
Was it helpful?

Solution

The welcome file request of / matches your Spring MVC servlet which is mapped on / and is therefore never hitting the JSF servlet which is mapped on *.xhtml.

You need to map your Spring MVC servlet on a more specific URL pattern. E.g. /rest/* or /api/* or whatever folder covers all those REST service resources. Once done that, the JSF servlet will be hit.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top