Migrate the application to spring 2.5 Based on Annotation MVC

Source: Internet
Author: User
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
  1. Package demo;
  2. Import org. springframework. stereotype. Controller;
  3. Import org. springframework. Web. Bind. annotation. requestmapping;
  4. @ Controller
  5. Public class simplecontroller {
  6. @ Requestmapping ("/index.html ")
  7. Public void indexhandler (){
  8. }
  9. @ Requestmapping ("/about.html ")
  10. Public void abouthandler (){
  11. }
  12. @ Requestmapping ("/admin.html ")
  13. Public void adminhandler (){
  14. }
  15. }
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

  1. <? XML version = "1.0" encoding = "UTF-8"?>
  2. <Beans xmlns = "http://www.springframework.org/schema/beans"
  3. Xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance"
  4. Xmlns: context = "http://www.springframework.org/schema/context"
  5. Xsi: schemalocation = "http://www.springframework.org/schema/beans
  6. Http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
  7. Http://www.springframework.org/schema/context
  8. Http://www.springframework.org/schema/context/spring-context-2.5.xsd>
  9. <Context: component-scan base-package = "Demo"/>
  10. <Bean id = "viewresolver"
  11. Class = "org. springframework. Web. servlet. View. internalresourceviewresolver">
  12. <Property name = "prefix" value = "/WEB-INF/JSP/"/>
  13. <Property name = "suffix" value = ". jsp"/>
  14. </Bean>
  15. </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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.