學習大數據——Spring MVC中@RequestMapping映射請求註解

@RequestMapping 概念

1) SpringMVC使用@RequestMapping註解爲控制器指定可以處理哪些 URL 請求
2) 作用:DispatcherServlet 截獲請求後,就通過控制器上 @RequestMapping 提供的映射信息確定請求所對應的處理方法。

@RequestMapping 可標註的位置

上一篇博客中的HelloWorld.java:

package com.learn.springmvc.helloworld;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

//@RequestMapping 可標註的位置
//@RequestMapping註解可以添加到類上,也可以添加到方法上
//@RequestMapping("HelloWorld")
@Controller
public class HelloWorld {
	/*
	 * 方法的返回值會被SpringMVC配置文件中配置的InternalResourceViewResolver這個視圖解析爲真是的物理視圖,
	 *  然後自動進行請求轉發
	 *  真是的物理視圖 = 前綴 + 方法返回值 + 後綴
	 *  即:/WEB-INF/views/success.jsp
	 */
	@RequestMapping("/hello")
	public String testHelloWorld() {
		System.out.println("Hello SpringMVC!");
		return "success";
	}
}

@RequestMapping 映射請求URL與請求方式

在上一篇博客的項目中,新建了SpringMVCHandler.java用於處理測試index.jsp頁面的請求。

部分SpringMVCHandler.java:

package com.learn.springmvc.helloworld;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.learn.springmvc.entities.Employee;

@Controller
public class SpringMVCHandler {

	/*
	 * @RequestMapping註解中的屬性
	 * 	1.value:用來設置映射的請求地址,值得類型是String類型的數組
	 * 		如果只映射一個請求地址,那麼value的值不需要添加大括號{},value屬性名可以省略不寫
	 * 	2.method:用來設置要映射的請求方式
	 * 		如果沒有設置該屬性,那麼只看映射的請求地址,不管請求方式
	 */
	@RequestMapping(value= {"/testValue","/testValue2"})
	public String testValue() {
		System.out.println("測試@RequestMapping註解value屬性");
		return "success";
	}
//	@RequestMapping(value="/testMethod",method=RequestMethod.GET,params="age=18")
	@RequestMapping(value="/testMethod",method=RequestMethod.POST)
	public String testMethod() {
		System.out.println("測試@RequestMapping註解method屬性");
		return "success";
	}

測試頁面index.jsp(使用到的是第2個<a>中的testValue2和第3個<a>中的testMethod):

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
	<a href="${pageContext.request.contextPath }/hello">Hello SpringMVC</a><br>
	<a href="${pageContext.request.contextPath }/testValue2">Test value</a><br>
	<a href="${pageContext.request.contextPath }/testMethod">Test method</a><br>
	<form action="${pageContext.request.contextPath }/testMethod" method="post">
		<input type="submit" value="Test Method">
	</form>
<%-- 		<a href="${pageContext.request.contextPath }/testRequestParam?username=admin&age=18">Test RequestParam</a><br> --%>
		<a href="${pageContext.request.contextPath }/testRequestParam?username=admin">Test RequestParam</a><br>
	<form action="${pageContext.request.contextPath }/testPOJO" method="post">
		員工工號:<input type="text" name="id"><br>
		員工姓名:<input type="text" name="lastName"><br>
		員工郵箱:<input type="text" name="email"><br>
		部門編號:<input type="text" name="dept.id"><br>
		部門名稱:<input type="text" name="dept.name"><br>
		<input type="submit" value="Test POJO">
	</form>
			<a href="${pageContext.request.contextPath }/testServletAPI?username=admin">Test ServletAPI</a><br>
</body>
</html>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章