SpringMVC学习01

ant风格url

spring官方解释

/**
 * In a Servlet environment only: the path mapping URIs (e.g. "/myPath.do").
 * Ant-style path patterns are also supported (e.g. "/myPath/*.do").
 * At the method level, relative paths (e.g. "edit.do") are supported within
 * the primary mapping expressed at the type level. Path mapping URIs may
 * contain placeholders (e.g. "/${connect}")
 * <p><b>Supported at the type level as well as at the method level!</b>
 * When used at the type level, all method-level mappings inherit
 * this primary mapping, narrowing it for a specific handler method.
 * @see org.springframework.web.bind.annotation.ValueConstants#DEFAULT_NONE
 * @since 4.2
 */
/**
 * ? 匹配一个字符
 * * 匹配任意多个字符或者一层路径
 * ** 多层路径
 * 
 * @return
 */
@RequestMapping("/antTest0?")
public String antTest01() {
	
	return "";
}

路径占位符

@RequestMapping("/user/{id}")
public String antTest01(@PathVariable("id")String id) {
	
	return "";
}

REST风格-重点

** Resources ** Respresentation ** State Transfre 资源表现层状态转化

操作步骤: 1. web.xml加入HiddenHttpMethodFilter 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>
	<url-pattern>/*</url-pattern>
</filter-mapping>	
  1. Controller注解指定请求类型 BookController.java

    @Controller
    public class BookController {
    	
    	@RequestMapping(value = "/book", method = RequestMethod.POST)
    	public String addBook() {
    		System.out.println("新增了图书");
    		return "success";
    	}
    	
    	@RequestMapping(value = "/book/{bid}", method = RequestMethod.DELETE)
    	public String deleteBook(@PathVariable("bid") Integer id) {
    		System.out.println("删除了" +id+"图书");
    		return "success";
    	}
    	
    	@RequestMapping(value = "/book/{bid}", method = RequestMethod.GET)
    	public String getBook(@PathVariable("bid") Integer id) {
    		System.out.println("查询到"+id+"图书");
    		return "success";
    	}
    	
    	@RequestMapping(value = "/book/{bid}", method = RequestMethod.PUT)
    	public String updateBook(@PathVariable("bid") Integer id) {
    		System.out.println("修改" +id+"图书");
    		return "success";
    	}
    }
    
  2. 页面指定_method index.jsp

    <!-- 新增图书 -->
    <form action="book" method="post">
    	<input type="text" name="_method" value="POST" />
    	<input type="submit" value="提交新增图书" />
    </form>
    <!-- 删除图书 -->
    <form action="book/1" method="post">
    	<input type="text" name="_method" value="delete" />
    	<input type="submit" value="提交删除图书" />
    </form>
    
    <!-- 查找图书 -->
    <form action="book/2" method="post">
    	<input type="text" name="_method" value="get" />
    	<input type="submit" value="提交查找图书" />
    </form>	
    
    <!-- 修改图书 -->
    <form action="book/6" method="post">
    	<input type="text" name="_method" value="put" />
    	<input type="submit" value="提交修改图书" />
    </form>	
    

问题:在高版本tomcat上,会不支持put,和delete请求

解决方式:
1.加入 @ResponseBody 注解。 
2.请求先转给一个Controller,再返回jsp页面。 
3.tomcat换到7.0以及以下版本。