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") public class Hellocontroller { @RequestMapping ("/hello.form") public String Execute () throws 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") public String execute () throws 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, which imports spring dependent packages.
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") public String execute () throws 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 /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 http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd "> < Context:component-scan base-package= "Com.souvc.controller"/> <mvc:annotation-driven/> <!--definition view plots Viewresolver--<bean id= "Viewresolver" class= "Org.springframework.web.servlet.view.InternalResourceVie Wresolver "> <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-nam e> <servlet-class> Org.springframework.web.servlet.DispatcherServlet </servlet-class& Gt <!--specify spring configuration Files--<init-param> <PARAM-NAME>CONTEXTCONFIGLOCATION</PARAM-NAME&G T <param-value>classpath:spring-mvc.xml</param-value> </init-param> <load-on-startup>1 </load-on-startup> </servlet> <servlet-mapping> <servlet-name>springmvc</servlet-n ame> <url-pattern>*.form</url-pattern> </servlet-mapping> <welcome-file-list> <weLcome-file>index.jsp</welcome-file> </welcome-file-list></web-app>
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 for annotation configuration