1. 程式人生 > >SpringMVC的Controller層引數繫結以及返回值

SpringMVC的Controller層引數繫結以及返回值

當客戶端通過get或post請求傳送來的引數通過Controller中方法的引數接受,叫做引數繫結

Controller方法的返回值1:返回void型別

@RequestMapping("/test_void.action")
public void controller01(HttpServletRequest request,HttpServletResponse response) throws Exception{
request.setCharacterEncoding("utf-8");
String username = request.getParameter("username");//通過HttpServletRequest獲得請求引數
	System.out.println("使用者名稱:"+username);
	request.setAttribute("username",username);
	User u = new User();
u.setUsername(username);
userService.insertUser(u);//插入資料庫
	request.getRequestDispatcher("/WEB-INF/jsp/success.jsp").forward(request, response);//轉發
}
Controller方法的返回值2:返回ModelAndView
@RequestMapping("/test_modelandview.action")
public ModelAndView controller02(HttpServletRequest request) throws Exception{
	request.setCharacterEncoding("utf-8");//轉碼,Tomcat預設是iso-8859-1
	String username = request.getParameter("username");
	System.out.println("使用者名稱:"+username);
	ModelAndView modelAndView = new ModelAndView();//new一個ModelAndView
	modelAndView.addObject("username",username);//相當於request.setAttribute(attrName,attrValue);
	modelAndView.setViewName("WEB-INF/jsp/success.jsp");//檢視跳轉
	return modelAndView;
}
Controller方法的返回值3:返回String型別(邏輯檢視)
@RequestMapping("/test_string.action")
public String controller03(HttpServletRequest request) throws Exception{
	request.setCharacterEncoding("utf-8");//轉碼
	String username = request.getParameter("username");
	request.setAttribute("username",username);//設定請求引數
	System.out.println("使用者名稱:"+username);
	return "/WEB-INF/jsp/success.jsp";//返回String型別,代表邏輯檢視
}	
Controller方法的返回值4:方法的引數是Model,返回值是String型別(邏輯檢視)
@RequestMapping("/test_model.action")
public String controller04(HttpServletRequest request,Model model) throws Exception{
request.setCharacterEncoding("utf-8");
String username = request.getParameter("username");
model.addAttribute("username", username);//等價於request.setAttribute(attrName,attrValue);
System.out.println("使用者名稱:"+username);
return "/WEB-INF/jsp/success.jsp";//返回String型別,跳轉到邏輯檢視
}
Controller方法的返回值5:返回重定向redirect後的邏輯檢視名
@RequestMapping("/test_redirect.action")
public String controller05(HttpServletRequest request) throws Exception{
	request.setCharacterEncoding("utf-8");
	String username = request.getParameter("username");
	User u = new User();
	u.setUsername(username);
	userService.insertUser(u);
	request.setAttribute("username",username);//由於是redirect,所以請求引數失效
	return "redirect:/controller/test_model.action";//redirect:重定向到一個Controller裡
}
Controller方法的返回值6:返回farward轉發後的邏輯檢視名
@RequestMapping("/test_forword.action")
	public String controller06(HttpServletRequest request) throws Exception{
	request.setCharacterEncoding("utf-8");
	String username = request.getParameter("username");
	User u = new User();
	u.setUsername(username);
	userService.insertUser(u);
	System.out.println("使用者名稱:"+username);
	request.setAttribute("username",username);//由於是轉發,所以請求引數有效
	return "forward:/controller/test_model.action";//轉發,跳轉到一個Controller裡
}

引數繫結

引數繫結的第一種方法:繫結普通型別

//引數繫結的第一種方法:客戶端提交的請求的input的name屬性會和Controller方法的引數名字一致才會繫結
@RequestMapping("/test_parambinding01.action")
public void controller07(HttpServletRequest request,HttpServletResponse response,String username,String password) throws Exception{
	//必須進行轉碼
	username = new String(username.getBytes("iso8859-1"),"UTF-8");
	request.setAttribute("username",username);
	request.getRequestDispatcher("/WEB-INF/jsp/success.jsp").forward(request, response);	
}
引數繫結的第二種方法:繫結pojo類
//引數繫結的第二種方法:客戶端的input標籤的那麼屬性必須和User的屬性名對應才可以對映成功
@RequestMapping("/test_parambinding02.action")
public ModelAndView controller08(HttpServletRequest request,User user) throws Exception{
	//必須進行轉碼
	user.setUsername(new String(user.getUsername().getBytes("iso-8859-1"),"utf-8"));
	userService.insertUser(user);
	ModelAndView modelAndView = new ModelAndView();
	request.setCharacterEncoding("utf-8");
	modelAndView.addObject("username",user.getUsername());
	modelAndView.addObject("password",user.getPassword());
	modelAndView.setViewName("/WEB-INF/jsp/success.jsp");
	return modelAndView;
}

引數繫結的第三種方法:當input的name與controller的引數名不一致時,可以採用@RequestParam註解
@RequestMapping("test_RequestParam.action")
//將客戶端的請求引數"username"與"uname"繫結
public ModelAndView controller09(@RequestParam(value="username") String uname,@RequestParam(value="password")String pwd) throws Exception{
	uname = new String(uname.getBytes("iso-8859-1"),"utf-8");	
	ModelAndView modelAndView = new ModelAndView();
	modelAndView.addObject("username",uname);
	modelAndView.setViewName("/WEB-INF/jsp/success.jsp");
	return modelAndView;
}