由於下一版本的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 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>
</filter-mapping>
<!-- 覆蓋default servlet的/, springmvc servlet將處理原來處理靜態資源的映射 -->
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<!-- 浏覽器不支持put,delete等method,由該filter將/blog?_method=delete轉換為標准的http delete方法 -->
<filter>
<filter-name>HiddenHttpMethodFilter</filter-name>
<filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>HiddenHttpMethodFilter</filter-name>
<servlet-name>springmvc</servlet-name>
</filter-mapping>
2.webapp/WEB-INF/springmvc-servlet.xml配置,使用如下兩個class激活@RequestMapping annotation
Java代碼
<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"/>
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"/>
完整配置
Java代碼
<beans default-autowire="byName" >
<!-- 自動搜索@Controller標注的類 -->
<context:component-scan base-package="com.**.controller"/>
<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"/>
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"/>
<!-- Default ViewResolver -->
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
<property name="prefix" value="/pages"/>
<property name="suffix" value=".jsp"></property>
</bean>
<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource" p:basename="i18n/messages"/>
<!-- Mapping exception to the handler view -->
<bean id="exceptionResolver" class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
<!-- to /commons/error.jsp -->
<property name="defaultErrorView" value="/commons/error"/>
<property name="exceptionMappings">
<props>
</props>
</property>
</bean>
</beans>
3.Controller編寫
Java代碼
/**
* @RequestMapping("/userinfo") 具有層次關系,方法級的將在類一級@RequestMapping之一,
* 如下面示例, 訪問方法級別的@RequestMapping("/new"),則URL為 /userinfo/new
*/
@Controller
@RequestMapping("/userinfo")
public class UserInfoController extends BaseSpringController{
//默認多列排序,example: username desc,createTime asc
protected static final String DEFAULT_SORT_COLUMNS = null;
private UserInfoManager userInfoManager;
private final String LIST_ACTION = "redirect:/userinfo";
/**
* 通過spring自動注入
**/
public void setUserInfoManager(UserInfoManager manager) {
this.userInfoManager = manager;
}
/** 列表 */
@RequestMapping
public ModelAndView index(HttpServletRequest request,HttpServletResponse response,UserInfo userInfo) {
PageRequest<Map> pageRequest = newPageRequest(request,DEFAULT_SORT_COLUMNS);
//pageRequest.getFilters(); //add custom filters
Page page = this.userInfoManager.findByPageRequest(pageRequest);
savePage(page,pageRequest,request);
return new ModelAndView("/userinfo/list","userInfo",userInfo);
}
/** 進入新增 */
@RequestMapping(value="/new")
public ModelAndView _new(HttpServletRequest request,HttpServletResponse response,UserInfo userInfo) throws Exception {
return new ModelAndView("/userinfo/new","userInfo",userInfo);
}
/** 顯示 */
@RequestMapping(value="/{id}")
public ModelAndView show(@PathVariable Long id,HttpServletRequest request,HttpServletResponse response) throws Exception {
UserInfo userInfo = (UserInfo)userInfoManager.getById(id);
return new ModelAndView("/userinfo/show","userInfo",userInfo);
}
/** 編輯 */
@RequestMapping(value="/{id}/edit")
public ModelAndView edit(@PathVariable Long id,HttpServletRequest request,HttpServletResponse response) throws Exception {
UserInfo userInfo = (UserInfo)userInfoManager.getById(id);
return new ModelAndView("/userinfo/edit","userInfo",userInfo);
}
/** 保存新增 */
@RequestMapping(method=RequestMethod.POST)
public ModelAndView create(HttpServletRequest request,HttpServletResponse response,UserInfo userInfo) throws Exception {
userInfoManager.save(userInfo);
return new ModelAndView(LIST_ACTION);
}
/** 保存更新 */
@RequestMapping(value="/{id}",method=RequestMethod.PUT)
public ModelAndView update(@PathVariable Long id,HttpServletRequest request,HttpServletResponse response) throws Exception {
UserInfo userInfo = (UserInfo)userInfoManager.getById(id);
bind(request,userInfo);
userInfoManager.update(userInfo);
return new ModelAndView(LIST_ACTION);
}
/** 刪除 */
@RequestMapping(value="/{id}",method=RequestMethod.DELETE)
public ModelAndView delete(@PathVariable Long id,HttpServletRequest request,HttpServletResponse response) {
userInfoManager.removeById(id);
return new ModelAndView(LIST_ACTION);
}
/** 批量刪除 */
@RequestMapping(method=RequestMethod.DELETE)
public ModelAndView batchDelete(HttpServletRequest request,HttpServletResponse response) {
String[] items = request.getParameterValues("items");
for(int i = 0; i < items.length; i++) {
java.lang.Long id = new java.lang.Long(items[i]);
userInfoManager.removeById(id);
}
return new ModelAndView(LIST_ACTION);
}
}
上面是rapid-framework 新版本生成器生成的代碼,以後也將應用此規則,rest url中增刪改查等基本方法與Controller的方法映射規則
Java代碼
/userinfo => index()
/userinfo/new => _new()
/userinfo/{id} => show()
/userinfo/{id}/edit => edit()
/userinfo POST => create()
/userinfo/{id} PUT => update()
/userinfo/{id} DELETE => delete()
/userinfo DELETE => batchDelete()
注(不使用 /userinfo/add => add() 方法是由於add這個方法會被maxthon浏覽器當做廣告鏈接過濾掉,因為包含ad字符)
4.jsp 編寫
Html代碼
<form:form action="${ctx}/userinfo/${userInfo.userId}" method="put">
</form:form>
生成的html內容如下, 生成一個hidden的_method=put,並於web.xml中的HiddenHttpMethodFilter配合使用,在服務端將post請求改為put請求
Java代碼
<form id="userInfo" action="/springmvc_rest_demo/userinfo/2" method="post">
<input type="hidden" name="_method" value="put"/>
</form>
另外一種方法是你可以使用ajax發送put,delete請求.
5.靜態資源的URL重寫
如上我們描述,現因為將default servlet映射至/static/的子目錄,現我們訪問靜態資源將會帶一個/static/前綴.
如 /foo.gif, 現在訪問該文件將是 /static/foo.gif.
那如何避免這個前綴呢,那就是應用URL rewrite,現我們使用 http://tuckey.org/urlrewrite/, 重寫規則如下
Xml代碼
<urlrewrite>
<!-- 訪問jsp及jspx將不rewrite url,其它.js,.css,.gif等將重寫,如 /foo.gif => /static/foo.gif -->
<rule>
<condition operator="notequal" next="and" type="request-uri">.*.jsp</condition>
<condition operator="notequal" next="and" type="request-uri">.*.jspx</condition>
<from>^(/.*\..*)$</from>
<to>/static$1</to>
</rule>
</urlrewrite>
另筆者專門寫了一個 RestUrlRewriteFilter來做同樣的事件,以後會隨著rapid-framework一起發布.比這個更加輕量級.
並且該代碼已經貢獻給spring,不知會不會在下一版本發布