Spring MVC simple HelloWorld instance application based on Annotation configuration, mvchelloworld
2.1 questions
Use annotations to reconstruct the helloworld application case.
2.2 Solution
1. @ RequestMapping annotation Application
@ RequestMapping can be used in class definition and method definition to indicate which customer request the class or method corresponds. The instance code is as follows:
@RequestMapping("/day01")public class HelloController { @RequestMapping("/hello.form") public String execute() throws Exception { return "hello"; }}
2. To enable @ RequestMapping annotation ing, you must configure it in the XML configuration file of Spring. The configuration code is as follows:
<mvc:annotation-driven/>
3. @ Controller annotation Application
We recommend that you use the @ Controller annotation to declare the Controller component, so that the Controller definition is more flexible. You do not need to implement the Controller Interface, And you can flexibly define the request processing method. The Code is as follows:
@Controller@RequestMapping("/day01")public class HelloController { @RequestMapping("/hello.form") public String execute() throws Exception { return "hello"; }}
4. To make the @ Controller annotation take effect, you need to enable the component scan definition in the Spring XML configuration file and specify the Controller component package. The configuration code is as follows:
<context:component-scan base-package="com.souvc.controller"/>
The environment used is eclipse, jdk7.0, and Tomcat 7.0.
Step 1
Step 1: Create a new project named SpringControler:
Step 2: add the HelloController class
Modify the HelloController class by using annotations. The Code is as follows:
package com.souvc.controller;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;@Controller@RequestMapping("/day01")public class HelloController { @RequestMapping("/hello.form") public String execute() throws Exception { return "hello"; }}
Step 3: configure the spring-mvc.xml
In the spring-mvc.xml file, enable @ RequestMapping annotation ing and component scanning, the Code is as follows:
<? 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" xmlns: jdbc = "http://www.springframework.org/schema/jdbc" xmlns: jee = "http://www.springframework.org/schema/jee" xmlns: tx = "http://www.springframework.org/schema/tx" xmlns: jpa = "http://www.springframework.org/schema/data/jpa" xmlns: Mvc = "http://www.springframework.org/schema/mvc" xsi: schemaLocation = "http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd Http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd http://www.springframework.org/schema/mvc Spring-mvc-3.2.xsd> <context: component-scan base-package = "com. souvc. controller"/> <mvc: annotation-driven/> <! -- Define ViewResolver --> <bean id = "viewResolver" class = "org. springframework. web. servlet. view. internalResourceViewResolver "> <property name =" prefix "value ="/WEB-INF/jsp/"/> <property name =" suffix "value = ". jsp "/> </bean> </beans>
The web. xml file is as follows:
<? Xml version = "1.0" encoding = "UTF-8"?> <Web-app version = "2.5" xmlns = "http://java.sun.com/xml/ns/javaee" xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance" xsi: schemaLocation = "http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <servlet-name> springmvc </servlet-name> <servlet-class> org. springframework. web. servlet. dispatcherServlet </servlet-class> <! -- Specify the Spring configuration file --> <init-param> <param-name> contextConfigLocation </param-name> <param-value> classpath: spring-mvc.xml </param-value> </init-param> <load-on-startup> 1 </load-on-startup> </servlet> <servlet-mapping> <servlet -name> springmvc </servlet-name> <url-pattern> *. form </url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file> index. jsp </welcome-file> </welcome-file-list> </web-app>
Step 4: Test
Access HelloController through the address "http: // localhost: 8080/SpringControler/day01/hello. form:
Source code is as follows: http://yunpan.cn/cm3CqEXD9TeJf access password 2d10