分享

Spring MVC中RequestMapping的各种用法

 时间要去哪 2014-05-22
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";
    }
}

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多