2014-03-13 94 views
1

我在使用註釋在Spring 3.1.1框架中嘗試多控制器。但它不工作,我試圖以不同的方式配置xml文件來支持註釋。這裏是我的代碼在Spring中使用註釋的多控制器3.1.1

調度-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

因爲你是混合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使用的是舊的春天的東西2.5基於@RequestMapping方式,你應該使用RequestMappingHandlerMapping,並且當您使用<mvc:annotation-driven />都被註冊RequestMappingHandlerAdapter

通常,你需要在你的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而不是用於@Controller至少從Spring 3.x起。

+0

Thanks M. Denium。我試過你的解決方案,但它的例外情況匹配的通配符是嚴格的,但沒有聲明可以在元素'mvc:annotation-driven'中找到。我在頂部檢查了模式位置,但我無法弄清楚這個異常的共同點。我試過spring-mvc.xsd和spring-mvc-3.1.xsd。請建議解決方案。 – Dramorian

+0

你有沒有必要的(spring-webmvc)和正確的罐子(沒有衝突的版本!)。建議使用無版本的xsd文件,因爲這些文件將指向最新版本的xsd。當我在線輸入此信息時,可能會出現拼寫錯誤,而不是編輯器中。 –

+0

是的,有打字錯誤,但我固定。我使用NetBeans IDE創建了新項目,因此所有Jar文件都存在。我們是否需要任何jar文件來支持。 – Dramorian

相關問題