Search code examples
angularjsjspspring-mvcresolverrequest-mapping

Spring MVC 4.2.4 / Controller with RequestMapping("/**") and static resources


I have an angular application which uses ui-router, and is served by a SpringMvc (4.2.4) java app. I decided to map any requests to a single Controller/method which loads the single JSP page of my project.

But, When I try to add static resources mappings, to load js and css files, those static resources are ignored... Every requests, inside the [mayapp]/resources/* path leads to my single jsp page.

Here is my config :

web.xml

      <servlet>
        <servlet-name>mvc-dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
          <param-name>contextConfigLocation</param-name>
          <param-value>classpath:spring/servlet.xml</param-value>
        </init-param>
      </servlet>
      <servlet-mapping>
        <servlet-name>mvc-dispatcher</servlet-name>
        <url-pattern>/</url-pattern>
      </servlet-mapping>

servlet.xml

      <context:component-scan base-package="com.adveasys.omrh.front.web" />
      <mvc:annotation-driven></mvc:annotation-driven>

      <mvc:resources mapping="/resources/**" location="/resources/">
       <mvc:resource-chain resource-cache="false" auto-registration="false">
         <mvc:resolvers>
           <bean class="org.springframework.web.servlet.resource.GzipResourceResolver"/>
           <bean class="org.springframework.web.servlet.resource.PathResourceResolver"/>
         </mvc:resolvers>
       </mvc:resource-chain>
     </mvc:resources> 

       <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
         <property name="prefix" value="/WEB-INF/pages/" />
         <property name="suffix" value=".jsp" />
      </bean>

    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
      <property name="prefix" value="/WEB-INF/pages/" />
      <property name="suffix" value=".jsp" />
    </bean>

MainController.java

@RequestMapping("/**")
public ModelAndView mainPage(HttpServletRequest request) throws JsonProcessingException {

Before having this problem, I used the default servlet in web.xml for every /resources/*, But I am generating a .gz version of my scripts/css with maven and I want this kind of configuration to be able to user the GzipResourceResolver.


Things I already tried, and did not work

  • declare 2 distincts DispatcherServlets in web.xml, one for JSP only, the other one only for resources. (I splitted the mvc configuration into 2 different files for each servlets)
  • @RequestMapping("/") in the controller, the user must enter the website at the root address, not acceptable.
  • I tried to set an order on and as a property for InternalResourceViewResolver

Thanks in advance.


Solution

  • Alright, after digging in debug mode. HandlerMappings where in this order :

    • RequestMappingHandlerMapping (the @Controller one, inner property "order" = 0)
    • SimpleUrlHandlerMapping (the resources one, inner property "order" = 0)
    • BeanNameUrlHandlerMapping (don't know what it is ... ^^)

    When calling a /resource/* file the RequestMappingHandlerMapping was the first to respond as a valid candidate.

    after adding such an order in Spring configuration :

    <mvc:resources mapping="/resources/**" location="/resources/" order = "-1">
    

    It worked.