/blog/1 HTTP GET => 得到id = 1的blog
![](http://img.e-com-net.com/image/product/ff3435bbf02e4f2c9b474eef455f6c26.gif)
/blog/1 HTTP DELETE => 删除 id = 1的blog
![](http://img.e-com-net.com/image/product/ff3435bbf02e4f2c9b474eef455f6c26.gif)
/blog/1 HTTP PUT => 更新id = 1的blog
![](http://img.e-com-net.com/image/product/ff3435bbf02e4f2c9b474eef455f6c26.gif)
/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请求.
1
![](http://img.e-com-net.com/image/product/ff3435bbf02e4f2c9b474eef455f6c26.gif)
@RequestMapping(value="/blog/{id}",method=RequestMethod.DELETE)
2
![](http://img.e-com-net.com/image/product/a1f351c222f2407bb0921afbb62a3c11.gif)
public ModelAndView delete(@PathVariable Long id,HttpServletRequest request,HttpServletResponse response)
{
3
blogManager.removeById(id);
4
return new ModelAndView(LIST_ACTION);
5
}
@RequestMapping @PathVariable如果URL中带参数,则配合使用,如
1
![](http://img.e-com-net.com/image/product/ff3435bbf02e4f2c9b474eef455f6c26.gif)
@RequestMapping(value="/blog/{blogId}/message/{msgId}",method=RequestMethod.DELETE)
2
![](http://img.e-com-net.com/image/product/a1f351c222f2407bb0921afbb62a3c11.gif)
public ModelAndView delete(@PathVariable("blogId") Long blogId,@PathVariable("msgId") Long msgId,HttpServletRequest request,HttpServletResponse response)
{
3
}
1.springmvc web.xml配置
1
![](http://img.e-com-net.com/image/product/ff3435bbf02e4f2c9b474eef455f6c26.gif)
<!-- 该servlet为tomcat,jetty等容器提供,将静态资源映射从/改为/static/目录,如原来访问 http://localhost/foo.css ,现在http://localhost/static/foo.css -->
2
![](http://img.e-com-net.com/image/product/ff3435bbf02e4f2c9b474eef455f6c26.gif)
<servlet-mapping>
3
![](http://img.e-com-net.com/image/product/ff3435bbf02e4f2c9b474eef455f6c26.gif)
<servlet-name>default</servlet-name>
4
![](http://img.e-com-net.com/image/product/ff3435bbf02e4f2c9b474eef455f6c26.gif)
<url-pattern>/static/*</url-pattern>
5
![](http://img.e-com-net.com/image/product/ff3435bbf02e4f2c9b474eef455f6c26.gif)
</servlet-mapping>
6
![](http://img.e-com-net.com/image/product/ff3435bbf02e4f2c9b474eef455f6c26.gif)
<servlet>
7
![](http://img.e-com-net.com/image/product/ff3435bbf02e4f2c9b474eef455f6c26.gif)
<servlet-name>springmvc</servlet-name>
8
![](http://img.e-com-net.com/image/product/ff3435bbf02e4f2c9b474eef455f6c26.gif)
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
9
![](http://img.e-com-net.com/image/product/ff3435bbf02e4f2c9b474eef455f6c26.gif)
<load-on-startup>1</load-on-startup>
10
![](http://img.e-com-net.com/image/product/ff3435bbf02e4f2c9b474eef455f6c26.gif)
</servlet>
11
12
![](http://img.e-com-net.com/image/product/ff3435bbf02e4f2c9b474eef455f6c26.gif)
<!-- URL重写filter,用于将访问静态资源http://localhost/foo.css 转为http://localhost/static/foo.css -->
13
![](http://img.e-com-net.com/image/product/ff3435bbf02e4f2c9b474eef455f6c26.gif)
<filter>
14
![](http://img.e-com-net.com/image/product/ff3435bbf02e4f2c9b474eef455f6c26.gif)
<filter-name>UrlRewriteFilter</filter-name>
15
![](http://img.e-com-net.com/image/product/ff3435bbf02e4f2c9b474eef455f6c26.gif)
<filter-class>org.tuckey.web.filters.urlrewrite.UrlRewriteFilter</filter-class>
16
![](http://img.e-com-net.com/image/product/ff3435bbf02e4f2c9b474eef455f6c26.gif)
<init-param>
17
![](http://img.e-com-net.com/image/product/ff3435bbf02e4f2c9b474eef455f6c26.gif)
<param-name>confReloadCheckInterval</param-name>
18
![](http://img.e-com-net.com/image/product/ff3435bbf02e4f2c9b474eef455f6c26.gif)
<param-value>60</param-value>
19
![](http://img.e-com-net.com/image/product/ff3435bbf02e4f2c9b474eef455f6c26.gif)
</init-param>
20
![](http://img.e-com-net.com/image/product/ff3435bbf02e4f2c9b474eef455f6c26.gif)
<init-param>
21
![](http://img.e-com-net.com/image/product/ff3435bbf02e4f2c9b474eef455f6c26.gif)
<param-name>logLevel</param-name>
22
![](http://img.e-com-net.com/image/product/ff3435bbf02e4f2c9b474eef455f6c26.gif)
<param-value>DEBUG</param-value>
23
![](http://img.e-com-net.com/image/product/ff3435bbf02e4f2c9b474eef455f6c26.gif)
</init-param>
24
![](http://img.e-com-net.com/image/product/ff3435bbf02e4f2c9b474eef455f6c26.gif)
</filter>
25
![](http://img.e-com-net.com/image/product/ff3435bbf02e4f2c9b474eef455f6c26.gif)
<filter-mapping>
26
![](http://img.e-com-net.com/image/product/ff3435bbf02e4f2c9b474eef455f6c26.gif)
<filter-name>UrlRewriteFilter</filter-name>
27
![](http://img.e-com-net.com/image/product/ff3435bbf02e4f2c9b474eef455f6c26.gif)
<url-pattern>/*</url-pattern>
28
![](http://img.e-com-net.com/image/product/ff3435bbf02e4f2c9b474eef455f6c26.gif)
</filter-mapping>
29
30
![](http://img.e-com-net.com/image/product/ff3435bbf02e4f2c9b474eef455f6c26.gif)
<!-- 覆盖default servlet的/, springmvc servlet将处理原来处理静态资源的映射 -->
31
![](http://img.e-com-net.com/image/product/ff3435bbf02e4f2c9b474eef455f6c26.gif)
<servlet-mapping>
32
![](http://img.e-com-net.com/image/product/ff3435bbf02e4f2c9b474eef455f6c26.gif)
<servlet-name>springmvc</servlet-name>
33
![](http://img.e-com-net.com/image/product/ff3435bbf02e4f2c9b474eef455f6c26.gif)
<url-pattern>/</url-pattern>
34
![](http://img.e-com-net.com/image/product/ff3435bbf02e4f2c9b474eef455f6c26.gif)
</servlet-mapping>
35
36
![](http://img.e-com-net.com/image/product/ff3435bbf02e4f2c9b474eef455f6c26.gif)
<!-- 浏览器不支持put,delete等method,由该filter将/blog?_method=delete转换为标准的http delete方法 -->
37
![](http://img.e-com-net.com/image/product/ff3435bbf02e4f2c9b474eef455f6c26.gif)
<filter>
38
![](http://img.e-com-net.com/image/product/ff3435bbf02e4f2c9b474eef455f6c26.gif)
<filter-name>HiddenHttpMethodFilter</filter-name>
39
![](http://img.e-com-net.com/image/product/ff3435bbf02e4f2c9b474eef455f6c26.gif)
<filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
40
![](http://img.e-com-net.com/image/product/ff3435bbf02e4f2c9b474eef455f6c26.gif)
</filter>
41
42
![](http://img.e-com-net.com/image/product/ff3435bbf02e4f2c9b474eef455f6c26.gif)
<filter-mapping>
43
![](http://img.e-com-net.com/image/product/ff3435bbf02e4f2c9b474eef455f6c26.gif)
<filter-name>HiddenHttpMethodFilter</filter-name>
44
![](http://img.e-com-net.com/image/product/ff3435bbf02e4f2c9b474eef455f6c26.gif)
<servlet-name>springmvc</servlet-name>
45
![](http://img.e-com-net.com/image/product/ff3435bbf02e4f2c9b474eef455f6c26.gif)
</filter-mapping>
2.webapp/WEB-INF/springmvc-servlet.xml配置,使用如下两个class激活@RequestMapping annotation
1
![](http://img.e-com-net.com/image/product/ff3435bbf02e4f2c9b474eef455f6c26.gif)
<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"/>
2
![](http://img.e-com-net.com/image/product/ff3435bbf02e4f2c9b474eef455f6c26.gif)
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"/>
3
3.Controller编写
1
/**
2
* @RequestMapping("/userinfo") 具有层次关系,方法级的将在类一级@RequestMapping之一,
3
* 如下面示例, 访问方法级别的@RequestMapping("/new"),则URL为 /userinfo/new
4
*/
5
@Controller
6
@RequestMapping("/userinfo")
7
public class UserInfoController extends BaseSpringController{
8
//默认多列排序,example: username desc,createTime asc
9
protected static final String DEFAULT_SORT_COLUMNS = null;
10![](http://img.e-com-net.com/image/product/68629797db704c09a34e3f22928f69ff.gif)
11
private UserInfoManager userInfoManager;
12![](http://img.e-com-net.com/image/product/68629797db704c09a34e3f22928f69ff.gif)
13
private final String LIST_ACTION = "redirect:/userinfo";
14![](http://img.e-com-net.com/image/product/68629797db704c09a34e3f22928f69ff.gif)
15
/**
16
* 通过spring自动注入
17
**/
18
public void setUserInfoManager(UserInfoManager manager) {
19
this.userInfoManager = manager;
20
}
21![](http://img.e-com-net.com/image/product/68629797db704c09a34e3f22928f69ff.gif)
22
/** 列表 */
23
@RequestMapping
24
public ModelAndView index(HttpServletRequest request,HttpServletResponse response,UserInfo userInfo) {
25
PageRequest<Map> pageRequest = newPageRequest(request,DEFAULT_SORT_COLUMNS);
26
//pageRequest.getFilters(); //add custom filters
27![](http://img.e-com-net.com/image/product/68629797db704c09a34e3f22928f69ff.gif)
28
Page page = this.userInfoManager.findByPageRequest(pageRequest);
29
savePage(page,pageRequest,request);
30
return new ModelAndView("/userinfo/list","userInfo",userInfo);
31
}
32![](http://img.e-com-net.com/image/product/68629797db704c09a34e3f22928f69ff.gif)
33
/** 进入新增 */
34
@RequestMapping(value="/new")
35
public ModelAndView _new(HttpServletRequest request,HttpServletResponse response,UserInfo userInfo) throws Exception {
36
return new ModelAndView("/userinfo/new","userInfo",userInfo);
37
}
38![](http://img.e-com-net.com/image/product/68629797db704c09a34e3f22928f69ff.gif)
39
/** 显示 */
40
@RequestMapping(value="/{id}")
41
public ModelAndView show(@PathVariable Long id,HttpServletRequest request,HttpServletResponse response) throws Exception {
42
UserInfo userInfo = (UserInfo)userInfoManager.getById(id);
43
return new ModelAndView("/userinfo/show","userInfo",userInfo);
44
}
45![](http://img.e-com-net.com/image/product/68629797db704c09a34e3f22928f69ff.gif)
46
/** 编辑 */
47
@RequestMapping(value="/{id}/edit")
48
public ModelAndView edit(@PathVariable Long id,HttpServletRequest request,HttpServletResponse response) throws Exception {
49
UserInfo userInfo = (UserInfo)userInfoManager.getById(id);
50
return new ModelAndView("/userinfo/edit","userInfo",userInfo);
51
}
52![](http://img.e-com-net.com/image/product/68629797db704c09a34e3f22928f69ff.gif)
53
/** 保存新增 */
54
@RequestMapping(method=RequestMethod.POST)
55
public ModelAndView create(HttpServletRequest request,HttpServletResponse response,UserInfo userInfo) throws Exception {
56
userInfoManager.save(userInfo);
57
return new ModelAndView(LIST_ACTION);
58
}
59![](http://img.e-com-net.com/image/product/68629797db704c09a34e3f22928f69ff.gif)
60
/** 保存更新 */
61
@RequestMapping(value="/{id}",method=RequestMethod.PUT)
62
public ModelAndView update(@PathVariable Long id,HttpServletRequest request,HttpServletResponse response) throws Exception {
63
UserInfo userInfo = (UserInfo)userInfoManager.getById(id);
64
bind(request,userInfo);
65
userInfoManager.update(userInfo);
66
return new ModelAndView(LIST_ACTION);
67
}
68![](http://img.e-com-net.com/image/product/68629797db704c09a34e3f22928f69ff.gif)
69
/** 删除 */
70
@RequestMapping(value="/{id}",method=RequestMethod.DELETE)
71
public ModelAndView delete(@PathVariable Long id,HttpServletRequest request,HttpServletResponse response) {
72
userInfoManager.removeById(id);
73
return new ModelAndView(LIST_ACTION);
74
}
75![](http://img.e-com-net.com/image/product/68629797db704c09a34e3f22928f69ff.gif)
76
/** 批量删除 */
77
@RequestMapping(method=RequestMethod.DELETE)
78
public ModelAndView batchDelete(HttpServletRequest request,HttpServletResponse response) {
79
String[] items = request.getParameterValues("items");
80
for(int i = 0; i < items.length; i++) {
81
java.lang.Long id = new java.lang.Long(items[i]);
82
userInfoManager.removeById(id);
83
}
84
return new ModelAndView(LIST_ACTION);
85
}
86![](http://img.e-com-net.com/image/product/68629797db704c09a34e3f22928f69ff.gif)
87
}
88
1
![](http://img.e-com-net.com/image/product/ff3435bbf02e4f2c9b474eef455f6c26.gif)
/userinfo => index()
2
![](http://img.e-com-net.com/image/product/ff3435bbf02e4f2c9b474eef455f6c26.gif)
/userinfo/new => _new()
3
![](http://img.e-com-net.com/image/product/ff3435bbf02e4f2c9b474eef455f6c26.gif)
/userinfo/{id} => show()
4
![](http://img.e-com-net.com/image/product/ff3435bbf02e4f2c9b474eef455f6c26.gif)
/userinfo/{id}/edit => edit()
5
![](http://img.e-com-net.com/image/product/ff3435bbf02e4f2c9b474eef455f6c26.gif)
/userinfo POST => create()
6
![](http://img.e-com-net.com/image/product/ff3435bbf02e4f2c9b474eef455f6c26.gif)
/userinfo/{id} PUT => update()
7
![](http://img.e-com-net.com/image/product/ff3435bbf02e4f2c9b474eef455f6c26.gif)
/userinfo/{id} DELETE => delete()
8
![](http://img.e-com-net.com/image/product/ff3435bbf02e4f2c9b474eef455f6c26.gif)
/userinfo DELETE => batchDelete()
9
注(不使用 /userinfo/add => add() 方法是由于add这个方法会被maxthon浏览器当做广告链接过滤掉,因为包含ad字符)
4.jsp 编写
1
![](http://img.e-com-net.com/image/product/ff3435bbf02e4f2c9b474eef455f6c26.gif)
<form:form action="${ctx}/userinfo${userInfo.userId}" method="put">
2
![](http://img.e-com-net.com/image/product/ff3435bbf02e4f2c9b474eef455f6c26.gif)
</form:form>
生成的html内容如下, 生成一个hidden的_method=put,并于web.xml中的HiddenHttpMethodFilter配合使用,在服务端将post请求改为put请求
1
![](http://img.e-com-net.com/image/product/ff3435bbf02e4f2c9b474eef455f6c26.gif)
<form id="userInfo" action="/springmvc_rest_demo/userinfo/2" method="post">
2
![](http://img.e-com-net.com/image/product/ff3435bbf02e4f2c9b474eef455f6c26.gif)
<input type="hidden" name="_method" value="put"/>
3
![](http://img.e-com-net.com/image/product/ff3435bbf02e4f2c9b474eef455f6c26.gif)
</form>
另外一种方法是你可以使用ajax发送put,delete请求.
5.静态资源的URL重写
如上我们描述,现因为将default servlet映射至/static/的子目录,现我们访问静态资源将会带一个/static/前缀.
如 /foo.gif, 现在访问该文件将是 /static/foo.gif.
那如何避免这个前缀呢,那就是应用URL rewrite,现我们使用 http://tuckey.org/urlrewrite/, 重写规则如下
1
![](http://img.e-com-net.com/image/product/ff3435bbf02e4f2c9b474eef455f6c26.gif)
<urlrewrite>
2
![](http://img.e-com-net.com/image/product/ff3435bbf02e4f2c9b474eef455f6c26.gif)
<!-- 访问jsp及jspx将不rewrite url,其它.js,.css,.gif等将重写,如 /foo.gif => /static/foo.gif -->
3
![](http://img.e-com-net.com/image/product/ff3435bbf02e4f2c9b474eef455f6c26.gif)
<rule>
4
![](http://img.e-com-net.com/image/product/ff3435bbf02e4f2c9b474eef455f6c26.gif)
<condition operator="notequal" next="and" type="request-uri">.*.jsp</condition>
5
![](http://img.e-com-net.com/image/product/ff3435bbf02e4f2c9b474eef455f6c26.gif)
<condition operator="notequal" next="and" type="request-uri">.*.jspx</condition>
6
![](http://img.e-com-net.com/image/product/ff3435bbf02e4f2c9b474eef455f6c26.gif)
<from>^(/.*\..*)$</from>
7
![](http://img.e-com-net.com/image/product/ff3435bbf02e4f2c9b474eef455f6c26.gif)
<to>/static$1</to>
8
![](http://img.e-com-net.com/image/product/ff3435bbf02e4f2c9b474eef455f6c26.gif)
</rule>
9
![](http://img.e-com-net.com/image/product/ff3435bbf02e4f2c9b474eef455f6c26.gif)
</urlrewrite>
静态资源访问<mvc:resources>
如果在DispatcherServlet中设置url-pattern为 /则必须对静态资源进行访问处理。
spring mvc 的<mvc:resources mapping="" location="">实现对静态资源进行映射访问。
如下是对js文件访问配置:
<mvc:resources location="/js/" mapping="/js/**"/>