|
package com.lee.study.spring.mvc.web;
import javax.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
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.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import com.lee.study.spring.mvc.domain.User;
@Controller
@RequestMapping("/owners/{ownerId}")
public class PelativePathUnTemplateController {
/**
* 這個方法使用URL通配符的方式,遵守REST風(fēng)格傳遞了多個參數(shù)
* @param ownerId
* @param petId
* @param model
*/
@RequestMapping("/pets/{petId}")
public void findPet(@PathVariable String ownerId,@PathVariable String petId,Model model){
//do something
}
/**
* 請求參數(shù)按照名稱匹配的方式綁定到方法參數(shù)中,方法返回的字符串代表邏輯視圖
* @param userName
* @param passworld
* @param realName
* @return
*/
@RequestMapping("/handle1")
public String handle1(@RequestParam("userName") String userName,
@RequestParam("passworld") String passworld,
@RequestParam("realName") String realName
){
return "success";
}
/**
* 取出cookie中的值和請求報頭中的值綁定到方法參數(shù)中
* @param sessionId
* @param accpetLanguage
* @return
*/
@RequestMapping("/handle2")
public ModelAndView handle2(
@CookieValue("JSESSIONID") String sessionId,
@RequestHeader("Accept-Language") String accpetLanguage
){
ModelAndView mav = new ModelAndView();
mav.setViewName("/success");
mav.addObject("user",new User());
return null;
}
/**
* 請求參數(shù)按照名稱匹配綁定到user的屬性中.
* @param user
* @return
*/
@RequestMapping("/handle3")
public String handle3(User user){
//do something
return "success";
}
/**
* 直接把request對象傳入到方法參數(shù)中,由此我們可以獲取請求中許多東西
* @param request
* @return
*/
@RequestMapping("/handle4")
public String handle4(HttpServletRequest request){
//do something
return "success";
}
}
|