说明:本文章的内容转载至:https://my.oschina.net/happyBKs/blog/411547 如有侵权的地方,请联系本人,本人将会立即删除!
前面说了利用@RequestMapping标识控制来进行请求url向物理视图的映射。但是 ,这种对请求的映射不仅仅不局限在标示的方法的返回值对请求url上,还可以对请求的其属性做出约定,如请求的method,是get还是post。如果做出了method的条件限定,当请求的url即使映射上了,method不符合的话也不能生成物理视图并转发到目标页面。
这里,我们定义了一个控制器,如下:
package com.happyBKs.springmvc.handlers; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; @RequestMapping("/c3") @Controller public class RMHandler { @RequestMapping(value="/success",method=RequestMethod.POST) public String handle() { return "successrm"; } }这里注意方法的@RequestMapping的value就是原先对返回值的限定。method则是对请求method属性的限定。
我们在请求页面创造两个请求,一个超链接和一个form表单,它们请求的url一样,但是一个是get方法一个是post方法。我们点击之后发现,get请求无法转发到目标页面,post可以.
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>Insert title here</title> </head> <body> <p>1</p> <a href="c3/success">try</a> <p>2</p> <form action="c3/success" method="post"> <input type="submit" value="post it"/> </form> </body> </html>springmvc.xml、web.xml参照上一次的文章。
点击超链接,结果显示: 返回,点击表单请求: