DispatcherServlet,解析器和控制器之间如何交互? [英] How does the DispatcherServlet, Resolver and Controllers interact?

查看:76
本文介绍了DispatcherServlet,解析器和控制器之间如何交互?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好,所以我遇到了相当普遍的错误:

Ok so I have encountered the fairly common errror:

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

我正在查看现有的答案,但还没有看到有关组件如何交互的很好的解释.鉴于我无法根据现有答案解决问题,所以希望有人可以提供DispatcherServletResolver s功能的详细说明.

I'm looking at the existing answers and I've not seen a really good explanation of how the components interact. Seeing as I can't solve my issue based on the existing answers, I'm hoping someone can provide a detailed explanation of the function of DispatcherServlet and Resolvers.

在Eclipse中进行开发,我具有以下结构:

Developing in Eclipse, I have the following structure:

/src/com/whiuk/philip/web/controller/IndexController.java
/WebContent
/WebContent/WEB-INF
/WebContent/WEB-INF/web.xml
/WebContent/WEB-INF/app-servlet.xml
/WebContent/WEB-INF/jsp/index.jsp

Eclipse部署程序集意味着它按以下方式进行部署:

Eclipse Deployment Assembly means it deploys as follows:

/src -> WEB-INF/classes
/WebContent -> /
/ivy.xml[*] -> WEB-INF/lib

我有一个 web.xml 文件,该文件定义了DispatcherServlet并映射到所有文件(/*)

I have a web.xml file that defines a DispatcherServlet and a mapping to all files (/*)

<servlet>
    <servlet-name>app</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>app</servlet-name>
    <url-pattern>/*</url-pattern>
</servlet-mapping>

我有一个 app-servlet.xml 文件,该文件扫描软件包并定义了InternalResourceViewResolver:

I have a app-servlet.xml file that scans the packages and defines a InternalResourceViewResolver:

<context:component-scan base-package="com.whiuk.philip.web" />
<mvc:annotation-driven />
<bean id="viewResolver"
    class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix" value="/WEB-INF/jsp/" />
    <property name="suffix" value=".jsp" />
</bean>

我有一个IndexController具有用于索引的RequestMapping:

I have a IndexController that has a RequestMapping for index:

 @Controller
 public class IndexController {
      @RequestMapping(value = "/index", method = RequestMethod.GET)
      public ModelAndView index() {
             return new ModelAndView();
      }
 }

日志显示已注册:

org.springframework.web.servlet.handler.AbstractHandlerMethodMapping registerHandlerMethod
INFO: Mapped "{[/index],methods=[GET],params=[],headers=[],consumes=[],produces=[],custom=[]}"
onto public org.springframework.web.servlet.ModelAndView 
com.whiuk.philip.web.controller.IndexController.index()

最后我有一个 index.jsp 文件.

有人可以解释一下配置错误导致顶部显示错误的原因吗,如果可能的话,请提供一句话,以说明DispatcherServlet,解析器以及它们如何馈入控制器.

Can someone please explain what the misconfiguration is that results in the error shown at the top and, if possible, provide a sentence or so on the purpose of DispatcherServlet, Resolvers and how they feed into Controllers.

推荐答案

本质上是发生了什么,因为您已将Spring的DispatcherServlet映射到/*,所以倾向于为每个请求调用它(可以),但是不幸的是即使将请求分派到JSP页面(/WEB-INF/jsp/index.jsp),也会被调用,而不是调用容器的默认servlet.

Essentially what is happening is, since you have Spring's DispatcherServlet mapped to /*, it tends to be called for every request(which is okay), but unfortunately gets invoked even when the request gets dispatched to the JSP page (/WEB-INF/jsp/index.jsp), instead of the containers default servlet getting invoked.

我知道的修复方法如下:

The fix that I am aware of is the following:

将其映射到默认servlet路径/:

Map it to the default servlet path / instead:

<servlet-mapping>
    <servlet-name>app</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>

您在上面看到的一个问题是,不幸的是,Webapp根目录下的资源将由DispatcherServlet处理,而该DispatcherServlet不知道该怎么做,解决方法是

One issue that you would see with above is that the resources under the root of your webapp will unfortunately get handled by DispatcherServlet which will not know what do with it, the fix is to register a default-servlet-handler this way:

<mvc:default-servlet-handler />

这篇关于DispatcherServlet,解析器和控制器之间如何交互?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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