2.1 Questions
Use annotations to refactor HelloWorld application cases.
2.2 Solutions
1. @RequestMapping Annotation Application
@RequestMapping can be used on class definitions and method definitions, which indicate which client request the class or method corresponds to. The instance code is as follows:
@RequestMapping ("/day01")publicclass hellocontroller { @RequestMapping (" /hello.form ") publicthrows Exception { return " Hello " ; }}
2. Open the @requestmapping annotation map, which needs to be configured in the spring XML configuration file, with the following configuration code:
<mvc:annotation-driven/>
3. @Controller Annotation Application
It is recommended to declare the controller component using the @controller annotation, which makes the controller definition more flexible, without implementing the Controller interface, and the method of request processing can be flexibly defined. The code is as follows:
@Controller @requestmapping ("/day01") Public class Hellocontroller { @RequestMapping ("/hello.form") publicthrows Exception { return "Hello"; }}
4. In order for the @controller annotation to take effect, you need to open the component scan definition in the spring XML configuration file and specify the package that contains the controller component, with the following configuration code:
<context:component-scan base-package = "Com.souvc.controller"/>
The environment used is eclipse, jdk7.0, Tomcat 7.0
2.3 Steps
Step one: Create a new project, the project is named Springcontroler, under:
Step two: Increase the Hellocontroller class
To modify the Hellocontroller class using annotations, the code looks like this:
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") publicthrows Exception { return "Hello"; }}
Step three: Configuration of the Spring-mvc.xml
In the Spring-mvc.xml file, open the @requestmapping annotation map and the component scan, as shown in the following 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" 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/beanshttp://www.springframework.org/schema/beans/spring-beans-3.2.xsdhttp//Www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-3.2.xsdhttp//Www.springframework.org/schema/jdbchttp://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsdhttp//Www.springframework.org/schema/jeehttp://www.springframework.org/schema/jee/spring-jee-3.2.xsdhttp//Www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx-3.2.xsdhttp//WWW.SPRINGFRAMEWORK.ORG/SCHEMA/DATA/JPAhttp://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsdhttp//Www.springframework.org/schema/mvchttp://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd"><context:component-scan base- Package= "Com.souvc.controller"/> <mvc:annotation-driven/> <!--definition View Resolver viewresolver--<bean I D= "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 reads 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> <servlet-name>springmvc</servlet-name> <servlet-class>Org.springframework.web.servlet.DispatcherServlet</servlet-class> <!--Specify spring configuration Files--<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& Gt;springmvc</servlet-name> <url-pattern>*.form</url-pattern> </servlet-mapping> < Welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list></web-app& Gt
Step Four: Test
Access Hellocontroller via the address "http://localhost:8080/SpringControler/day01/hello.form":
The source code is as follows: Http://yunpan.cn/cm3CqEXD9TeJf access password 2D10
Spring MVC Simple HelloWorld instance application based on annotation configuration