Search code examples
spring-mvcservletsurl-mappingservlet-mapping

spring servlet-mapping / url-pattern


I have this servlet-mapping

<servlet-mapping>
        <servlet-name>devicesWeb</servlet-name>
        <url-pattern>*.do</url-pattern>
        <url-pattern>/device-catalogue</url-pattern>
        <url-pattern>/device-catalogue/</url-pattern>                
        <url-pattern>/device-catalogue/*</url-pattern>
        <url-pattern>/search/search.do</url-pattern>
</servlet-mapping>

With these 2 methods:

   @RequestMapping(value = "/device-catalogue", method = RequestMethod.GET)
        private String initForm(@ModelAttribute("searchForm") final SearchForm searchForm, 
                                 BindingResult result, HttpServletRequest request, Model model, Locale locale) {

            sessionHelper.checkSessionAttributes(request,  locale);
            return SEARCH_VIEW;
        }


@RequestMapping(value = { "/search/showProductDetails.do",  "/device-catalogue/{id}" },  method = { RequestMethod.GET, RequestMethod.POST })
    private String showProductDetails(@ModelAttribute("searchForm") final SearchForm searchForm, 
                              HttpServletRequest request, Model model, Locale locale) {

        StringTokenizer st = new StringTokenizer(StringEscapeUtils.escapeHtml(searchForm.getItemId()),"=");

        if (st.countTokens()>1) {

            String awardId=st.nextToken();
            String id=st.nextToken();

            Item item = deviceService.getItemByAwardId  (Long.parseLong(id), awardId);

            normalizeWebsiteURL (item);

            orderCountriesAvailability(item.getCountriesAvailability(), locale);

            model.addAttribute("item", encodeItemForHTML(item));    
        }
        return PRODUCT_DETAIL_VIEW;
    }

This URL works fine:

http://127.0.0.1:7001/eDevices/device-catalogue

But not this one (I got a 404) !

http://127.0.0.1:7001/eDevices/device-catalogue/16720

If I add this to the web.xml it works

<url-pattern>/product-catalogue/16720</url-pattern>

Solution

  • Don't write a <url-pattern> per url. Use the front controller of the spring mvc (DispatcherServlet) to be responsible for handling all application requests.

    In order to do that, in your web.xml you need something like:

    <servlet>
        <servlet-name>dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    </servlet>
    
    <servlet-mapping>
        <servlet-name>dispatcher</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
    

    and a dispatcher-servlet.xml beside that:

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">
    
       <context:component-scan base-package="com.test" /> <!-- specify your base package instead of "com.test" -->
    
       <mvc:annotation-driven />
    
       <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
              <property name="prefix" value="/WEB-INF/views/" />
              <property name="suffix" value=".jsp" />
              <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
       </bean>
    
       <mvc:resources mapping="/resources/**" location="WEB-INF/resources/" /> <!-- Not necessary but it's nice to address resources(css, images, etc) like this -->
    
    </beans>