Springmvc构造RESTful详细讲解

Rest介绍

Springmvc构造RESTful详细讲解/blog/1 HTTP GET =>  得到id = 1的blog
Springmvc构造RESTful详细讲解/blog/1 HTTP DELETE => 删除 id = 1的blog
Springmvc构造RESTful详细讲解/blog/1 HTTP PUT =>  更新id = 1的blog
Springmvc构造RESTful详细讲解/blog   HTTP POST =>  新增BLOG

以下详细解一下spring rest使用.

首先,我们带着如下两个问题查看本文。

1.如何在java构造没有扩展名的RESTful url,如 /forms/1,而不是 /forms/1.do

2.浏览器的form标签不支持提交delete,put请求,如何曲线解决

springmvc rest 实现

springmvc的resturl是通过@RequestMapping 及@PathVariable annotation提供的,
    通过如@RequestMapping(value="/blog /{id}",method=RequestMethod.DELETE)即可处理/blog/1 的delete请求.

1Springmvc构造RESTful详细讲解@RequestMapping(value="/blog/{id}",method=RequestMethod.DELETE)
2Springmvc构造RESTful详细讲解public ModelAndView delete(@PathVariable Long id,HttpServletRequest request,HttpServletResponse response) {
3Springmvc构造RESTful详细讲解  blogManager.removeById(id);
4Springmvc构造RESTful详细讲解  return new ModelAndView(LIST_ACTION);
5Springmvc构造RESTful详细讲解}

@RequestMapping @PathVariable如果URL中带参数,则配合使用,如

1Springmvc构造RESTful详细讲解@RequestMapping(value="/blog/{blogId}/message/{msgId}",method=RequestMethod.DELETE)
2Springmvc构造RESTful详细讲解public ModelAndView delete(@PathVariable("blogId") Long blogId,@PathVariable("msgId") Long msgId,HttpServletRequest request,HttpServletResponse response) {
3Springmvc构造RESTful详细讲解}

1.springmvc web.xml配置

 1Springmvc构造RESTful详细讲解<!-- 该servlet为tomcat,jetty等容器提供,将静态资源映射从/改为/static/目录,如原来访问 http://localhost/foo.css ,现在http://localhost/static/foo.css -->
 2Springmvc构造RESTful详细讲解  <servlet-mapping>
 3Springmvc构造RESTful详细讲解  <servlet-name>default</servlet-name>
 4Springmvc构造RESTful详细讲解  <url-pattern>/static/*</url-pattern>
 5Springmvc构造RESTful详细讲解  </servlet-mapping>
 6Springmvc构造RESTful详细讲解  <servlet>
 7Springmvc构造RESTful详细讲解    <servlet-name>springmvc</servlet-name>
 8Springmvc构造RESTful详细讲解    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
 9Springmvc构造RESTful详细讲解    <load-on-startup>1</load-on-startup>
10Springmvc构造RESTful详细讲解  </servlet>
11Springmvc构造RESTful详细讲解
12Springmvc构造RESTful详细讲解  <!-- URL重写filter,用于将访问静态资源http://localhost/foo.css 转为http://localhost/static/foo.css -->
13Springmvc构造RESTful详细讲解  <filter>
14Springmvc构造RESTful详细讲解  <filter-name>UrlRewriteFilter</filter-name>
15Springmvc构造RESTful详细讲解  <filter-class>org.tuckey.web.filters.urlrewrite.UrlRewriteFilter</filter-class>
16Springmvc构造RESTful详细讲解  <init-param>
17Springmvc构造RESTful详细讲解     <param-name>confReloadCheckInterval</param-name>
18Springmvc构造RESTful详细讲解     <param-value>60</param-value>
19Springmvc构造RESTful详细讲解    </init-param>
20Springmvc构造RESTful详细讲解  <init-param>
21Springmvc构造RESTful详细讲解        <param-name>logLevel</param-name>
22Springmvc构造RESTful详细讲解        <param-value>DEBUG</param-value>
23Springmvc构造RESTful详细讲解      </init-param>
24Springmvc构造RESTful详细讲解  </filter>
25Springmvc构造RESTful详细讲解  <filter-mapping>
26Springmvc构造RESTful详细讲解  <filter-name>UrlRewriteFilter</filter-name>
27Springmvc构造RESTful详细讲解  <url-pattern>/*</url-pattern>
28Springmvc构造RESTful详细讲解  </filter-mapping>
29Springmvc构造RESTful详细讲解
30Springmvc构造RESTful详细讲解  <!-- 覆盖default servlet的/, springmvc servlet将处理原来处理静态资源的映射 -->
31Springmvc构造RESTful详细讲解  <servlet-mapping>
32Springmvc构造RESTful详细讲解    <servlet-name>springmvc</servlet-name>
33Springmvc构造RESTful详细讲解    <url-pattern>/</url-pattern>
34Springmvc构造RESTful详细讲解  </servlet-mapping>
35Springmvc构造RESTful详细讲解
36Springmvc构造RESTful详细讲解  <!-- 浏览器不支持put,delete等method,由该filter将/blog?_method=delete转换为标准的http delete方法 -->
37Springmvc构造RESTful详细讲解  <filter>
38Springmvc构造RESTful详细讲解  <filter-name>HiddenHttpMethodFilter</filter-name>
39Springmvc构造RESTful详细讲解  <filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
40Springmvc构造RESTful详细讲解  </filter>
41Springmvc构造RESTful详细讲解
42Springmvc构造RESTful详细讲解  <filter-mapping>
43Springmvc构造RESTful详细讲解  <filter-name>HiddenHttpMethodFilter</filter-name>
44Springmvc构造RESTful详细讲解  <servlet-name>springmvc</servlet-name>
45Springmvc构造RESTful详细讲解  </filter-mapping>

2.webapp/WEB-INF/springmvc-servlet.xml配置,使用如下两个class激活@RequestMapping annotation

1Springmvc构造RESTful详细讲解<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"/>   
2Springmvc构造RESTful详细讲解<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"/>  
3Springmvc构造RESTful详细讲解

3.Controller编写

 1Springmvc构造RESTful详细讲解/**
 2Springmvc构造RESTful详细讲解  * @RequestMapping("/userinfo") 具有层次关系,方法级的将在类一级@RequestMapping之一,
 3Springmvc构造RESTful详细讲解  * 如下面示例, 访问方法级别的@RequestMapping("/new"),则URL为 /userinfo/new
 4Springmvc构造RESTful详细讲解  */
 5Springmvc构造RESTful详细讲解@Controller
 6Springmvc构造RESTful详细讲解@RequestMapping("/userinfo")
 7Springmvc构造RESTful详细讲解public class UserInfoController extends BaseSpringController{
 8Springmvc构造RESTful详细讲解  //默认多列排序,example: username desc,createTime asc
 9Springmvc构造RESTful详细讲解  protected static final String DEFAULT_SORT_COLUMNS = null;
10Springmvc构造RESTful详细讲解
11Springmvc构造RESTful详细讲解  private UserInfoManager userInfoManager;
12Springmvc构造RESTful详细讲解
13Springmvc构造RESTful详细讲解  private final String LIST_ACTION = "redirect:/userinfo";
14Springmvc构造RESTful详细讲解
15Springmvc构造RESTful详细讲解  /**
16Springmvc构造RESTful详细讲解  * 通过spring自动注入
17Springmvc构造RESTful详细讲解  **/
18Springmvc构造RESTful详细讲解  public void setUserInfoManager(UserInfoManager manager) {
19Springmvc构造RESTful详细讲解  this.userInfoManager = manager;
20Springmvc构造RESTful详细讲解  }
21Springmvc构造RESTful详细讲解
22Springmvc构造RESTful详细讲解  /** 列表 */
23Springmvc构造RESTful详细讲解  @RequestMapping
24Springmvc构造RESTful详细讲解  public ModelAndView index(HttpServletRequest request,HttpServletResponse response,UserInfo userInfo) {
25Springmvc构造RESTful详细讲解  PageRequest<Map> pageRequest = newPageRequest(request,DEFAULT_SORT_COLUMNS);
26Springmvc构造RESTful详细讲解  //pageRequest.getFilters(); //add custom filters 
27Springmvc构造RESTful详细讲解
28Springmvc构造RESTful详细讲解  Page page = this.userInfoManager.findByPageRequest(pageRequest);
29Springmvc构造RESTful详细讲解  savePage(page,pageRequest,request);
30Springmvc构造RESTful详细讲解  return new ModelAndView("/userinfo/list","userInfo",userInfo);
31Springmvc构造RESTful详细讲解  }
32Springmvc构造RESTful详细讲解
33Springmvc构造RESTful详细讲解  /** 进入新增 */
34Springmvc构造RESTful详细讲解  @RequestMapping(value="/new")
35Springmvc构造RESTful详细讲解  public ModelAndView _new(HttpServletRequest request,HttpServletResponse response,UserInfo userInfo) throws Exception {
36Springmvc构造RESTful详细讲解  return new ModelAndView("/userinfo/new","userInfo",userInfo);
37Springmvc构造RESTful详细讲解  }
38Springmvc构造RESTful详细讲解
39Springmvc构造RESTful详细讲解  /** 显示 */
40Springmvc构造RESTful详细讲解  @RequestMapping(value="/{id}")
41Springmvc构造RESTful详细讲解  public ModelAndView show(@PathVariable Long id,HttpServletRequest request,HttpServletResponse response) throws Exception {
42Springmvc构造RESTful详细讲解  UserInfo userInfo = (UserInfo)userInfoManager.getById(id);
43Springmvc构造RESTful详细讲解  return new ModelAndView("/userinfo/show","userInfo",userInfo);
44Springmvc构造RESTful详细讲解  }
45Springmvc构造RESTful详细讲解
46Springmvc构造RESTful详细讲解  /** 编辑 */
47Springmvc构造RESTful详细讲解  @RequestMapping(value="/{id}/edit")
48Springmvc构造RESTful详细讲解  public ModelAndView edit(@PathVariable Long id,HttpServletRequest request,HttpServletResponse response) throws Exception {
49Springmvc构造RESTful详细讲解  UserInfo userInfo = (UserInfo)userInfoManager.getById(id);
50Springmvc构造RESTful详细讲解  return new ModelAndView("/userinfo/edit","userInfo",userInfo);
51Springmvc构造RESTful详细讲解  }
52Springmvc构造RESTful详细讲解
53Springmvc构造RESTful详细讲解  /** 保存新增 */
54Springmvc构造RESTful详细讲解  @RequestMapping(method=RequestMethod.POST)
55Springmvc构造RESTful详细讲解  public ModelAndView create(HttpServletRequest request,HttpServletResponse response,UserInfo userInfo) throws Exception {
56Springmvc构造RESTful详细讲解  userInfoManager.save(userInfo);
57Springmvc构造RESTful详细讲解  return new ModelAndView(LIST_ACTION);
58Springmvc构造RESTful详细讲解  }
59Springmvc构造RESTful详细讲解
60Springmvc构造RESTful详细讲解  /** 保存更新 */
61Springmvc构造RESTful详细讲解  @RequestMapping(value="/{id}",method=RequestMethod.PUT)
62Springmvc构造RESTful详细讲解  public ModelAndView update(@PathVariable Long id,HttpServletRequest request,HttpServletResponse response) throws Exception {
63Springmvc构造RESTful详细讲解  UserInfo userInfo = (UserInfo)userInfoManager.getById(id);
64Springmvc构造RESTful详细讲解  bind(request,userInfo);
65Springmvc构造RESTful详细讲解  userInfoManager.update(userInfo);
66Springmvc构造RESTful详细讲解  return new ModelAndView(LIST_ACTION);
67Springmvc构造RESTful详细讲解  }
68Springmvc构造RESTful详细讲解
69Springmvc构造RESTful详细讲解  /** 删除 */
70Springmvc构造RESTful详细讲解  @RequestMapping(value="/{id}",method=RequestMethod.DELETE)
71Springmvc构造RESTful详细讲解  public ModelAndView delete(@PathVariable Long id,HttpServletRequest request,HttpServletResponse response) {
72Springmvc构造RESTful详细讲解  userInfoManager.removeById(id);
73Springmvc构造RESTful详细讲解  return new ModelAndView(LIST_ACTION);
74Springmvc构造RESTful详细讲解  }
75Springmvc构造RESTful详细讲解
76Springmvc构造RESTful详细讲解  /** 批量删除 */
77Springmvc构造RESTful详细讲解  @RequestMapping(method=RequestMethod.DELETE)
78Springmvc构造RESTful详细讲解  public ModelAndView batchDelete(HttpServletRequest request,HttpServletResponse response) {
79Springmvc构造RESTful详细讲解  String[] items = request.getParameterValues("items");
80Springmvc构造RESTful详细讲解  for(int i = 0; i < items.length; i++) {
81Springmvc构造RESTful详细讲解   java.lang.Long id = new java.lang.Long(items[i]);
82Springmvc构造RESTful详细讲解   userInfoManager.removeById(id);
83Springmvc构造RESTful详细讲解  }
84Springmvc构造RESTful详细讲解  return new ModelAndView(LIST_ACTION);
85Springmvc构造RESTful详细讲解  }
86Springmvc构造RESTful详细讲解
87Springmvc构造RESTful详细讲解}
88Springmvc构造RESTful详细讲解
1Springmvc构造RESTful详细讲解/userinfo  => index()
2Springmvc构造RESTful详细讲解  /userinfo/new => _new()
3Springmvc构造RESTful详细讲解  /userinfo/{id} => show()
4Springmvc构造RESTful详细讲解  /userinfo/{id}/edit  => edit()
5Springmvc构造RESTful详细讲解  /userinfo POST => create()
6Springmvc构造RESTful详细讲解  /userinfo/{id} PUT => update()
7Springmvc构造RESTful详细讲解  /userinfo/{id} DELETE => delete()
8Springmvc构造RESTful详细讲解  /userinfo DELETE => batchDelete()
9Springmvc构造RESTful详细讲解

注(不使用 /userinfo/add  => add() 方法是由于add这个方法会被maxthon浏览器当做广告链接过滤掉,因为包含ad字符)

4.jsp 编写

1Springmvc构造RESTful详细讲解<form:form action="${ctx}/userinfo${userInfo.userId}" method="put">
2Springmvc构造RESTful详细讲解</form:form>

生成的html内容如下, 生成一个hidden的_method=put,并于web.xml中的HiddenHttpMethodFilter配合使用,在服务端将post请求改为put请求

1Springmvc构造RESTful详细讲解<form id="userInfo" action="/springmvc_rest_demo/userinfo/2" method="post">
2Springmvc构造RESTful详细讲解  <input type="hidden" name="_method" value="put"/>
3Springmvc构造RESTful详细讲解</form>

另外一种方法是你可以使用ajax发送put,delete请求.

5.静态资源的URL重写

如上我们描述,现因为将default servlet映射至/static/的子目录,现我们访问静态资源将会带一个/static/前缀.

如 /foo.gif, 现在访问该文件将是 /static/foo.gif.

那如何避免这个前缀呢,那就是应用URL rewrite,现我们使用 http://tuckey.org/urlrewrite/, 重写规则如下

1Springmvc构造RESTful详细讲解<urlrewrite>
2Springmvc构造RESTful详细讲解   <!-- 访问jsp及jspx将不rewrite url,其它.js,.css,.gif等将重写,如 /foo.gif => /static/foo.gif -->
3Springmvc构造RESTful详细讲解   <rule>
4Springmvc构造RESTful详细讲解    <condition operator="notequal" next="and" type="request-uri">.*.jsp</condition>
5Springmvc构造RESTful详细讲解    <condition operator="notequal" next="and" type="request-uri">.*.jspx</condition>
6Springmvc构造RESTful详细讲解     <from>^(/.*\..*)$</from>
7Springmvc构造RESTful详细讲解     <to>/static$1</to>
8Springmvc构造RESTful详细讲解   </rule>
9Springmvc构造RESTful详细讲解</urlrewrite>
上一篇:/etc/resolv.conf文件中的search项作用;如何保持resolv.conf文件内容不被修改


下一篇:hdu-3333 Turing Tree 离线区间+树状数组(区间不同数的和)