1

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>

1 Answer 1

1

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>
Sign up to request clarification or add additional context in comments.

2 Comments

mmm..... Doing this it seems that I don't see all the images / css/ styles properly (?)
In your WEB-INF create a resources folder, inside that create css, images, fonts, etc folders. Now whenever you wanted to use any of these resources in your jsp pages, use c:url tag of jstl. For example showing an image will look like: <img src="<c:url value="/resources/images/logo.jpeg" />">

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.