Spring 2.5 introduces MVC controllers based on Annotation configuration. This short article describes how to migrate your spring 2.0 application to spring 2.5, at least MVC-related applications.
First make sure that you have placed the spring-webmvc.jar in your classpath, dispatcherservlet is no longer part of spring. jar and is now in a separate module.
Any controller class can be set in one or two ways, and the controller can control one or more actions. The following is an example of a Multi-Action controller that contains three basic independent actions.
Java code
- Package demo;
- Import org. springframework. stereotype. Controller;
- Import org. springframework. Web. Bind. annotation. requestmapping;
- @ Controller
- Public class simplecontroller {
- @ Requestmapping ("/index.html ")
- Public void indexhandler (){
- }
- @ Requestmapping ("/about.html ")
- Public void abouthandler (){
- }
- @ Requestmapping ("/admin.html ")
- Public void adminhandler (){
- }
- }
package demo; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; @Controller public class SimpleController { @RequestMapping("/index.html") public void indexHandler() { } @RequestMapping("/about.html") public void aboutHandler() { } @RequestMapping("/admin.html") public void adminHandler() { } }
That is to say, this is the simplest example. You need to pay attention to some important points, especially the early versions of spring. First, you should note that the controller is a pojo and does not expand abstractcontroller or other controller classes. You will do this in earlier versions of spring. Second, pay attention to annotations, I have used @ controller annotation to mark the controller itself and used @ requestmapping annotations to mark independent methods. I also use annotation for URL Mapping. Finally, use the request URL to locate the logic view name. If dispatcherservlet is not specified, it will automatically match/index.htm to logical name "Index.
The configuration of application context config file is as follows:
Java 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>
- <Context: component-scan base-package = "Demo"/>
- <Bean id = "viewresolver"
- Class = "org. springframework. Web. servlet. View. internalresourceviewresolver">
- <Property name = "prefix" value = "/WEB-INF/JSP/"/>
- <Property name = "suffix" value = ". jsp"/>
- </Bean>
- </Beans>
<?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"> <context:component-scan base-package="demo"/> <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/jsp/"/> <property name="suffix" value=".jsp"/> </bean> </beans>
For in-depth configuration of spring MVC, see annotated web MVC controllers in spring 2.5.
If you want to configure other layers of the application, see annotation-based autowiring in spring 2.5
From: wheelersoftware.com