DispatcherServlet、Resolver 和 Controller 是如何交互的? [英] How does the DispatcherServlet, Resolver and Controllers interact?

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

问题描述

好的,所以我遇到了相当常见的错误:

警告:在名称为app"的 DispatcherServlet 中未找到具有 URI [/WEB-INF/jsp/index.jsp] 的 HTTP 请求的映射

我正在查看现有的答案,但我还没有看到有关组件如何交互的很好的解释.看到我无法根据现有答案解决我的问题,我希望有人能详细解释DispatcherServletResolvers的功能.>

在Eclipse中开发,我的结构如下:

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

Eclipse 部署程序集意味着它按如下方式部署:

/src ->WEB-INF/类/WebContent ->//ivy.xml[*] ->网络信息/库

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

<servlet-name>app</servlet-name><servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class><启动时加载>1</启动时加载></servlet><servlet-mapping><servlet-name>app</servlet-name><url-pattern>/*</url-pattern></servlet-mapping>

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

<mvc:注解驱动/><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 索引:

 @Controller公共类索引控制器{@RequestMapping(value = "/index", method = RequestMethod.GET)公共模型和视图索引(){返回新的 ModelAndView();}}

日志显示已注册:

org.springframework.web.servlet.handler.AbstractHandlerMethodMapping registerHandlerMethod信息:映射{[/index],methods=[GET],params=[],headers=[],consumes=[],produces=[],custom=[]}"到公共 org.springframework.web.servlet.ModelAndViewcom.whiuk.philip.web.controller.IndexController.index()

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

谁能解释一下导致顶部显示错误的错误配置是什么,如果可能,请提供有关 DispatcherServlet、Resolvers 的用途以及它们如何馈入 Controllers 的句子等.

解决方案

本质上发生的事情是,由于您将 Spring 的 DispatcherServlet 映射到 /*,它往往会为每个请求(其中没问题),但不幸的是,即使请求被分派到 JSP 页面(/WEB-INF/jsp/index.jsp),也会被调用,而不是调用容器默认的 servlet.

我所知道的修复如下:

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

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

您会在上面看到的一个问题是,不幸的是,您的 web 应用程序根目录下的资源将被 DispatcherServlet 处理,而 DispatcherServlet 不知道如何处理它,修复方法是 以这种方式注册一个默认的servlet-handler::>

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'

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.

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 Deployment Assembly means it deploys as follows:

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

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>

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>

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();
      }
 }

Logs show this is registered:

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()

Finally I have a index.jsp file.

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.

解决方案

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:

Map it to the default servlet path / instead:

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

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、Resolver 和 Controller 是如何交互的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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