1

web.xml

<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/spring/webmvc-config.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>dispatcher</servlet-name>
    <url-pattern>/*</url-pattern>
</servlet-mapping>

<welcome-file-list>
    <welcome-file>/</welcome-file>
</welcome-file-list>

/WEB-INF/spring/webmvc-config.xml

<bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
    <property name="mediaTypes">
        <map>
            <entry key="atom" value="application/atom+xml" />
            <entry key="html" value="text/html" />
            <entry key="json" value="application/json" />
        </map>
    </property>
    <property name="viewResolvers">
        <list>
            <bean class="org.springframework.web.servlet.view.BeanNameViewResolver" />
            <bean
                class="org.springframework.web.servlet.view.InternalResourceViewResolver">
                <property name="prefix" value="/WEB-INF/views/" />
                <property name="suffix" value=".jsp" />
            </bean>
        </list>
    </property>
    <property name="defaultViews">
        <list>
            <bean class="org.springframework.web.servlet.view.json.MappingJacksonJsonView" />
        </list>
    </property>
</bean>

Controller

@Controller
@RequestMapping ( "/" )
public class IndexController extends BaseController
{
    @RequestMapping ( "/" )
    public String index ( Model model ){
        System.out.println("AA");
        return index2(model);
    }

    @RequestMapping ( "/index" )
    public String index2 ( Model model ){
        System.out.println("BB");
        return "index";
    }
}

And exist index.jsp File

enter image description here

I guess that is very good working

BBBBBBBBBBBUUUUUUUUUTTTTTTTTT, BUT!

enter image description here

WHY???? WHY???? WHY???? WHY????

And More strange

enter image description here

??????????????????????????????????????????????????????????????????

Controller work it!! but don't display browser

What's going on?

Please help me.


And Log

DispatcherServlet with name 'dispatcher' processing GET request for [/WEB-INF/views/index.jsp]

No mapping found for HTTP request with URI [/WEB-INF/views/index.jsp] in DispatcherServlet with name 'dispatcher'

6
  • You should enable Spring logging, you should notice it log that it can't find a handler for /WEB-INF/views/index.jsp. Commented Aug 28, 2014 at 15:22
  • look at log. definitely must try to find the [/WEB-INF/views/index.jsp]. DispatcherServlet with name 'dispatcher' processing GET request for [/WEB-INF/views/index.jsp] No mapping found for HTTP request with URI [/WEB-INF/views/index.jsp] in DispatcherServlet with name 'dispatcher' Commented Aug 28, 2014 at 15:31
  • Now consider what /* means. Commented Aug 28, 2014 at 15:32
  • use servlet path "/", "/series/{id}", "/episode/{id}", "/user/{id}", but i don't want use multi servlet mapping. should never use only one Servlet mapping? Commented Aug 28, 2014 at 15:41
  • so i'm find looking exclude resource path Commented Aug 28, 2014 at 15:42

1 Answer 1

12

Servlet containers have rules for how they map and handle URI requests. These can be found in the Servlet Specification. It's also important to note that most Servlet containers have a Servlet to handle JSPs, mapped to *.jsp, which is an extension mapping. Tomcat has a JspServlet to do this.

You've mapped your DispatcherServlet to

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

which is a path mapping. Path mappings take precedence over extension mappings. So when you submit your view name

return "index";

Spring will use the ViewResolver

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

to resolve a path to use with a RequestDispatcher's forward method. That path will be /WEB-INF/views/index.jsp. Now the Servlet container will receive that path and attempt to find a Servlet to handle it. Since you have a Servlet mapped to /* it will use it, but your DispatcherServlet doesn't have a mapping for that path and therefore responds with a 404.

The simple solution is to change your mapping to /, which is the default handler if no other matches are found. In this case, when you submit your view and the container must find a mapped Servlet, it will find the JspServlet and use it.

Sign up to request clarification or add additional context in comments.

1 Comment

he is also mapping /index with "index" which is legal but strange

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.