首页 > 代码库 > Spring MVC URL配置问题

Spring MVC URL配置问题

最近因为手误,两次遇到 访问Spring MVC 程序时不能正确解析JSP,返回类型是text/plain,后来终于发现问题的原因:

下面是web.xml中的 Dispatcher Servlet 配置,注意 <url-pattern>的配置

<!--spring mvc 这种方法不能正确访问,返回类型是text/plain-->
    <servlet>
        <servlet-name>Dispatcher Servlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>
                classpath:spring-mvc-config.xml
            </param-value>
        </init-param>
        <load-on-startup>2</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>Dispatcher Servlet</servlet-name>
        <url-pattern>/*</url-pattern>
    </servlet-mapping>
<!--spring mvc 这种方法可以正确访问,返回类型是text/html-->
    <servlet>
        <servlet-name>Dispatcher Servlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>
                classpath:spring-mvc-config.xml
            </param-value>
        </init-param>
        <load-on-startup>2</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>Dispatcher Servlet</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>


参与这两个位置:

http://my.oschina.net/ayesd/blog/278336

http://www.cnblogs.com/fangjian0423/p/servletContainer-tomcat-urlPattern.html#springmvc





Spring MVC URL配置问题