spring 3.0应用 - springmvc构造RESTful URL详细讲解
时间:2011-01-03 javaeye badqiu
由于下一版本的rapid-framwork需要集成spring RESTful URL,所以研究了一下怎么搭建.并碰到了一下问题。
springmvc 3.0 中增加 RESTful URL功能,构造出类似javaeye现在的URL。 rest介绍
比如如下URL
Java代码
/blog/1 HTTP GET => 得到id = 1的blog
/blog/1 HTTP DELETE => 删除 id = 1的blog
/blog/1 HTTP PUT => 更新id = 1的blog
/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请求.
Java代码
@RequestMapping(value="/blog/{id}",method=RequestMethod.DELETE)
public ModelAndView delete(@PathVariable Long id,HttpServletRequest request,HttpServletResponse response) {
blogManager.removeById(id);
return new ModelAndView(LIST_ACTION);
}
@RequestMapping @PathVariable如果URL中带参数,则配合使用,如
Java代码
@RequestMapping(value="/blog/{blogId}/message/{msgId}",method=RequestMethod.DELETE)
public ModelAndView delete(@PathVariable("blogId") Long blogId,@PathVariable("msgId") Long msgId,HttpServletRequest request,HttpServletResponse response) {
}
spring 3.0应用 - springmvc构造RESTful URL详细讲解(2)
时间:2011-01-03 javaeye badqiu
spring rest配置指南
1.springmvc web.xml配置
Xml代码
<!-- 该servlet为tomcat,jetty等容器提供,将静态资源映射从/改为/static/目录,如原来访问 http://localhost/foo.css ,现在http://localhost/static/foo.css -->
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>/static/*</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<!-- URL重写filter,用于将访问静态资源http://localhost/foo.css 转为http://localhost/static/foo.css -->
<filter>
<filter-name>UrlRewriteFilter</filter-name>
<filter-class>org.tuckey.web.filters.urlrewrite.UrlRewriteFilter</filter-class>
<init-param>
<param-name>confReloadCheckInterval</param-name>
<param-value>60</param-value>
</init-param>
<init-param>
<param-name>logLevel</param-name>
<param-value>DEBUG</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>UrlRewriteFilter</filter-name>
<url-pattern>/*</url-pattern&g
|