springMvc中的Url模板映射

URL模版映射

主要是为请求restfull设计模式

Restfull软件架构设计模式:请求更间接,更安全,方便于搜索引擎收录

list.jsp:

[html] view plain copy
  1. <body>  
  2. <table border="1" style="color: blue">  
  3. <tr>  
  4. <td>姓名</td>  
  5. <td>生日</td>  
  6. <td>性别</td>  
  7. <td>地址</td>  
  8. <td>操作</td>  
  9. </tr>  
  10.   
  11. <c:forEach items="${userList }" var="user">  
  12.     <tr>  
  13.     <td>${user.username }</td>  
  14.     <td>${user.birthday }</td>  
  15.     <td>${user.sex }</td>  
  16.     <td>${user.address }</td>  
  17.     <td>  
  18.     <a href="${pageContext.request.contextPath }/user/update.do?id= ${user.id}修改</a>  
  19. </td>  
  20. </tr>  
  21. </c:forEach>  
  22. </table>  
  23. </body>  

普通模式修改:

[html] view plain copy
  1. <a href="${pageContext.request.contextPath }/user/update.do?id= ${user.id}修改</a>  

代码:

[java] view plain copy
  1. @RequestMapping("update")  
  2.     public String update(Integer id){  
  3.         System.out.println(id);  
  4.         return "redirect:list.do";  
  5.     }  

url模版映射过程

springMvc中的Url模板映射

url模版映射功能:
请求参数映射到{id}

{id}传递到方法里面的参数id

通过@Pathvariable把{id}传递到方法里面的id

利用servlet拦截请求目录功能,实现无扩展名真正的restfull风格

Restfull风格设计

[html] view plain copy
  1. <a href="${pageContext.request.contextPath }/rest/user/updateByID/${user.id }">修改</a>  

Web.xml拦截方式:在rest目录下所有请求都被拦截,servlet可以拦截目录。

[html] view plain copy
  1. <servlet-mapping>  
  2. <servlet-name>springmvc</servlet-name>  
  3. <url-pattern>/rest/*</url-pattern>  
  4. </servlet-mapping>  

{}:匹配接受页面Url路径参数

@Pathariable:{}里面参数注入后面参数里面

[java] view plain copy
  1. @RequestMapping("update/{id}")  
  2.     public String update(@PathVariable Integer id){  
  3.         System.out.println(id);  
  4.         return "redirect:user/list.do";  
  5.     }