-
Aug 13th, 2012, 02:21 PM
#1
SpringMVC using annotations and xml configurations
I am very new to Spring MVC i am creating an application and i want to use both the xml configurations since its easier for me to follow and learn and there are some benefits from the annotation configuration that i would like to make use of.
I have an application that functions great with the xml configuration so i just want to convert that to annotations and still maintain my xml configurations. Basically what i want is to use the @Controller annotation since the SimpleFormController has been depreciated. I followed previous threads on this forum but i am getting an HTTP 404 error. Under is my application. Can someone help me here or tell me what i am doing wrong.
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.RequestMap ping;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.bind.annotation.RequestMet hod;
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/schem...-beans-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schem...ng-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.AuthenticationManage r">
<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.Pr opertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:jdbc.properties</value>
</list>
</property>
</bean>
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSou rceTransactionManager">
<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/schem...-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schem...ontext-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.annotat ion.DefaultAnnotationHandlerMapping"/>
<bean class="org.springframework.web.servlet.mvc.annotat ion.AnnotationMethodHandlerAdapter"/>
<bean class="org.springframework.web.servlet.mvc.SimpleC ontrollerHandlerAdapter"/>
<bean id="messageSource" class="org.springframework.context.support.Resourc eBundleMessageSource">
<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.AuthenticationManage r" />
<bean name="login" class="com.crimetrack.business.Login" />
<bean id="viewResolver" class="org.springframework.web.servlet.view.Intern alResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlVi ew"></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.Paramet erizableViewController"> <property name="viewName" value="login"/> </bean> -->
</beans>
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules