- 浏览: 362140 次
- 性别:
- 来自: 沈阳
文章分类
最新评论
-
87029274:
JDBCAppender 批量存入数据库,源代码写的就是个锤子 ...
把重要的业务日志异步批量写入数据库 LOG4J -
zzz_robinson:
受益匪浅...很经典
世界少了我,其实无所谓。但我少了我,还剩什么? -
jackytang520:
你们也有这个问题啊,不过我们当初也是, 老是打印的时候内存溢出 ...
生产环境jvm内存运行1小时,就接近xmx,系统奇慢。大量的jasperreport无法释放。 -
xurichusheng:
当我在jsp页面上输入“<”时,这东西只弹出html标签 ...
Veloeclipse是一个HTML/XML/JSP/Velocity编辑器 -
neptune:
phoenixup 写道给你举个例子,我做过一个应用~~10万 ...
用tomcat做的大中型系统有哪些
http://badqiu.iteye.com/blog/473301
由于下一版本的rapid-framwork需要集成spring RESTful URL,所以研究了一下怎么搭建. 并碰到了一下问题。
springmvc 3.0 中增加 RESTful URL功能,构造出类似javaeye现在的URL。 rest介绍
比如如下URL
Java代码
1. /blog/1 HTTP GET => 得到id = 1的blog
2. /blog/1 HTTP DELETE => 删除 id = 1的blog
3. /blog/1 HTTP PUT => 更新id = 1的blog
4. /blog HTTP POST => 新增BLOG
/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代码
1. @RequestMapping(value="/blog/{id}",method=RequestMethod.DELETE)
2. public ModelAndView delete(@PathVariable Long id,HttpServletRequest request,HttpServletResponse response) {
3. blogManager.removeById(id);
4. return new ModelAndView(LIST_ACTION);
5. }
@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代码
1. @RequestMapping(value="/blog/{blogId}/message/{msgId}",method=RequestMethod.DELETE)
2. public ModelAndView delete(@PathVariable("blogId") Long blogId,@PathVariable("msgId") Long msgId,HttpServletRequest request,HttpServletResponse response) {
3. }
@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代码
1. <!-- 该 servlet为tomcat,jetty等容器提供,将静态资源映射从/改为/static/目录,如原来访问 http://localhost /foo.css ,现在http://localhost/static/foo.css -->
2. <servlet-mapping>
3. <servlet-name>default</servlet-name>
4. <url-pattern>/static/*</url-pattern>
5. </servlet-mapping>
6. <servlet>
7. <servlet-name>springmvc</servlet-name>
8. <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
9. <load-on-startup>1</load-on-startup>
10. </servlet>
11.
12. <!-- URL重写filter,用于将访问静态资源http://localhost/foo.css 转为http://localhost/static/foo.css -->
13. <filter>
14. <filter-name>UrlRewriteFilter</filter-name>
15. <filter-class>org.tuckey.web.filters.urlrewrite.UrlRewriteFilter</filter-class>
16. <init-param>
17. <param-name>confReloadCheckInterval</param-name>
18. <param-value>60</param-value>
19. </init-param>
20. <init-param>
21. <param-name>logLevel</param-name>
22. <param-value>DEBUG</param-value>
23. </init-param>
24. </filter>
25. <filter-mapping>
26. <filter-name>UrlRewriteFilter</filter-name>
27. <url-pattern>/*</url-pattern>
28. </filter-mapping>
29.
30. <!-- 覆盖default servlet的/, springmvc servlet将处理原来处理静态资源的映射 -->
31. <servlet-mapping>
32. <servlet-name>springmvc</servlet-name>
33. <url-pattern>/</url-pattern>
34. </servlet-mapping>
35.
36. <!-- 浏览器不支持put,delete等method,由该filter将/blog?_method=delete转换为标准的http delete方法 -->
37. <filter>
38. <filter-name>HiddenHttpMethodFilter</filter-name>
39. <filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
40. </filter>
41.
42. <filter-mapping>
43. <filter-name>HiddenHttpMethodFilter</filter-name>
44. <servlet-name>springmvc</servlet-name>
45. </filter-mapping>
<!-- 该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代码
1. <bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"/>
2. <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"/>
<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"/>
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"/>
完整配置
Java代码
1. <beans default-autowire="byName" >
2.
3. <!-- 自动搜索@Controller标注的类 -->
4. <context:component-scan base-package="com.**.controller"/>
5.
6. <bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"/>
7.
8. <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"/>
9.
10. <!-- Default ViewResolver -->
11. <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
12. <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
13. <property name="prefix" value="/pages"/>
14. <property name="suffix" value=".jsp"></property>
15. </bean>
16.
17. <bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource" p:basename="i18n/messages"/>
18.
19. <!-- Mapping exception to the handler view -->
20. <bean id="exceptionResolver" class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
21. <!-- to /commons/error.jsp -->
22. <property name="defaultErrorView" value="/commons/error"/>
23. <property name="exceptionMappings">
24. <props>
25. </props>
26. </property>
27. </bean>
28.
29. </beans>
<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代码
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.
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<Map> pageRequest = newPageRequest(request,DEFAULT_SORT_COLUMNS);
26. //pageRequest.getFilters(); //add custom filters
27.
28. Page page = this.userInfoManager.findByPageRequest(pageRequest);
29. savePage(page,pageRequest,request);
30. return new ModelAndView("/userinfo/list","userInfo",userInfo);
31. }
32.
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.
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.
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.
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.
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.
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.
76. /** 批量删除 */
77. @RequestMapping(method=RequestMethod.DELETE)
78. public ModelAndView batchDelete(@RequestParam("items") Long[] items,HttpServletRequest request,HttpServletResponse response) {
79.
80. for(int i = 0; i < items.length; i++) {
81.
82. userInfoManager.removeById(items[i]);
83. }
84. return new ModelAndView(LIST_ACTION);
85. }
86.
87. }
/**
* @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(@RequestParam("items") Long[] items,HttpServletRequest request,HttpServletResponse response) {
for(int i = 0; i < items.length; i++) {
userInfoManager.removeById(items[i]);
}
return new ModelAndView(LIST_ACTION);
}
}
上面是rapid-framework 新版本生成器生成的代码,以后也将应用此规则,rest url中增删改查等基本方法与Controller的方法映射规则
Java代码
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()
/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代码
1. <form:form action="${ctx}/userinfo/${userInfo.userId}" method="put">
2. </form:form>
<form:form action="${ctx}/userinfo/${userInfo.userId}" method="put">
</form:form>
生成的html内容如下, 生成一个hidden的_method=put,并于web.xml中的HiddenHttpMethodFilter配合使用,在服务端将post请求改为put请求
Java代码
1. <form id="userInfo" action="/springmvc_rest_demo/userinfo/2" method="post">
2. <input type="hidden" name="_method" value="put"/>
3. </form>
<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代码
1. <urlrewrite>
2. <!-- 访问jsp及jspx将不rewrite url,其它.js,.css,.gif等将重写,如 /foo.gif => /static/foo.gif -->
3. <rule>
4. <condition operator="notequal" next="and" type="request-uri">.*.jsp</condition>
5. <condition operator="notequal" next="and" type="request-uri">.*.jspx</condition>
6. <from>^(/.*\..*)$</from>
7. <to>/static$1</to>
8. </rule>
9. </urlrewrite>
<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,不知会不会在下一版本发布
由于下一版本的rapid-framwork需要集成spring RESTful URL,所以研究了一下怎么搭建. 并碰到了一下问题。
springmvc 3.0 中增加 RESTful URL功能,构造出类似javaeye现在的URL。 rest介绍
比如如下URL
Java代码
1. /blog/1 HTTP GET => 得到id = 1的blog
2. /blog/1 HTTP DELETE => 删除 id = 1的blog
3. /blog/1 HTTP PUT => 更新id = 1的blog
4. /blog HTTP POST => 新增BLOG
/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代码
1. @RequestMapping(value="/blog/{id}",method=RequestMethod.DELETE)
2. public ModelAndView delete(@PathVariable Long id,HttpServletRequest request,HttpServletResponse response) {
3. blogManager.removeById(id);
4. return new ModelAndView(LIST_ACTION);
5. }
@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代码
1. @RequestMapping(value="/blog/{blogId}/message/{msgId}",method=RequestMethod.DELETE)
2. public ModelAndView delete(@PathVariable("blogId") Long blogId,@PathVariable("msgId") Long msgId,HttpServletRequest request,HttpServletResponse response) {
3. }
@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代码
1. <!-- 该 servlet为tomcat,jetty等容器提供,将静态资源映射从/改为/static/目录,如原来访问 http://localhost /foo.css ,现在http://localhost/static/foo.css -->
2. <servlet-mapping>
3. <servlet-name>default</servlet-name>
4. <url-pattern>/static/*</url-pattern>
5. </servlet-mapping>
6. <servlet>
7. <servlet-name>springmvc</servlet-name>
8. <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
9. <load-on-startup>1</load-on-startup>
10. </servlet>
11.
12. <!-- URL重写filter,用于将访问静态资源http://localhost/foo.css 转为http://localhost/static/foo.css -->
13. <filter>
14. <filter-name>UrlRewriteFilter</filter-name>
15. <filter-class>org.tuckey.web.filters.urlrewrite.UrlRewriteFilter</filter-class>
16. <init-param>
17. <param-name>confReloadCheckInterval</param-name>
18. <param-value>60</param-value>
19. </init-param>
20. <init-param>
21. <param-name>logLevel</param-name>
22. <param-value>DEBUG</param-value>
23. </init-param>
24. </filter>
25. <filter-mapping>
26. <filter-name>UrlRewriteFilter</filter-name>
27. <url-pattern>/*</url-pattern>
28. </filter-mapping>
29.
30. <!-- 覆盖default servlet的/, springmvc servlet将处理原来处理静态资源的映射 -->
31. <servlet-mapping>
32. <servlet-name>springmvc</servlet-name>
33. <url-pattern>/</url-pattern>
34. </servlet-mapping>
35.
36. <!-- 浏览器不支持put,delete等method,由该filter将/blog?_method=delete转换为标准的http delete方法 -->
37. <filter>
38. <filter-name>HiddenHttpMethodFilter</filter-name>
39. <filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
40. </filter>
41.
42. <filter-mapping>
43. <filter-name>HiddenHttpMethodFilter</filter-name>
44. <servlet-name>springmvc</servlet-name>
45. </filter-mapping>
<!-- 该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代码
1. <bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"/>
2. <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"/>
<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"/>
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"/>
完整配置
Java代码
1. <beans default-autowire="byName" >
2.
3. <!-- 自动搜索@Controller标注的类 -->
4. <context:component-scan base-package="com.**.controller"/>
5.
6. <bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"/>
7.
8. <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"/>
9.
10. <!-- Default ViewResolver -->
11. <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
12. <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
13. <property name="prefix" value="/pages"/>
14. <property name="suffix" value=".jsp"></property>
15. </bean>
16.
17. <bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource" p:basename="i18n/messages"/>
18.
19. <!-- Mapping exception to the handler view -->
20. <bean id="exceptionResolver" class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
21. <!-- to /commons/error.jsp -->
22. <property name="defaultErrorView" value="/commons/error"/>
23. <property name="exceptionMappings">
24. <props>
25. </props>
26. </property>
27. </bean>
28.
29. </beans>
<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代码
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.
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<Map> pageRequest = newPageRequest(request,DEFAULT_SORT_COLUMNS);
26. //pageRequest.getFilters(); //add custom filters
27.
28. Page page = this.userInfoManager.findByPageRequest(pageRequest);
29. savePage(page,pageRequest,request);
30. return new ModelAndView("/userinfo/list","userInfo",userInfo);
31. }
32.
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.
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.
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.
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.
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.
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.
76. /** 批量删除 */
77. @RequestMapping(method=RequestMethod.DELETE)
78. public ModelAndView batchDelete(@RequestParam("items") Long[] items,HttpServletRequest request,HttpServletResponse response) {
79.
80. for(int i = 0; i < items.length; i++) {
81.
82. userInfoManager.removeById(items[i]);
83. }
84. return new ModelAndView(LIST_ACTION);
85. }
86.
87. }
/**
* @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(@RequestParam("items") Long[] items,HttpServletRequest request,HttpServletResponse response) {
for(int i = 0; i < items.length; i++) {
userInfoManager.removeById(items[i]);
}
return new ModelAndView(LIST_ACTION);
}
}
上面是rapid-framework 新版本生成器生成的代码,以后也将应用此规则,rest url中增删改查等基本方法与Controller的方法映射规则
Java代码
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()
/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代码
1. <form:form action="${ctx}/userinfo/${userInfo.userId}" method="put">
2. </form:form>
<form:form action="${ctx}/userinfo/${userInfo.userId}" method="put">
</form:form>
生成的html内容如下, 生成一个hidden的_method=put,并于web.xml中的HiddenHttpMethodFilter配合使用,在服务端将post请求改为put请求
Java代码
1. <form id="userInfo" action="/springmvc_rest_demo/userinfo/2" method="post">
2. <input type="hidden" name="_method" value="put"/>
3. </form>
<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代码
1. <urlrewrite>
2. <!-- 访问jsp及jspx将不rewrite url,其它.js,.css,.gif等将重写,如 /foo.gif => /static/foo.gif -->
3. <rule>
4. <condition operator="notequal" next="and" type="request-uri">.*.jsp</condition>
5. <condition operator="notequal" next="and" type="request-uri">.*.jspx</condition>
6. <from>^(/.*\..*)$</from>
7. <to>/static$1</to>
8. </rule>
9. </urlrewrite>
<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,不知会不会在下一版本发布
相关推荐
spring 3.0 应用springmvc 构造RESTful
在IT行业中,Spring框架是Java企业级应用开发的首选,而Spring MVC是Spring框架的一个重要...文档`spring_3.0_应用springmvc_构造RESTful_URL_详细讲解.docx`应该包含了更详尽的步骤和示例,帮助读者深入理解这一主题。
在本文中,我们将深入探讨如何在Spring 3.0中应用Spring MVC来构建RESTful URL,以实现更加优雅和功能强大的Web服务。REST(Representational State Transfer)是一种架构风格,用于构建基于HTTP协议的Web服务,它...
在本文中,我们将深入探讨如何在Spring 3.0中整合MVC框架与RESTful服务,并结合Maven构建项目。RESTful(Representational State Transfer)是一种软件架构风格,用于设计网络应用程序,尤其适用于Web服务。Spring ...
《Spring 3.0、Spring MVC 3.0与MyBatis 3.0整合详解》 在现代Java企业级应用开发中,Spring框架因其强大的功能和灵活性而被广泛使用。Spring 3.0作为其重要的一个版本,引入了诸多改进和新特性,提升了开发效率和...
5. **SpringMVC**:作为Spring框架的Web层解决方案,SpringMVC在3.0版本中引入了更多特性,如异步处理、模板引擎支持(例如FreeMarker、Thymeleaf),以及对RESTful服务的优化。 6. **数据访问**:Spring3.0对JDBC...
而将Metrics3.0与SpringMVC框架集成,则能够更好地实现对SpringMVC应用的全面监控。本文将深入探讨Metrics3.0与SpringMVC的集成原理及实践方法。 一、Metrics3.0简介 Metrics3.0是由Coda Hale创建的一个开源项目,...
《Spring3.0就这么简单》主要介绍了Spring3.0的核心内容,不仅讲解了Spring3.0的基础知识,还深入讨论了SpringIoC容器、SpringAOP、使用SpringJDBC访问数据库、集成Hibernate、Spring的事务管理、SpringMVC、单元...
Spring MVC 是一个流行的Java Web应用程序框架,用于构建高效、可维护的Web应用。在这个主题中,我们将深入探讨如何基于Servlet 3.0标准手动实现Spring MVC的一些核心功能,包括依赖注入(IOC)和请求处理机制。 ...
Spring MVC 是一个基于Java的轻量级Web应用框架,它是Spring框架的重要组成部分,主要用于构建Web应用程序的后端控制器。在Spring 3.0版本中,它引入了许多改进和新特性,提高了开发效率和灵活性。本篇文章将深入...
Spring 3.0重要特性总结如下: ◆Spring表达式(SpEL):用于bean定义的核心表达式分析器 ◆对基于注释的组件的更多支持:允许通过元注释创建注释的“快捷方式” ◆标准化的依赖性注入注释:对Java中依赖性注入的...
通过以上步骤,我们可以构建出一套完整的Spring MVC RESTful CRUD应用。理解并实践这些概念,对于提升Spring MVC的使用技能以及构建高质量的Web服务至关重要。记得在实际开发中根据项目需求进行调整和优化,保持代码...
3. **SpringMVC**:作为Spring的Web MVC框架,Spring3.0提升了其处理HTTP请求的能力,增加了ModelAndView对象的改进,支持RESTful风格的URL映射,以及@RequestBody和@ResponseBody注解,便于JSON数据的处理。...
3. **RESTful Web服务支持**:Spring3.0支持构建RESTful API,使服务更易于消费。 4. **注解驱动的数据格式化**:通过注解如`@DateTimeFormat`和`@NumberFormat`,可以方便地进行日期和货币格式转换。 5. **JPA 2.0...
这个是基于Spring的一个小例子 , 主要是为了帮助大家学习SpringSecurity和SpringMvc 和Mybatis3.0 1.SS不用再数据库建表 2.使用了SS提供的登录方式,在输入用户名和密码时,访问到服务器后台 3.判断如果是用户名是...
SpringMVC&Restful所需要的jar包,其中包含了spring-aop-4.0.0.RELEASE.jar,bean,aop,context,core,web,webMVC,commons等jar
在IT行业中,SpringMVC是Java企业级应用开发中广泛使用的Web框架,它极大地简化了构建基于MVC(Model-View-Controller)架构的应用程序。而RESTful风格是一种设计网络应用程序的方法,它强调资源的概念,并通过HTTP...
Spring MVC 是一个强大的Java Web开发框架,用于构建高效、可维护的Web应用程序。在本教程中,我们将专注于如何使用Spring MVC 4搭建一个基于RESTful风格的服务。REST(Representational State Transfer)是一种软件...
在Java开发领域,Spring、SpringMVC和MyBatis是三个非常重要的开源框架,它们各自在不同的层次上解决了Web应用中的问题。Spring作为全面的框架,提供了依赖注入(DI)和面向切面编程(AOP)等核心特性;SpringMVC是...