http://www.blogjava.net/soken/articles/371548.html
Rest介绍
/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请求.1@RequestMapping(value=”/blog/{id}”,method=RequestMethod.DELETE) 2public ModelAndView delete(@PathVariable Long id,HttpServletRequest request,HttpServletResponse response) { 3 blogManager.removeById(id); 4 return new ModelAndView(LIST_ACTION); 5}
@RequestMapping @PathVariable如果URL中带参数,则配合使用,如1@RequestMapping(value=”/blog/{blogId}/message/{msgId}”,method=RequestMethod.DELETE) 2public ModelAndView delete(@PathVariable(“blogId”) Long blogId,@PathVariable(“msgId”) Long msgId,HttpServletRequest request,HttpServletResponse response) { 3}
1.springmvc web.xml配置1 2 3 default 4 /static/* 5 6 7 springmvc 8 org.springframework.web.servlet.DispatcherServlet 9 1 10 11 12 13 14 UrlRewriteFilter 15 org.tuckey.web.filters.urlrewrite.UrlRewriteFilter 16 17 confReloadCheckInterval 18 60 19 20 21 logLevel 22 DEBUG 23 24 25 26 UrlRewriteFilter 27 /* 28 29 30 31 32 springmvc 33 / 34 35 36 37 38 HiddenHttpMethodFilter 39 org.springframework.web.filter.HiddenHttpMethodFilter 40 41 42 43 HiddenHttpMethodFilter 44 springmvc 45
2.webapp/WEB-INF/springmvc-servlet.xml配置,使用如下两个class激活@RequestMapping annotation1 2 3
3.Controller编写1/** 2 * @RequestMapping(“/userinfo”) 具有层次关系,方法级的将在类一级@RequestMapping之一, 3 * 如下面示例, 访问方法级别的@RequestMapping(“/new”),则URL为 /userinfo/new 4 */ 5@Controller 6@RequestMapping(“/userinfo”) 7public class UserInfoController extends BaseSpringController{ 8 //默认多列排序,example: username desc,createTime asc 9 protected static final String DEFAULT_SORT_COLUMNS = null; 10 11 private UserInfoManager userInfoManager; 12 13 private final String LIST_ACTION = “redirect:/userinfo”; 14 15 /** 16 * 通过spring自动注入 17 **/ 18 public void setUserInfoManager(UserInfoManager manager) { 19 this.userInfoManager = manager; 20 } 21 22 /* 列表 / 23 @RequestMapping 24 public ModelAndView index(HttpServletRequest request,HttpServletResponse response,UserInfo userInfo) { 25 PageRequest
1/userinfo => index() 2 /userinfo/new => _new() 3 /userinfo/{id} => show() 4 /userinfo/{id}/edit => edit() 5 /userinfo POST => create() 6 /userinfo/{id} PUT => update() 7 /userinfo/{id} DELETE => delete() 8 /userinfo DELETE => batchDelete() 9
注(不使用 /userinfo/add => add() 方法是由于add这个方法会被maxthon浏览器当做广告链接过滤掉,因为包含ad字符)4.jsp 编写1 2
生成的html内容如下, 生成一个hidden的_method=put,并于web.xml中的HiddenHttpMethodFilter配合使用,在服务端将post请求改为put请求1 2 3
另外一种方法是你可以使用Ajax发送put,delete请求.5.静态资源的URL重写 如上我们描述,现因为将default servlet映射至/static/的子目录,现我们访问静态资源将会带一个/static/前缀. 如 /foo.gif, 现在访问该文件将是 /static/foo.gif. 那如何避免这个前缀呢,那就是应用URL rewrite,现我们使用 http://tuckey.org/urlrewrite/, 重写规则如下1 2 3 4 .*.jsp 5 .*.jspx 6 ^(/...)
新闻热点
疑难解答