SpringMVC-2

SpringMVC

  • 21.限定请求方法的映射
  • 22.限定请求参数的映射

21.限定请求方法的映射

用到了框架提供的RequestMethod枚举类,源代码截图:

public enum RequestMethod {
     
	GET,HEAD,POST,PUT,PATCH,DELETE,OPTIONS,TRACE
}
@RequestMapping(value = "show6.do", method = RequestMethod.POST)
public ModelAndView test6(){
     
    ModelAndView mv = new ModelAndView();
    mv.setViewName("index");
    mv.addObject("msg", "限定请求方法的映射:POST");
    return mv;
}

测试:

SpringMVC-2_第1张图片
限定多种请求方法

@RequestMapping(value = "show7.do", method = {
     RequestMethod.POST, RequestMethod.GET})
public ModelAndView test7(){
     
    ModelAndView mv = new ModelAndView();
    mv.setViewName("index");
    mv.addObject("msg", "限定请求方法的映射:POST");
    return mv;
}

测试:

SpringMVC-2_第2张图片

22.限定请求参数的映射

@RequestMapping(value=””,params=””)
params=”userId”:请求参数中必须带有userId
params=”!userId”:请求参数中不能包含userId
params=”userId=1”:请求参数中userId必须为1
params=”userId!=1”:请求参数中userId必须不为1,参数中可以不包含userId
params={“userId”, ”name”}:请求参数中必须有userId,name参数

package com.kgc.springmvc.demo02.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

@RequestMapping("index")
@Controller
public class IndexController {
     

    @RequestMapping(value = "show8.do", params = "id")
    public ModelAndView test8() {
     
        ModelAndView mv = new ModelAndView("index");
        mv.addObject("msg", "springmvc的映射之限定请求参数,id");
        return mv;
    }

    @RequestMapping(value = "show9.do", params = "!id")
    public ModelAndView test9() {
     
        ModelAndView mv = new ModelAndView("index");
        mv.addObject("msg", "springmvc的映射之限定请求参数,!id");
        return mv;
    }

    @RequestMapping(value = "show10.do", params = "id=1")
    public ModelAndView test10() {
     
        ModelAndView mv = new ModelAndView("index");
        mv.addObject("msg", "springmvc的映射之限定请求参数,id=1");
        return mv;
    }

    @RequestMapping(value = "show11.do", params = "id!=1")
    public ModelAndView test11() {
     
        ModelAndView mv = new ModelAndView("index");
        mv.addObject("msg", "springmvc的映射之限定请求参数,id!=1");
        return mv;
    }

    @RequestMapping(value = "show12.do", params = {
      "id", "name" })
    public ModelAndView test12() {
     
        ModelAndView mv = new ModelAndView("index");
        mv.addObject("msg", "springmvc的映射之限定请求参数,id,name");
        return mv;
    }
}

SpringMVC-2_第3张图片

你可能感兴趣的:(java)