SpringMVC(二) 请求地址映射 @RequestMapping注解

@RequestMapping("/User")  // 指定控制器映射的URL,请求URL要加上User

 

@RequestMapping(value="/user/*/index",method=RequestMethod.POST)

value ="/user/*/index"

 匹配 user/aa/index、user/bbb/index 等URL

value ="/user/**/index"

匹配 user/index、user/aa/bb/index等URL

value="/user/index??"

匹配 user/indexaa、user/indexbb等URL

value="/user/{userId}"

匹配 user/123、user/abc等URL

value="/user/**/{userId}"

匹配 user/aaa/bbb/123、user/aaa/456等URL

value="/user/{userId}/user/{userId2}/detail"

匹配 user/123/user/123/detail 等URL

 

  // 方法中的占位符可以通过@PathVariable 绑定到方法入参中
@RequestMapping(value="{userId}",method=RequestMethod.POST)
public ModelAndView index(@PathVariable("userId")String userId) {
	System.out.println(userId);
	// 返回指定的jsp页面  
	ModelAndView mav = new ModelAndView("message");
	return mav;
}
 
 
// 不建议使用这种写法,原因是不指定参数名,只有在编译时打开debug开关(javac - debug)时才可行
@RequestMapping(value="{userId}",method=RequestMethod.POST)
public ModelAndView index(@PathVariable String userId) {
	System.out.println(userId);
	// 返回指定的jsp页面  
	ModelAndView mav = new ModelAndView("message");
	return mav;

}

 method 方法

序号

请求方法

说明

1

GET

多次执行同一GET请求,不会对系统造成影响,GET方法具有幂等性【指多个相同请求相同的结果】。GET请求可以充分使用客户端的缓存。

2

POST

通常表示“创建一个新资源”,但它既不安全也不具有幂等性(多次操作会产生多个新资源)。

3

DELETE

表示删除一个资源(删除不存在的东西没有任何问题)

4

PUT

幂等性同样适用于PUT(基本含议是“更新资源数据,如果资源不存在的话,则根据此URL创建一个新的资源”)

 

// 所有URL为/delete的请求由index方法处理(任何请求)
@RequestMapping(value="/delete")
public ModelAndView index() {
	// 返回指定的jsp页面  
	ModelAndView mav = new ModelAndView("message");
	return mav;
}

// 所有URL 为/delete的请求且请求方法为DELETE的请求由index 方法处理
@RequestMapping(value="/delete",method=RequestMethod.DELETE)
public ModelAndView index() {
	// 返回指定的jsp页面  
	ModelAndView mav = new ModelAndView("message");
	return mav;
}

 

 html中form表单只支持GET与POST请求,而DELETE、PUT等method并不支持,spring3.0添加了一个过滤器,可以将这些请求转换为标准的http方法,使得支持GET、POST、PUT与DELETE请求

 

配置web.xml

<filter> 
    <filter-name>HiddenHttpMethodFilter</filter-name> 
    <filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class> 
</filter> 
 
<filter-mapping> 
    <filter-name>HiddenHttpMethodFilter</filter-name> 
    <servlet-name>spring</servlet-name> 
</filter-mapping> 

 

普通页面提交PUT请求,只需添加_method

 

 <input type="hidden" name="_method" value="put"/>  

猜你喜欢

转载自llyilo.iteye.com/blog/2366195