This is my attempt to have both Annotation and xml based controllers in one application. I started with SpringMVC version 2.5 step-by-step tutorial v. This was XML based controller. I was able to successfully deploy this web application. This app has two views hello.htm and priceincrease.htm. I retained the hello.htm as it is (i.e. XML based controller) and modified the priceincrease.htm to Annotation based. I was able to compile and deploy successfully these approaches together. However, I am encountering a strange issue: Now my application is unable to find mapping for "hello.htm" but "priceincrease.htm" works fine.
Here are some modifications I did (I have retained code in form of comments which were applicable for XML based controller. Also for some reason Validator wasn't working so I have commented this code...pls ignore that for now.):
Code:
//public class PriceIncreaseFormController extends SimpleFormController {
@Controller
@RequestMapping("/priceincrease.htm")
public class PriceIncreaseFormController{
/** Logger for this class and subclasses */
protected final Log logger = LogFactory.getLog(getClass());
@Autowired
private ProductManager productManager;
//@Autowired
// private Validator priceValidator;
// public ModelAndView onSubmit(Object command) throws ServletException {
@RequestMapping(method=RequestMethod.POST)
public String processSubmit(@ModelAttribute("priceIncrease") PriceIncrease priceIncrease,
BindingResult result, SessionStatus status){
//priceValidator.validate(priceIncrease, result);
//if(result.hasErrors()){
//return "priceincrease"; // return the view name
//} else {
int increase = priceIncrease.getPercentage();
logger.info("Increasing prices by " + increase + "%.");
productManager.increasePrice(increase);
status.setComplete();
//logger.info("returning from PriceIncreaseForm view to " + getSuccessView());
//return new ModelAndView(new RedirectView(getSuccessView()));
return "redirect:hello.htm";
//}
}
@RequestMapping(method=RequestMethod.GET)
public String setupForm(@RequestParam(required = false, value= "20") Integer percentage, ModelMap model){
//protected Object formBackingObject(HttpServletRequest request) throws ServletException {
PriceIncrease priceIncrease = new PriceIncrease();
priceIncrease.setPercentage(20);
model.addAttribute("priceIncrease", priceIncrease);
return "priceincrease";
}
public void setProductManager(ProductManager productManager) {
this.productManager = productManager;
}
public ProductManager getProductManager() {
return productManager;
}
}
and the springapp-servlet.xml looks like
Code:
<?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:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd">
<!-- the application context definition for the springapp DispatcherServlet -->
<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
<property name="basename" value="messages"/>
</bean>
<bean name="/hello.htm" class="springapp.web.InventoryController">
<property name="productManager" ref="productManager" />
</bean>
<!-- Commented this as now the Annotations-based approach takes care of this
<bean name="/priceincrease.htm" class="springapp.web.PriceIncreaseFormController">
<property name="sessionForm" value="true"/>
<property name="commandName" value="priceIncrease"/>
<property name="commandClass" value="springapp.service.PriceIncrease"/>
<property name="validator">
<bean class="springapp.service.PriceIncreaseValidator"/>
</property>
<property name="formView" value="priceincrease"/>
<property name="successView" value="hello.htm"/>
<property name="productManager" ref="productManager"/>
</bean>
-->
<bean name="/addproducts.htm" class="springapp.web.AddNewProductFormController">
<property name="sessionForm" value="true"/>
<property name="commandName" value="addNewProduct"/>
<property name="commandClass" value="springapp.domain.Product" />
<property name="validator">
<bean class="springapp.service.NewProductValidator"/>
</property>
<property name="formView" value="addproducts"/>
<property name="successView" value="hello.htm"/>
<property name="productManager" ref="productManager"/>
</bean>
<!-- changes for Annotations: Position of this block did not matter -->
<context:component-scan base-package="springapp.web" />
<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"/>
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"/>
<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>
</beans>
Apart from this nothing was changed.
Configuration: Spring MVC 2.5 + HSQL + Apache Tomcat 6.0.26
Please let me know your thoughts...
Thanks,
Vikram