SpringMVC學習筆記(三),springmvc學習筆記
一.SpringMVC使用註解完成
1.首先,匯入SpringMVC需要的jar包。
2.添加Web.xml設定檔中關於SpringMVC的配置
<!--configure the setting of springmvcDispatcherServlet and configure the mapping--><servlet> <servlet-name>springmvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:springmvc-servlet.xml</param-value> </init-param> <!-- 表示伺服器啟動時載入該設定檔 <load-on-startup>1</load-on-startup> --></servlet> <servlet-mapping> <servlet-name>springmvc</servlet-name> <url-pattern>*.do</url-pattern></servlet-mapping>
3.在src下添加springmvc-servlet.xml設定檔
<?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:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd"> <!-- 掃描包以及子包 --> <context:component-scan base-package="test.SpringMVC"/> <!-- 不處理靜態資源 --> <mvc:default-servlet-handler /> <!-- 如果使用註解必須跟如下配置 --> <mvc:annotation-driven /> <!-- 視圖解析器 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" id="internalResourceViewResolver"> <!-- 首碼 --> <property name="prefix" value="/WEB-INF/jsp/" /> <!-- 尾碼 --> <property name="suffix" value=".jsp" /> </bean></beans>
4.在WEB-INF檔案夾下建立名為jsp的檔案夾,用來存放jsp視圖。建立一個hello.jsp,在body中添加“Hello World”。
5.建立包及Controller,如下所示
6.編寫Controller代碼
@Controller@RequestMapping("/mvc")public class mvcController { @RequestMapping("/hello") public String hello(){ return "hello"; }}
7.啟動伺服器,鍵入 http://localhost:8080/項目名/mvc/hello
二、配置解析
1.Dispatcherservlet
DispatcherServlet是前端控制器,配置在web.xml檔案中的。攔截匹配的請求,Servlet攔截匹配規則要自已定義,把攔截下來的請求,依據相應的規則由HandleMapping分發到目標Controller來處理,是配置spring MVC的第一步。
2.InternalResourceViewResolver
視圖名稱解析器
3.以上出現的註解
@Controller 負責註冊一個bean 到spring 上下文中
@RequestMapping 註解為控制器指定可以處理哪些 URL 請求
三、SpringMVC常用註解
@Controller
負責註冊一個bean 到spring 上下文中
@RequestMapping
註解為控制器指定可以處理哪些 URL 請求
@RequestBody
該註解用於讀取Request請求的body部分資料,使用系統預設配置的HttpMessageConverter進行解析,然後把相應的資料繫結到要返回的對象上 ,再把HttpMessageConverter返回的對象資料繫結到 controller中方法的參數上
@ResponseBody
該註解用於將Controller的方法返回的對象,通過適當的HttpMessageConverter轉換為指定格式後,寫入到Response對象的body資料區
@ModelAttribute
在方法定義上使用 @ModelAttribute 註解:Spring MVC 在調用目標處理方法前,會先逐個調用在方法級上標註了@ModelAttribute 的方法
在方法的入參前使用 @ModelAttribute 註解:可以從隱含對象中擷取隱含的模型資料中擷取對象,再將請求參數 –綁定到對象中,再傳入入參將方法入參對象添加到模型中
@RequestParam
在處理方法入參處使用 @RequestParam 可以把請求參 數傳遞給要求方法
@PathVariable
綁定 URL 預留位置到入參
@ExceptionHandler
註解到方法上,出現異常時會執行該方法
@ControllerAdvice
使一個Contoller成為全域的異常處理類,類中用@ExceptionHandler方法註解的方法可以處理所有Controller發生的異常