Spring mvc窄化访问的方法梳理。


 

在使用注解进行配置Spring Controller 的时候可以利用多种方法将访问进行“窄化”处理。


1、给Controller 添加 @RequestMapping()

@Controller
@RequestMapping("/spring")
public class HelloWorldController{
    @RequestMapping("/hello")
    public ModelAndView hello(){
        ModelAndView modelAndView=new ModelAndView();
        modelAndView.setViewName("hello");
        modelAndView.addObject("msg", "mys");
        return modelAndView;
        
    }
}
2、通过指定请求方法来进行限定

 

@Controller

 

@RequestMapping("/customers/**")

 

public class RequestMethodController {

 

@RequestMapping(value="/regist",method=RequestMethod.GET)

 

public String showForm(){

 

return "/regist";

 

}

 

@RequestMapping(value="regist",method=RequestMethod.POST)

 

public String  submitForm(){

 

return "/regist";

 

}

 

}

 

 3、通过指定请求中必须包含或不能包含指定的参数名来限定

@RequestMapping(value="/regist",method=RequestMethod.GET,params="create")
public String showFormWithParam(){
    return "/regist2";
}
//指定 不包含指定参数名
@RequestMapping(value="/regist",method=RequestMethod.GET,params="!create")
public String showFormWithParam(){
    return "/regist2";
}
//指定参数名必须等于某值的请求
@RequestMapping(value="/regist",method=RequestMethod.GET,params="create=person")
public String showFormWithParamVaue(){
    return "/regist";
}

 

posted @ 2016-11-02 22:44  sanemu  阅读(432)  评论(0)    收藏  举报