Request请求相关

Request请求相关

package com.hous.springmvc.controller;

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

@RequestMapping("/shanshanbox")
@Controller
public class ShanShanBox {

	private static final String SUCCESS = "success";
	
	/**
	 * Rest风格的URL
	 * 以CRUD为例:
	 * 新增:/order POST
	 * 删除:/order/1 DELETE   delete?id=1
	 * 修改:/order/2 PUT   	 update?id=2
	 * 查询:/order/3 GET      get?id=3
	 * 
	 * 如何发送PUT请求和DELETE请求
	 * 1.配置HiddenHttpMethodFilter
	 * 2.发送post请求
	 * 3.form发送post请求是,添加隐藏域name="_method",值为DELETE或PUT
	 * 
	 * 在springmvc的目标方法中可以通过@PathVariable注解得到id值
	 */
	@RequestMapping(value="/testRest/{id}",method=RequestMethod.GET)
	public String testGet(@PathVariable String id) {
		System.out.println("get restful " + id);
		return SUCCESS;
	}
	
	@RequestMapping(value="/testRest",method=RequestMethod.POST)
	public String testPost() {
		System.out.println("post restful");
		return SUCCESS;
	}
	
	@RequestMapping(value="/testRest/{id}",method=RequestMethod.PUT)
	public String testPut(@PathVariable String id) {
		System.out.println("put restful " + id);
		return SUCCESS;
	}
	
	@RequestMapping(value="/testRest/{id}",method=RequestMethod.DELETE)
	public String testDelete(@PathVariable String id) {
		System.out.println("delete restful " + id);
		return SUCCESS;
	}
	
	
	/**
	 * 使用@RequestParam绑定请求参数
	 * value请求参数的参数名
	 * required值是否必须
	 * defaultValue请求参数的默认值
	 * @return
	 */
	@RequestMapping(value="/testRequestParam")
	public String testRequestParam(@RequestParam(value="n") String name, 
			@RequestParam(value="a",required=false, defaultValue="0") Integer age) {
		System.out.println("user name: " + name + " and age: " + age);
		return SUCCESS;
	}
	
	/**
	 * 使用@RequestHeader绑定请求头信息
	 * @param language
	 * @return
	 */
	@RequestMapping(value="/testRequestHeader")
	public String testRequestHeader(@RequestHeader(value="Accept-Language") String language) {
		System.out.println("Accept-Language=>" + language);
		return SUCCESS;
	}
	
	
	/**
	 * 使用@CookieValue映射cookie值
	 * @param sessionId
	 * @return
	 */
	@RequestMapping(value="/testCookieValue")
	public String testCookieValue(@CookieValue("JSESSIONID") String sessionId) {
		System.out.println("sessionId=>" + sessionId);
		return SUCCESS;
	}
	
}

猜你喜欢

转载自shuizhongyue.iteye.com/blog/2294398
今日推荐