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风格传递了多个参数
* @param ownerId
* @param petId
* @param model
*/
@RequestMapping ( "/pets/{petId}" )
public void findPet( @PathVariable String ownerId, @PathVariable String petId,Model model){
//do something
}
/**
* 请求参数按照名称匹配的方式绑定到方法参数中,方法返回的字符串代表逻辑视图
* @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中的值和请求报头中的值绑定到方法参数中
* @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 ;
}
/**
* 请求参数按照名称匹配绑定到user的属性中.
* @param user
* @return
*/
@RequestMapping ( "/handle3" )
public String handle3(User user){
//do something
return "success" ;
}
/**
* 直接把request对象传入到方法参数中,由此我们可以获取请求中许多东西
* @param request
* @return
*/
@RequestMapping ( "/handle4" )
public String handle4(HttpServletRequest request){
//do something
return "success" ;
}
}
|