1

我对 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;
    }

}

应用程序上下文.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>
4

1 回答 1

1

尝试移动此注释:

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

到您的 handleRequest() 方法。Spring 需要知道收到请求后要调用的具体方法。对类进行注解可以让您将一组方法分组到一个具有公共前缀的类中,但我相信您仍然需要对每个处理请求的方法进行注解。

于 2012-08-13T19:33:20.720 回答