spring 3 MVC 筆記 2

@RequestMapping 詳解:

package home.dong.springmvc;

import javax.servlet.http.HttpServletRequest;

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.ResponseBody;

//@RequestMapping(
//value="/method-level", 請求路徑,繼承類級別。若類級別也有@RequestMapping("/class-level"),則請求路徑爲 /class-level/method-level
//method=RequestMethod.GET,  RequestMethod 中 GET, HEAD, POST, PUT, DELETE, OPTIONS, TRACE 的一個
//params="abc", 請求路徑中是否含有該參數 支持 param =“!abc”(不含) 。
//headers=""  請求頭信息)
//public String method(){
//
//}

/**
 * 
 * @RequestMapping(value="/xxx",)
 * @author tiger
 * 
 */

@Controller
public class MappingPathController {


	@RequestMapping("/mapping/path")
	public @ResponseBody
	String byPath() {
		return "Mapped by path!";
	}

	@RequestMapping(value = "/mapping/method", method = RequestMethod.GET)
	public @ResponseBody
	String byMethod() {
		return "00000000Mapped by path + method";
	}

	/**
	 * 請求路徑中含有參數名爲foo的,則調用次方法, 如 http://localhost:8080/application_name/mapping/parameter?foo=abc
	 * @return
	 */
	@RequestMapping(value = "/mapping/parameter", method = RequestMethod.GET, params = "foo")
	public @ResponseBody
	String byParameter() {
		return "Mapped by path + method + presence of query parameter! <h4>Parameter name = foo</h4>";
	}

	
	/**
	 * 請求路徑中不含有參數名爲foo的,則調用次方法, 如 http://localhost:8080/application_name/mapping/parameter?abc=cde
	 * @return
	 */
	@RequestMapping(value = "/mapping/parameter", method = RequestMethod.GET, params = "!foo")
	public @ResponseBody
	String byParameterNegation() {
		return "2222222Mapped by path + method + not presence of query parameter!";
	}

	
	
	/**
	 * 請求路徑中,請求頭信息含有“Accept=text/plain”,則調用次方法。
	 * 
	 * request.getHeader(request.getHeaderNames()[i] = value);
	 * request.getHeaderNames()可取得請求頭信息中的所有鍵值。
	 * @param request
	 * @return
	 */
	
	// headers可以是下列鍵值中的任意一個
	//	Host=	localhost:8080
	//	User-Agent =	Mozilla/5.0 (X11; Linux i686; rv:6.0) Gecko/20100101 Firefox/6.0
	//	Accept=	text/plain, */*; q=0.01
	//	Accept-Language	= en-us,zh-cn;q=0.7,en;q=0.3
	//	Accept-Encoding = gzip, deflate
	//	Accept-Charset =	ISO-8859-1,utf-8;q=0.7,*;q=0.7
	//	Connection =	keep-alive
	//	X-Requested-With =	XMLHttpRequest
	//	Referer = http://localhost:8080/Lesson1_SpringMVC/simple/simpleGet
	//	Cookie = JSESSIONID=882CC34288A3B84455E0A54D654E4FA5
	//
	
	@RequestMapping(value = "/mapping/header", method = RequestMethod.GET, headers = "Accept=text/plain")
	public @ResponseBody
	String byHeader(HttpServletRequest request) {
		return "33333333Mapped by path + method + presence of header!";
		
	}

	
	/**
	 * 請求路徑中,請求頭信息不含有“!FooHeader”,則調用次方法。
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/mapping/notheader", method = RequestMethod.GET, headers = "!FooHeader")
	public @ResponseBody
	String byHeaderNegation(HttpServletRequest request) {
		request.getHeader("Accept");
		return "444444444Mapped by path + method + not presence header!";
	}

	/**
	 * 如請求路徑不能找到匹配時,則調用次方法。 如 http://localhost:8080/application_name/mapping/abc_path 
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/mapping/*", method = RequestMethod.GET)
	public @ResponseBody
	String regexp(HttpServletRequest request) {
		return "555555555Mapped by regexp!";
	}
}

JSP:

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link href="<c:url value="/resources/form.css" />" rel="stylesheet" type="text/css" />
<link href="<c:url value="/resources/jqueryui/1.8/themes/base/jquery.ui.all.css" />" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="<c:url value="/resources/jquery/1.6/jquery.js" />"></script>
<script type="text/javascript" src="<c:url value="/resources/jqueryform/2.8/jquery.form.js" />"></script>
<script type="text/javascript" src="<c:url value="/resources/jqueryui/1.8/jquery.ui.core.js" />"></script>
<script type="text/javascript" src="<c:url value="/resources/jqueryui/1.8/jquery.ui.widget.js" />"></script>
<script type="text/javascript" src="<c:url value="/resources/jqueryui/1.8/jquery.ui.tabs.js" />"></script>
<script type="text/javascript" src="<c:url value="/resources/json2.js" />"></script>
<title>Spring 3 MVC</title>

<script type="text/javascript">
	MvcUtil = {};
	MvcUtil.showSuccessResponse = function(text, element) {
		MvcUtil.showResponse("success", text, element);
	};
	MvcUtil.showErrorResponse = function showErrorResponse(text, element) {
		MvcUtil.showResponse("error", text, element);
	};
	MvcUtil.showResponse = function(type, text, element) {
		var responseElementId = element.attr("id") + "Response";
		var responseElement = $("#" + responseElementId);
		if (responseElement.length == 0) {
			responseElement = $(
					'<span id="' + responseElementId + '" class="' + type + '" style="display:none">'
							+ text + '</span>').insertAfter(element);
		} else {
			responseElement
					.replaceWith('<span id="' + responseElementId + '" class="' + type + '" style="display:none">'
							+ text + '</span>');
			responseElement = $("#" + responseElementId);
		}
		responseElement.fadeIn("slow");
	};
	MvcUtil.xmlencode = function(xml) {
		//for IE 
		var text;
		if (window.ActiveXObject) {
			text = xml.xml;
		}
		// for Mozilla, Firefox, Opera, etc.
		else {
			text = (new XMLSerializer()).serializeToString(xml);
		}
		return text.replace(/\&/g, '&' + 'amp;').replace(/</g, '&' + 'lt;')
				.replace(/>/g, '&' + 'gt;').replace(/\'/g, '&' + 'apos;')
				.replace(/\"/g, '&' + 'quot;');
	};
</script>

<script type="text/javascript">
	$(document).ready(function() {
		$("a[class=textLink]").click(function() {
			var link = $(this);
			$.ajax({
				url : link.attr("href"),
				dataType : "text",
				success : function(text) {
					MvcUtil.showSuccessResponse(text, link);
				},
				error : function(xhr) {
					MvcUtil.showErrorResponse(xhr.responseText, link);
				}
			});
			return false;
		});

	});
</script>
</head>
<body>
	this is simple page!

	<ul>
		<li><a id="byPath" href="<c:url value="/mapping/path" />">By path</a></li>
		<li><a id="byMethod" href="<c:url value="/mapping/method" />">By path and method</a></li>
		<li><a id="byParameter" href="<c:url value="/mapping/parameter?foo=fee" />">By path, method, and presence of parameter</a></li>
		<li><a id="byNotParameter" href="<c:url value="/mapping/parameter?bar=foo" />">By path, method, and not presence of parameter</a></li>
		<li><a id="byHeader" class="textLink" href="<c:url value="/mapping/header" />">By presence of header</a></li>
		<li><a id="byHeaderNegation" class="textLink" href="<c:url value="/mapping/notheader" />">By not presence of header</a></li>
		<li><a id="byWildcard" class="textLink" href="<c:url value="/mapping/sss" />">By regexp</a></li>
	</ul>
</body>
</html>


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章