Spring:servlet-mapping - > url-pattern:/ *工作但无法显示 [英] Spring: servlet-mapping -> url-pattern : /* working but can't display

查看:170
本文介绍了Spring:servlet-mapping - > url-pattern:/ *工作但无法显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

/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
@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";
    }
}

并且存在index.jsp文件

And exist index.jsp File

我想这是非常好的工作

BBBBBBBBBBBUUUUUUUUTTTTTTTT,但是

为什么? ???
为什么????
为什么????
为什么????

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

更奇怪

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

控制器工作吧!!但是不显示浏览器

Controller work it!! but don't display browser

发生了什么?

请帮助我。

和日志

名为'dispatcher'的DispatcherServlet处理[/ WEB的GET请求-INF / views / index.jsp]

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

在DispatcherServlet中找不到带有URI [/WEB-INF/views/index.jsp]的HTTP请求的映射,名称为dispatcher '

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

推荐答案

Servlet容器有关于如何映射和处理URI请求的规则。这些可以在 Servlet规范中找到。同样重要的是要注意大多数Servlet容器都有 Servlet 来处理JSP,映射到 * .jsp ,这是扩展映射。 Tomcat有 JspServlet 来执行此操作。

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.

您已映射 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将使用 ViewResolver

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>

解析与 RequestDispatcher 的转发方法。该路径将是 /WEB-INF/views/index.jsp 。现在,Servlet容器将接收该路径并尝试查找 Servlet 来处理它。由于你有 Servlet 映射到 / * 它会使用它,但你的 DispatcherServlet 没有该路径的映射,因此以404响应。

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.

简单的解决方案是将映射更改为 / ,如果没有找到其他匹配项,则为默认处理程序。在这种情况下,当您提交视图并且容器必须找到映射的 Servlet 时,它将找到 JspServlet 和使用它。

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.

这篇关于Spring:servlet-mapping - &gt; url-pattern:/ *工作但无法显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆