2012-08-13 69 views
1

我對Spring MVC很新穎。我正在創建一個應用程序,並且我想使用xml配置(因爲它更容易跟蹤和學習),但是我希望使用註釋配置的一些好處。SpringMVC使用註釋和XML配置

我有一個應用程序,與xml配置功能很好,所以我只想將我的MVC控制器轉換爲註釋,仍然保持我的xml配置的其餘部分。基本上我想要的是使用@Controller註釋,因爲SimpleFormController已被棄用。我跟着以前的線程在這個論壇上,但我得到一個HTTP 404錯誤。有人可以幫助我嗎?或者告訴我我做錯了什麼?

控制器

import java.util.HashMap; 
import java.util.Map; 
import javax.servlet.http.HttpServletRequest; 
import javax.servlet.http.HttpServletResponse; 
import org.apache.log4j.Logger; 
import org.springframework.stereotype.Controller; 
import org.springframework.web.bind.annotation.RequestMapping; 
import org.springframework.web.servlet.ModelAndView; 
import org.springframework.web.bind.annotation.RequestMethod; 

import com.crimetrack.service.CountryManager; 

@Controller 
@RequestMapping(value="/hello.htm", method = RequestMethod.GET) 
public class CountryListController{ 

    private final Logger logger = Logger.getLogger(getClass()); 
    private CountryManager countryManager; 


    public ModelAndView handleRequest(HttpServletRequest request, 
      HttpServletResponse response) throws Exception { 

     logger.debug("In Http method for CountryListController"); 

     Map<String, Object> myModel = new HashMap<String, Object>(); 
     myModel.put("countryList", this.countryManager.getCountries()); 

     return new ModelAndView("hello", "model", myModel); 
    } 


    public void setCountryManager(CountryManager countrymanager){ 

     this.countryManager = countrymanager; 
    } 

} 

的applicationContext.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:aop="http://www.springframework.org/schema/aop" 
     xmlns:tx="http://www.springframework.org/schema/tx" 
     xsi:schemaLocation="http://www.springframework.org/schema/beans 
      http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
      http://www.springframework.org/schema/aop 
      http://www.springframework.org/schema/aop/spring-aop-3.0.xsd 
      http://www.springframework.org/schema/tx 
      http://www.springframework.org/schema/tx/spring-tx-3.0.xsd"> 



    <bean id="countryManager" class="com.crimetrack.service.CountryManager"> 
     <property name="countryDao" ref="countryDao"/> 
    </bean> 

    <bean id="countryDao" class="com.crimetrack.jdbc.JdbcCountryDAO"> 
     <property name="dataSource" ref="dataSource"/> 
    </bean> 

    <bean id="authenticationManager" class="com.crimetrack.service.AuthenticationManager"> 
     <property name="loginDao" ref="loginDao" /> 
    </bean> 

    <bean id="loginDao" class="com.crimetrack.jdbc.JdbcLoginDAO"> 
     <property name="dataSource" ref="dataSource" /> 
    </bean> 


    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> 
      <property name="driverClassName" value="${jdbc.driverClassName}"/> 
      <property name="url" value="${jdbc.url}"/> 
      <property name="username" value="${jdbc.username}"/> 
      <property name="password" value="${jdbc.password}"/> 
    </bean> 

    <bean id="propertyConfigurer" 
      class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> 
     <property name="locations"> 
      <list> 
       <value>classpath:jdbc.properties</value> 
      </list> 
     </property> 
    </bean> 

    <bean id="transactionManager" 
      class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> 
     <property name="dataSource" ref="dataSource"/> 
    </bean>  

    </beans> 

應用servlet.xml中

<?xml version="1.0" encoding="UTF-8"?> 

<beans xmlns="http://www.springframework.org/schema/beans" 
     xmlns:context="http://www.springframework.org/schema/context" 
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
     xsi:schemaLocation="http://www.springframework.org/schema/beans 
          http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
          http://www.springframework.org/schema/context 
          http://www.springframework.org/schema/context/spring-context-3.0.xsd 
          http://www.springframework.org/schema/beans/spring-context-3.0.xsd"> 


    <context:annotation-config/> 

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

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

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

    <bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"/> 


    <bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource"> 
      <property name="basename" value="messages"/> 
    </bean> 


     <bean name="/login.htm" class="com.crimetrack.web.AuthenticationController"> 
     <property name="authenticationManager" ref="authenticationManager"/> 
     <property name="login" ref="login"/> 

    </bean> 

    <bean name="authenticationManager" class="com.crimetrack.service.AuthenticationManager" /> 

    <bean name="login" class="com.crimetrack.business.Login" /> 



     <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 
     <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"></property> 
     <property name="prefix" value="/WEB-INF/jsp/"></property> 
     <property name="suffix" value=".jsp"></property>   
     </bean> 

    <!-- <bean name="/login.htm" class="org.springframework.web.servlet.mvc.ParameterizableViewController"> <property name="viewName" value="login"/> </bean> --> 


</beans> 
+0

我很抱歉,我沒有注意到我的短暫來臨,現在我讀了規則,現在我明白事情是如何工作的,我將在未來更加細心和體貼 – devdar 2012-08-13 18:59:01

+0

您在瀏覽器中使用了哪些URL? – 2012-08-13 19:10:25

+0

它假設導航到http:// localhost:8084/crimeTrack/hello.htm – devdar 2012-08-13 19:27:34

回答

1

嘗試移動此註釋:

@RequestMapping(value="/hello.htm", method = RequestMethod.GET) 

to your handleRequest()method。 Spring收到請求後需要知道調用哪個特定的方法。對類進行註釋可以將一組方法分組爲一個具有通用前綴的類,但我相信您仍然需要註釋處理請求的每個方法。

+0

非常感謝你的工作。 – devdar 2012-08-13 19:38:35

+0

我在我的hello.htm頁面中有">Login,所以我添加了添加到application-servlet.xml,但現在不再有效。我的請求是否正在尋找此頁面的Controller? – devdar 2012-08-13 20:10:48

+0

使用用於調用沒有控制器的頁面 – devdar 2012-08-13 20:43:25