5


I try to configure simple Controller.

I have:
in web.xml

<servlet>
    <servlet-name>mvc-dispatcher</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>mvc-dispatcher</servlet-name>
    <url-pattern>/index.jsp</url-pattern>
</servlet-mapping>

in mvc-dispatcher-servlet.xml

<bean id="viewResolver"
      class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix">
        <value>/jsp/</value>
    </property>
    <property name="suffix">
        <value>.jsp</value>
    </property>
</bean>

<bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
    <property name="urlMap">
        <map>
            <entry key="/index.jsp">
                <ref bean="mainPage"/>
            </entry>
        </map>
    </property>
</bean>

<bean name="mainPage" class="ru.mypack.TBController" />

here is my Controller:

public class TBController extends AbstractController {

@Override
protected ModelAndView handleRequestInternal(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws Exception {
    System.out.println("It is here");
    ModelAndView model = new ModelAndView("index");
    return model;
}}

I run in on Tomcat 6 and in this configuration it (/index.jsp) works perfect!

But if I change url-pattern like this

<servlet-mapping>
    <servlet-name>mvc-dispatcher</servlet-name>
    <url-pattern>*.jsp</url-pattern>
</servlet-mapping>

it returns 404 accessing /index.jsp

I see "It is here" in console, it means url-pattern works fine but ModelAndView doesn't get initialized

Strange thing is that it looks like he tries to access empty resource (Chrome dysplays me "HTTP Status 404 - ")

Please, help me to understand what is going on.. May be I missed something in url-pattern specification?

UPD:
Thanks to Pavel Horal, the solution has been found.
I just replaced my url-pattern in web.xml with

<url-pattern>/test/*</url-pattern>

And now it responds by /test/index.jsp

Andrey Vlasenko
  • 127
  • 1
  • 2
  • 7
  • Please take a look at this thread. It should help. http://stackoverflow.com/questions/15385596/servlet-mapping-web-xml – smwikipedia Nov 05 '15 at 08:23

1 Answers1

6

Spring is working with the information how the servlet mapping is defined. If you are using suffix mapping (*.something), then Spring uses just the first part (without the suffix). This means you shuold map just /index in your url-pattern (without the suffix).

JavaDoc in Spring's UrlPathHelper#getPathWithinServletMapping gives a better description what is being used in the mapping process:

Return the path within the servlet mapping for the given request, i.e. the part of the request's URL beyond the part that called the servlet, or "" if the whole URL has been used to identify the servlet.

Detects include request URL if called within a RequestDispatcher include.

E.g.: servlet mapping = "/test/*"; request URI = "/test/a" -> "/a".

E.g.: servlet mapping = "/test"; request URI = "/test" -> "".

E.g.: servlet mapping = "/*.test"; request URI = "/a.test" -> "".

Pavel Horal
  • 17,782
  • 3
  • 65
  • 89
  • Thank you, now that's clear! I promise, next time I'll read JavaDoc more carefully :) – Andrey Vlasenko Jun 10 '13 at 07:14
  • 1
    Well this particular JavaDoc is not easy to find. This is internal Spring class. However what I love about Spring is, that even internal classes are well defined and documented (unlike in many other frameworks). – Pavel Horal Jun 10 '13 at 07:17
  • @PavelHoral Could you help me with this question? Thanks. http://stackoverflow.com/questions/33522888/how-does-the-getservletmapping-affects-the-url-in-spring-webmvc – smwikipedia Nov 04 '15 at 14:21