How to configure Spring MVC (not in the web. xml and XML modes) using Java classes, mvcweb. xml

Source: Internet
Author: User
Tags spring mvc tutorial

How to configure Spring MVC (not in the web. xml and XML modes) using Java classes, mvcweb. xml

DispatcherServlet is the core of Spring MVC. It needs to be configured to the web in the traditional way. in xml. I personally do not like the XML configuration method. XML looks too tired and tedious. with the help of Servlet 3 specifications and Spring 3.1 feature enhancements, Spring MVC can be configured in a new and more concise way. the following is a Hello World MVC configuration in this way.

Step 1: Use eclipse to create a Maven WEB project. pom. xml file as follows:

 1 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 2   <modelVersion>4.0.0</modelVersion> 3   <groupId>ocr</groupId> 4   <artifactId>ocr</artifactId> 5   <version>0.0.1-SNAPSHOT</version> 6   <packaging>war</packaging> 7  8     <properties> 9         <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>10         <javaee-api.version>7.0</javaee-api.version>11         <spring.version>4.2.0.RELEASE</spring.version>12         <junit.version>4.12</junit.version>13     </properties>14 15     <dependencies>16          <dependency>17               <groupId>javax</groupId>18               <artifactId>javaee-api</artifactId>19               <version>${javaee-api.version}</version>20         </dependency>21         <dependency>22                 <groupId>junit</groupId>23                 <artifactId>junit</artifactId>24                 <version>${junit.version}</version>25         </dependency>26         <dependency>27             <groupId>org.springframework</groupId>28             <artifactId>spring-context</artifactId>29             <version>${spring.version}</version>30         </dependency>31         <dependency>32             <groupId>org.springframework</groupId>33             <artifactId>spring-aop</artifactId>34             <version>${spring.version}</version>35         </dependency>36         <dependency>37             <groupId>org.springframework</groupId>38             <artifactId>spring-webmvc</artifactId>39             <version>${spring.version}</version>40         </dependency>41         <dependency>42             <groupId>org.springframework</groupId>43             <artifactId>spring-web</artifactId>44             <version>${spring.version}</version>45         </dependency>46 47         <dependency>48             <groupId>javax.servlet</groupId>49             <artifactId>jstl</artifactId>50             <version>1.2</version>51         </dependency>52 53         <dependency>54             <groupId>commons-logging</groupId>55             <artifactId>commons-logging</artifactId>56             <version>1.1.3</version>57         </dependency>58     </dependencies>59 60 61     <build>62         <plugins>63             <plugin>64                 <artifactId>maven-compiler-plugin</artifactId>65                 <version>3.3</version>66                 <configuration>67                     <source>1.7</source>68                     <target>1.7</target>69                 </configuration>70             </plugin>71             <plugin>72                 <artifactId>maven-war-plugin</artifactId>73                 <version>2.6</version>74                 <configuration>75                     <warSourceDirectory>WebContent</warSourceDirectory>76                     <failOnMissingWebXml>false</failOnMissingWebXml>77                 </configuration>78             </plugin>79         </plugins>80     </build>81 </project>

Step 2: Configure DispatcherServlet. You need to create a Web initialization class OcrWebAppInitializer, inherited from AbstractAnnotationConfigDispatcherServletInitializer

1 package com. chry. ocr. config; 2 3 import org. springframework. web. servlet. support. abstractAnnotationConfigDispatcherServletInitializer; 4 5 public class OcrWebAppInitializer extends actannotationconfigdispatcherservletinitializer {6 7 @ Override 8 protected Class <?> [] GetRootConfigClasses () {9 return new Class <?> [] {RootConfig. class}; 10} 11 12 @ Override13 protected Class <?> [] GetServletConfigClasses () {14 return new Class <?> [] {WebConfig. class}; // configure the specified Web configuration class 15} 16 17 @ Override18 protected String [] getServletMappings () {// map DispatcherServlet to "/" 19 return new String [] {"/"}; 20} 21 22}

Step 3: Configure Spring MVC view to parse WebConfig. java. You need to create a class inherited from WebMvcConfigurerAdapter

1 package com. chry. ocr. config; 2 3 import org. springframework. context. annotation. bean; 4 import org. springframework. context. annotation. componentScan; 5 import org. springframework. context. annotation. configuration; 6 import org. springframework. web. servlet. viewResolver; 7 import org. springframework. web. servlet. config. annotation. defaultServletHandlerConfigurer; 8 import org. springframework. web. servlet. config. annotation. enableWebMvc; 9 import org. springframework. web. servlet. config. annotation. webMvcConfigurerAdapter; 10 import org. springframework. web. servlet. view. internalResourceViewResolver; 11 12 @ Configuration13 @ EnableWebMvc // start SpringMVC14 @ ComponentScan ("com. chry. ocr. controller ") // start component scan 15 public class WebConfig extends webmvcjavaseradapter {16 17 // configure JSP view parser 18 @ Bean19 public ViewResolver viewResolver () {20 InternalResourceViewResolver resolver = new InternalResourceViewResolver (); 21 resolver. setPrefix ("WEB-INF/views/"); 22 resolver. setSuffix (". jsp "); 23 resolver. setExposeContextBeansAsAttributes (true); 24 return resolver; 25} 26 27 // handle static resource configuration 28 @ Override29 public void configuredefaservservlethandling (defaservlethandlerconfigurer handler ER) {30 handler er. enable (); // requests to static resources are forwarded to the container's default servlet instead of DispatcherServlet31} 32 33}

Step 4: Configure RootConfig. java

 1 package com.chry.ocr.config; 2  3 import org.springframework.context.annotation.ComponentScan; 4 import org.springframework.context.annotation.ComponentScan.Filter; 5 import org.springframework.context.annotation.Configuration; 6 import org.springframework.context.annotation.FilterType; 7 import org.springframework.web.servlet.config.annotation.EnableWebMvc; 8  9 @Configuration10 @ComponentScan( basePackages={"com.chry.ocr"}, 11                 excludeFilters = { @Filter(type=FilterType.ANNOTATION,value=EnableWebMvc.class)}12               )13 14 public class RootConfig {15 16 }

So far, the traditional method needs to use web. the preceding three java classes (OcrWebAppInitializer, RootConfig, and WebConfig) have been completed. you can start writing Controller and Page code.

Step 5: Write A HomeController. java that will output "hello World from Spring MVC" to the home. jsp page.

 1 package com.chry.ocr.controller; 2  3 import static org.springframework.web.bind.annotation.RequestMethod.*; 4 import org.springframework.stereotype.Controller; 5 import org.springframework.web.bind.annotation.RequestMapping; 6 import org.springframework.web.bind.annotation.RequestMethod; 7 import org.springframework.web.servlet.ModelAndView; 8  9 @Controller10 public class HomeController {11     @RequestMapping(value = "/", method=GET)12     public ModelAndView home() {13         String message = "Hello world from Spring MVC";14         return new ModelAndView("home", "message", message);15     }16 }

Step 6: compile a jsp page, according to our configuration in the view parser and Controller, put in the WEB-INF/views/home. jsp

 1 <%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="UTF-8"%> 2 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 3 

Step 7: after all the work is completed, use the "clean install" option of maven to compile and package the package, and then run the command to access http: // localhost: 8080. the page effect and project structure are as follows. There is no web in the project. xml

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.