我正在使用注释在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"/>
答案 0 :(得分:0)
对于初学者,你混合了2个策略,你有一个Controller
注释了@Controller
现在应该优先哪一个?通过不扩展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
方式,您应该使用RequestMappingHandlerMapping
和RequestMappingHandlerAdapter
,这些<mvc:annotation-driven />
和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
中需要的是以下内容
<context:component-scan ... />
这会记录处理@RequestMapping
注释所需的所有内容(<mvc:annotation-driven />
和SimpleUrlHandlerMapping
注释(Controller
。您不需要{{ 1}}因为它至少与Spring 3.x一起用于@Controller
而不是{{1}}。