SpringMVC使用注释和xml配置

时间:2012-08-13 18:28:58

标签: java xml spring model-view-controller annotations

我是Spring MVC的新手。我正在创建一个应用程序,我想使用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>

1 个答案:

答案 0 :(得分:1)

尝试移动此注释:

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

到你的handleRequest()方法。 Spring需要知道在收到请求后要调用的具体方法。注释类允许您将方法集合分组到具有公共前缀的类中,但我相信您仍需要注释处理请求的每个方法。