Controller 方法签名(方法入参)介绍 springmvc

看代码,一看就明白:
package com.wondersgroup.hs.test.controller;

import java.io.IOException;
import java.io.OutputStream;
import java.security.Principal;
import java.util.Locale;

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

import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.stereotype.Controller;
import org.springframework.util.FileCopyUtils;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.context.request.WebRequest;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.util.WebUtils;

import com.wondersgroup.hs.test.model.User;
@Controller
@RequestMapping("/methodSignature")
public class ProcessingMethodSignatureController {
	
	//1.使用@RequestParam绑定请求参数值
	@RequestMapping(value="/method1")
	public String method1(
			@RequestParam(value="userName",required = false) String userName,
			@RequestParam("pwd") String pwd){
		
		return "methodSignature";
	}
	//2.使用@CookieValue绑定请求中的Cookie值
	@RequestMapping(value="/method2")
	public String method2(
			@CookieValue(value="sessionId",required=false)String sessionId,
			@RequestParam("count") int count){
		
		return "methodSignature";
	}
	//3.使用@RequestHeader绑定请求报文头的属性值
	@RequestMapping(value="/method3")
	public String method3(
			@RequestHeader("Accept-Encoding") String encoding,
			@RequestHeader("Keep-Alive") long keepAlive){
		return "methodSignature";
	}
	//4.使用命令/表单对象绑定请求参数值
	@RequestMapping(value="/method4")
	public String method4(User user){
		return "methodSignature";
	}
	
	//5.使用Servlet API对象作为入参
	//(1)同时使用HttpServletRequest/HttpServletResponse
	@RequestMapping(value="method5")
	public void method5(HttpServletRequest request,HttpServletResponse response){
		String userName = WebUtils.findParameterValue(request, "userName");
		response.addCookie(new Cookie("userName",userName));
	}
	//(2)仅使用HttpServletRequest作为入参
	@RequestMapping(value="method6")
	public ModelAndView method6(HttpServletRequest request){
		String userName = WebUtils.findParameterValue(request, "userName");
		ModelAndView mav = new ModelAndView();
		mav.setViewName("success");
		mav.addObject("userName", userName);
		return mav;
	}
	//(3)使用HttpSession作为入参
	@RequestMapping(value="method7")
	public String method7(HttpSession session){
		session.setAttribute("sessionId", 1234);
		return "success";
	}
	//(4)既使用HttpServletRequest同时使用基本类型的入参
	@RequestMapping(value="method8")
	public String method8(HttpServletRequest request,
			@RequestParam("userName") String userName){
		return "methodSignature";
	}
	//(5)若干Servlet原生API接口,如WebRequest和NativeWebRequest等
	@RequestMapping(value="method9")
	public String method9(WebRequest request){
		String userName = request.getParameter("userName");
		return "methodSignature";
	}
	//5.使用IO对象作为入参
	//使用java.io.InputStream/java.io.Reader 及java.io.OutputStream/java.io.Writer
	@RequestMapping(value="method10")
	public void method10(OutputStream os) throws IOException{
		Resource res = new ClassPathResource("/image.jpg");//读取类路径下的图片文件
		FileCopyUtils.copy(res.getInputStream(), os);
	}
	
	//6.使用其他类型的参数
	//java.util.Locale/java.security.Principal  
	//也可以通过Servlet的HttpServletRequest的getLocale()及getUserPrincipal()
	public String method11(Locale locale,Principal principal){
		return "methodSignature";
	}
	
	
}

你可能感兴趣的:(spring,mvc,controller,方法签名,方法入参)