【问题标题】:Multi-controller with annotations in Spring 3.1.1Spring 3.1.1 中带有注释的多控制器
【发布时间】:2014-04-17 15:45:31
【问题描述】:

我正在使用注释在 Springs 3.1.1 框架中尝试多控制器。但它不起作用我尝试通过以不同方式配置 xml 文件来启用对注释的支持。这是我的代码

调度程序-servlet.xml

    <?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
   http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
   http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
   http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
   http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">

<context:component-scan base-package="com.web" />

<bean id="userKey" class="controllers.UserController" />
<bean id="newUserKey" class="controllers.UserFormController" />
<bean id="demoKey" class="controllers.demoController" />


<!--
Most controllers will use the ControllerClassNameHandlerMapping above, but
for the index controller we are using ParameterizableViewController, so we must
define an explicit mapping for it.
-->

<bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
    <property name="mappings">
        <props>
            <prop key="index.htm">indexController</prop>
            <prop key="user.htm">userKey</prop>
            <prop key="add_user.htm">newUserKey</prop>
        </props>
    </property>
</bean>

<bean id="viewResolver"
      class="org.springframework.web.servlet.view.InternalResourceViewResolver"
      p:prefix="/WEB-INF/jsp/"
      p:suffix=".jsp" />

<!--
The index controller.
-->
<bean name="indexController"
      class="org.springframework.web.servlet.mvc.ParameterizableViewController"
      p:viewName="index" />

</beans>

控制器:

@Controller
public class multi extends MultiActionController {

@RequestMapping("/multi/add")
public ModelAndView add(){
    return new ModelAndView("demo", "message", "Add method called");
}

@RequestMapping("/user/divide.htm")
public ModelAndView divide(HttpServletRequest request, HttpServletResponse response) {
    return new ModelAndView("demo", "message", "divide method called");
}
}

web.xml:

    <?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
    <servlet-name>dispatcher</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>2</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>dispatcher</servlet-name>
    <url-pattern>*.htm</url-pattern>
</servlet-mapping>

<session-config>
    <session-timeout>
        30
    </session-timeout>
</session-config>
<welcome-file-list>
    <welcome-file>redirect.jsp</welcome-file>
</welcome-file-list>
</web-app>

我试过了:

解决方案 1:

<mvc:annotation-driven />

解决方案 2:

<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"/>
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"/>

【问题讨论】:

    标签: xml spring spring-mvc annotations springsource


    【解决方案1】:

    对于初学者,您正在混合 2 种策略,您有一个 Controller,它带有 @Controller 注释,现在哪个应该优先?通过 not 扩展 MultiActionController 来修复您的控制器,因为这不适用于注释。

    @Controller
    public class multi {
    
        @RequestMapping("/multi/add")
        public ModelAndView add(){
            return new ModelAndView("demo", "message", "Add method called");
        }
    
        @RequestMapping("/user/divide.htm")
        public ModelAndView divide(HttpServletRequest request, HttpServletResponse response) {
            return new ModelAndView("demo", "message", "divide method called");
        }
    }
    

    您的解决方案 2 使用的是基于 Spring 2.5 的较旧的 @RequestMapping 方式,您应该使用 RequestMappingHandlerMappingRequestMappingHandlerAdapter,它们都在您使用 &lt;mvc:annotation-driven /&gt; 时注册。

    基本上你在dispatcher-servlet.xml 中需要的是以下内容

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="
       http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
       http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">
    
       <!-- Scan only for @Controllers -->
        <context:component-scan base-package="com.web" use-default-filters="false">
            <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller />
        </context:component-scan>
    
        <mvc:annotation-driven />
    
        <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"
              p:prefix="/WEB-INF/jsp/"
              p:suffix=".jsp" />
    
    <!-- The index controller. -->
        <mvc:view-controller view-name="index" />
    
    </beans>
    

    这将注册处理@Controller 注释(&lt;context:component-scan ... /&gt;@RequestMapping 注释(&lt;mvc:annotation-driven /&gt;)所需的所有内容。您不需要SimpleUrlHandlerMapping,因为它与Controller 而不是 @Controller 至少从 Spring 3.x 开始。

    【讨论】:

    • 谢谢 M. Denium。我尝试了您的解决方案,但它给出了异常匹配的通配符是严格的,但找不到元素'mvc:annotation-driven'的声明。我检查了顶部的架构位置,但我无法弄清楚这个异常的原因是什么。我尝试了 spring-mvc.xsd 和 spring-mvc-3.1.xsd。请提出解决方案。
    • 你有所有必要的(spring-webmvc)和正确的 jars(没有冲突的版本!)。建议使用无版本 xsd 文件,因为这些文件将指向最新版本的 xsd。当我在网上而不是在编辑器中输入此内容时,某处可能存在拼写错误。
    • 是的,有输入错误,但我已修复。我已经使用 NetBeans IDE 创建了新项目,因此所有 Jar 文件都存在。我们需要任何 jar 文件来支持 .
    • 不确定为什么所有 jar 文件都会出现,因为您使用的是 netbeans。我强烈建议使用 maven 或 Gralde 来管理您的依赖项。恕我直言,你不应该依赖你的 ide 来为你做这件事。
    • 你是对的。我错过了架构位置的一个地方的空间。谢谢@M。 Deinum
    猜你喜欢
    • 2012-05-06
    • 2011-12-21
    • 1970-01-01
    • 2010-12-30
    • 1970-01-01
    • 2015-01-15
    • 2012-06-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多