java Spring: servlet-mapping -> url-pattern : /* 工作但无法显示

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25552450/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 08:13:07  来源:igfitidea点击:

Spring: servlet-mapping -> url-pattern : /* working but can't display

javaspringjspspring-mvcservlets

提问by Jeong SangCheol

web.xml

网页.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.MappingHymansonJsonView" />
        </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

并存在 index.jsp 文件

enter image description here

在此处输入图片说明

I guess that is very good working

我想这很好用

BBBBBBBBBBBUUUUUUUUUTTTTTTTTT, BUT!

BBBBBBBBBBBUUUUUUUUTTTTTTTTT,但是!

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]

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

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

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

回答by Sotirios Delimanolis

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 Servletto handle JSPs, mapped to *.jsp, which is an extension mapping. Tomcat has a JspServletto do this.

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

You've mapped your DispatcherServletto

你已经映射DispatcherServlet

<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

Spring 将使用 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 forwardmethod. That path will be /WEB-INF/views/index.jsp. Now the Servlet container will receive that path and attempt to find a Servletto handle it. Since you have a Servletmapped to /*it will use it, but your DispatcherServletdoesn't have a mapping for that path and therefore responds with a 404.

解析要与 aRequestDispatcherforward方法一起使用的路径。那条路径将是/WEB-INF/views/index.jsp. 现在 Servlet 容器将接收该路径并尝试找到一个Servlet来处理它。由于您有一个Servlet映射到/*它会使用它,但您DispatcherServlet没有该路径的映射,因此以 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 JspServletand use it.

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