gpt4 book ai didi

java - SpringMVC使用注解和xml配置

转载 作者:太空宇宙 更新时间:2023-11-04 08:06:50 25 4
gpt4 key购买 nike

我对 Spring MVC 非常陌生。我正在创建一个应用程序,并且我想使用 xml 配置(因为它对我来说更容易遵循和学习),但是我想利用注释配置有一些好处。

我有一个可以很好地使用 xml 配置的应用程序,因此我只想将我的 MVC Controller 转换为注释,并仍然保留其余的 xml 配置。基本上我想要的是使用 @Controller 注释,因为 SimpleFormController 已被弃用。我关注了此论坛上以前的帖子,但收到 HTTP 404 错误。有人可以帮助我或告诉我我做错了什么吗?

Controller

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>

application-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>

最佳答案

尝试移动此注释:

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

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

关于java - SpringMVC使用注解和xml配置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11940194/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com