java - spring不使用RequestMappingHandlerMapping来映射请求

标签 java spring tiles

我正在 Spring MVC 和图 block 集成上创建一个简单的应用程序。 最初我在加载静态内容页面(如 img)时遇到问题。 因此,为了提供静态页面,我在dispatcher-servlet.xml中包含以下行

<mvc:resources mapping="/resources/**" location="/resources/" />

现在应用程序可以加载静态页面。但不知何故,其他请求无法到达 Controller 。在调试时,我发现 handlerMappings 和 handlerAdapters 中分别缺少“RequestMappingHandlerMapping”和“RequestMappingHandlerAdapter”。 当我从dispatcher-servlet.xml 中省略资源映射条目时,我的动态请求将成功得到服务。作为解决方法,我在 xml 文件中手动注册了这两个 bean。

dispatcher-servlet.xml

<context:annotation-config />
    <mvc:resources location="/images/" mapping="/images/**"></mvc:resources>
    <beans:bean id="handlerMapping"
        class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping">
    </beans:bean>
    <beans:bean id="handlerAdapter"
        class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"></beans:bean>
    <context:component-scan
        base-package="com.mds.presentation.controller"></context:component-scan>
    <bean
        class="org.springframework.web.servlet.view.UrlBasedViewResolver"
        id="tilesViewResolver">
        <property name="viewClass"
            value="org.springframework.web.servlet.view.tiles3.TilesView" />
    </bean>
    <bean id="tilesConfigurer"
        class="org.springframework.web.servlet.view.tiles3.TilesConfigurer">
        <property name="definitions">
            <list>
                <value>/WEB-INF/tiles.xml</value>
            </list>
        </property>
    </bean>

测试 Controller

@Controller
public class TestController {

    @RequestMapping(value="/login", method=RequestMethod.GET)
    public ModelAndView login(HttpServletRequest req, HttpServletResponse resp) {
        ModelAndView mav=new ModelAndView("login");
        mav.addObject("home","home");
        mav.addObject("buy","buy");
        mav.addObject("cart","cart");
        return mav;
    }
}

我的好奇心是为什么 spring 在包含 mvc:resources 时无法自行注册适当的处理程序和适配器。我缺少什么吗? 我尝试在 spring 文档和谷歌中搜索它,但找不到任何相关内容。

最佳答案

我认为自己定义这些 bean 没有意义。

你有<mvc:annotation-driven />吗?在你的 XML 配置中?

如果没有,请尝试将其添加到您的 XML 文件中并删除:

<beans:bean id="handlerMapping" class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping">
</beans:bean>

<beans:bean id="handlerAdapter" class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
</beans:bean>

这应该有助于您的请求到达 Controller 。

基本上,mvc:annotation-driven标签设置您的 Spring 上下文,以允许将请求分派(dispatch)到 Controller 。

该标签将配置两个bean DefaultAnnotationHandlerMappingAnnotationMethodHandlerAdapter 。或者也许新版本的 Spring 中有一些新的 bean,但无论如何该标签都会为您配置它们。

关于java - spring不使用RequestMappingHandlerMapping来映射请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49958696/

相关文章:

java - 您如何以编程方式搜索 Google 的 Java API

java - Spring Boot @ExceptionHandler 隐藏异常名称

spring - 如何在没有spring-boot的情况下使用eureka+feign?

java - 其中哪一个是使用 tiles 的更好方法

openlayers-3 - 确保所有切片都加载到 Open Layers 3 XYZ 源中

java - 解决插件版本错误 'org.mortbay.jetty:jetty-maven-plugin'

java - 为什么在 JAVA 中不推荐使用默认包?

java - Spring + JAXB 集成 : Class has two properties of the same name

java - Stuts2 Tiles Tomcat疑似将UTF-8改成?

java - 具有不同状态的值对象的验证 - 密码大小写