2012-08-13 91 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