使用注解的方式重構helloworld應用案例。
2.2 方案1. @RequestMapping注解應用
@RequestMapping可以用在類定義和方法定義上,它標明這個類或方法與哪一個客戶請求對應。實例代碼如下:
@RequestMapping("/day01")public class HelloController { @RequestMapping("/hello.form") public String execute() throws Exception { return "hello"; }}
2. 開啟@RequestMapping注解映射,需要在Spring的xml配置文件進行配置,配置代碼如下:
<mvc:annotation-driven/>
3. @Controller注解應用
推薦使用@Controller注解聲明Controller組件,這樣可以使得Controller定義更加靈活,可以不用實現Controller接口,請求處理的方法也可以靈活定義。代碼如下:
@Controller@RequestMapping("/day01")public class HelloController { @RequestMapping("/hello.form") public String execute() throws Exception { return "hello"; }}
4. 為了使@Controller注解生效,需要在Spring的XML配置文件中開啟組件掃描定義,并指定Controller組件所在包,配置代碼如下:
<context:component-scan base-package="com.souvc.controller"/>
使用的環境是eclipse ,jdk7.0 ,Tomcat 7.0,導入Spring依賴包。
2.3 步驟步驟一:新建一個新工程,工程名為SpringControler,如圖下:
步驟二:增加HelloController類
使用注解的方式,修改HelloController類,代碼如下所示:
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"; }}
步驟三:spring-mvc.xml的配置
在spring-mvc.xml文件中,開啟@RequestMapping注解映射以及組件掃描,代碼如下所示:
<?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/> <!-- 定義視圖解析器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>
web.xml文件如下:
<?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> <!-- 指定Spring的配置文件 --> <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>
步驟四:測試
通過地址“http://localhost:8080/SpringControler/day01/hello.form”訪問HelloController:
新聞熱點
疑難解答