Spring3 REST MVC框架,提速你的Web开发
<P>最近在Java web 项目中需要采用非常简单的REST框架,Struts2、webwork、JSF 经过一番比较,最后选择了Spring3,理由只有一个 “<U><STRONG>简单</STRONG>,<STRONG>好用</STRONG>,并满足需要</U>”。很久以前就Rod Johnson大叔说<STRONG> Spring3</STRONG> 全面支持REST风格的Web服务,"We're really seeing extensive interest and growth in REST, and it will have comprehensive support for RESTful Web services," said Johnson,今天亲自尝试了一下,真有点相识恨晚的感觉,如果在这次项目运用没有太大的问题,将来在其他项目会大量运用。</P><P><STRONG>工作原理如图所示:</STRONG><BR><IMG alt=http://1aqpcg.bay.livefilestore.com/y1p_Q90l9w2JCMiEtgzkdikNQZoyy7Ic7GiCrm8Uk2GUhHZ1C80d7j2Ty4X0IWuydFepV3htprYHjptpEtq561o-7Ok_rkBgoZa/spring_rest_fm.png src="http://1aqpcg.bay.livefilestore.com/y1p_Q90l9w2JCMiEtgzkdikNQZoyy7Ic7GiCrm8Uk2GUhHZ1C80d7j2Ty4X0IWuydFepV3htprYHjptpEtq561o-7Ok_rkBgoZa/spring_rest_fm.png"><BR>*根据HTTP请求的URL,调用相应的DispatcherServlet控制器。<BR>*提供一个视图是作为HTTP响应发送。</P>
<P><STRONG>页面上</STRONG><STRONG>最终</STRONG><STRONG>运行</STRONG><STRONG>效果,如图所示:</STRONG></P>
<P><IMG alt=http://1aqpcg.bay.livefilestore.com/y1pu1qm8gBjr1VJ_9hg2BvILVMcD3tnvyc1WTPRVpkVTHqbP00tQTml9vMDJU_xnyfNrGls2OPS7Df-Z96_zPSMFbqsWXxxUr2t/topic_view.png src="http://1aqpcg.bay.livefilestore.com/y1pu1qm8gBjr1VJ_9hg2BvILVMcD3tnvyc1WTPRVpkVTHqbP00tQTml9vMDJU_xnyfNrGls2OPS7Df-Z96_zPSMFbqsWXxxUr2t/topic_view.png"><BR><STRONG><SPAN style="COLOR: rgb(255,255,255)"><SPAN style="BACKGROUND-COLOR: rgb(0,100,0)">主要代码:</SPAN></SPAN></STRONG><BR><STRONG><SPAN style="COLOR: rgb(255,255,255)"><SPAN style="BACKGROUND-COLOR: rgb(128,0,0)"><BR>清单1:TopicController</SPAN></SPAN></STRONG><BR>package com.javabloger.springrest.action;</P>
<P>import javax.servlet.http.HttpServletRequest;<BR>import javax.servlet.http.HttpServletResponse;<BR>import org.springframework.stereotype.Controller;<BR>import org.springframework.web.bind.annotation.PathVariable;<BR>import org.springframework.web.bind.annotation.RequestMapping;<BR>import org.springframework.web.bind.annotation.RequestMethod;</P>
<P>@Controller<BR>@RequestMapping("/topic") <STRONG> //url映射的名称</STRONG><BR>public class TopicController {</P>
<P> @RequestMapping(value = "/{id}",method=RequestMethod.GET)<BR> public String helloWorld(<BR> @PathVariable Long id, <BR> HttpServletRequest request,<BR> HttpServletResponse response) {<BR> request.setAttribute("message", "You Input Topci Id is: <b>"+id+"</b>"); <BR> return "topic" ; <STRONG> // 对应 /WEB-INF/jsp 目录下的 topic.jsp 文件</STRONG><BR> }<BR> <BR> @RequestMapping(value="/add")<BR> public String test(HttpServletRequest request, <BR> HttpServletResponse response){<BR> System.out.println("Hello www.JavaBloger.com ");<BR> request.setAttribute("message", "Hello JavaBloger ! ,@RequestMapping(value='/add')"); <BR> return "topic"; <STRONG>// 对应 /WEB-INF/jsp 目录下的 topic.jsp 文件</STRONG><BR> <BR> }<BR>}</P>
<P><SPAN style="BACKGROUND-COLOR: rgb(128,0,0)"><SPAN style="COLOR: rgb(255,255,255)"><STRONG>清单2 :UserController</STRONG></SPAN></SPAN><BR>package com.javabloger.springrest.action;</P>
<P>import javax.servlet.http.HttpServletRequest;<BR>import javax.servlet.http.HttpServletResponse;<BR>import org.springframework.stereotype.Controller;<BR>import org.springframework.web.bind.annotation.RequestMapping;<BR>import com.javabloger.springrest.pojo.Users;</P>
<P>@Controller<BR>@RequestMapping("/user")</P>
<P>public class UserController {<BR> @RequestMapping(value="/login")<BR> public String test(HttpServletRequest request, <BR> HttpServletResponse response,Users userinfo){ <STRONG>// 非常方便可以直接在方法里面放入对象</STRONG><BR> if (userinfo.getUsername().equals("username") && <BR> userinfo.getPassword().equals("password")<BR> )<BR> {<BR> request.setAttribute("user", userinfo); <BR> return "users/list"; <STRONG>//判断,将跳转不同的页面</STRONG><BR> }<BR> else{<BR> return "users/loginerr"; <STRONG>//判断,将跳转不同的页面</STRONG><BR> }<BR> }<BR>}</P>
<P><STRONG><SPAN style="COLOR: rgb(255,255,255)"><SPAN style="BACKGROUND-COLOR: rgb(128,0,0)">清单3:web.xml</SPAN></SPAN></STRONG><BR> <servlet> <BR> <servlet-name>springmvc</servlet-name> <BR> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <BR> <load-on-startup>2</load-on-startup> <BR> </servlet> <BR> <servlet-mapping> <BR> <servlet-name>springmvc</servlet-name> <BR> <url-pattern>/</url-pattern> <BR> </servlet-mapping> <BR><STRONG><SPAN style="COLOR: rgb(255,255,255)"><SPAN style="BACKGROUND-COLOR: rgb(128,0,0)"><BR>清单4:springmvc-servlet.xml<BR></SPAN></SPAN></STRONG><?xml version="1.0" encoding="UTF-8"?><BR><beans xmlns="http://www.springframework.org/schema/beans"<BR> xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"<BR> xmlns:aop="http://www.springframework.org/schema/aop" xmlns:p="http://www.springframework.org/schema/p"<BR> xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jdbc="http://www.springframework.org/schema/jdbc"<BR> xsi:schemaLocation="http://www.springframework.org/schema/beans <BR> http://www.springframework.org/schema/beans/spring-beans-3.0.xsd <BR> http://www.springframework.org/schema/context <BR> http://www.springframework.org/schema/context/spring-context-3.0.xsd <BR> http://www.springframework.org/schema/tx <BR> http://www.springframework.org/schema/tx/spring-tx-3.0.xsd <BR> http://www.springframework.org/schema/jdbc <BR> http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd"><BR> <bean<BR> class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" /><BR> <bean<BR> class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" /><BR> <!– 自动搜索@Controller标注的类 –><BR> <context:component-scan base-package="com.javabloger.springrest.action" /><BR> <bean<BR> class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" /><BR> <bean<BR> class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" /><BR> <!– Default ViewResolver –><BR> <bean id="viewResolver"<BR> class="org.springframework.web.servlet.view.InternalResourceViewResolver"><BR> <property name="viewClass"<BR> value="org.springframework.web.servlet.view.JstlView" /><BR> <property name="prefix" value="/WEB-INF/jsp/" /><BR> <property name="suffix" value=".jsp"></property><BR> </bean><BR> <bean id="messageSource"<BR> class="org.springframework.context.support.ResourceBundleMessageSource"<BR> p:basename="i18n/messages" /><BR></beans></P>
<P> </P>
<P><SPAN style="FONT-SIZE: 16px"><SPAN style="BACKGROUND-COLOR: rgb(255,215,0)"><STRONG><A href="http://javabloger-mini-books.googlecode.com/files/SpringREST.rar" target=_blank><<完整的Spring3 REST代码例子下载>></A></STRONG></SPAN></SPAN></P>
<P><SPAN style="COLOR: rgb(0,0,0)"><SPAN style="COLOR: rgb(255,255,255)"><SPAN style="BACKGROUND-COLOR: rgb(0,100,0)"><STRONG><BR>一些废话:<BR></STRONG></SPAN></SPAN><STRONG>我个人非常非常喜欢没有xml配置框架,因为项目一大不仅需要对代码进行维护对xml文件还需要维护相当繁琐,在这个项目中除了采用Spring3的REST特性,还采用了Apache的dbutils对数据进行操作,传说中的SSH轻量级框架是针对EJB和J2EE而言的轻量级,对于上述这样的框架SSH还能称得上“轻量级”吗?也许SSH目前成为了标准,但不再是“轻量级J2EE框架”的代名词!</STRONG></SPAN></P>
<P><U><SPAN style="BACKGROUND-COLOR: rgb(0,128,0)"><STRONG style="DISPLAY: block; MARGIN: 12px 0pt 4px">另外,还有一份专门介绍 Spring3 MVC的资料《Spring 3 MVC CodeMash 2009》,请看幻灯片:</STRONG></SPAN></U></P>
<DIV id=__ss_915100 style="WIDTH: 425px"><EMBED src=http://static.slidesharecdn.com/swf/ssplayer2.swf?doc=spring-mvc-code-mash09a-1231894880943441-1&stripped_title=spring-mvc-code-mash09-presentation width=425 height=355 type=application/x-shockwave-flash allowfullscreen="true" allow.access="always"></EMBED>
<P> </P>
<P><STRONG>相关文章:<A class=row-title title="编辑 “spring3 oscache mrpersister dbcp dbutils 整合”" href="http://www.javabloger.com/wp-admin/post.php?action=edit&post=927"><BR></A></STRONG><a href="http://www.javabloger.com/article/no-mapping-found-for-http-request-with-uri.html" target="_blank">Spring3无法显示css/js/jpg/gif </A><BR><a href="http://www.javabloger.com/article/spring3-dbutils-mrpersister-dbcp-oscache.html" target="_blank">spring3 oscache mrpersister dbcp dbutils 整合 </A><BR><a href="http://www.javabloger.com/article/spring-3-file-upload-example.html" target="_blank">Spring3文件上传,提速你的Web开发 <BR></A><a href="http://www.javabloger.com/article/setuseclasscache.html" target="_blank">Spring+Webwork整合 NoSuchMethodError 错误 </A><BR><a href="http://www.javabloger.com/article/spring3-rest-multiparthttpservletrequest.html" target="_blank">Spring3 REST 上传多个文件 </A><BR><a href="http://www.javabloger.com/article/spring3-rest-annotation.html" target="_blank">Spring3 REST中的@ </A><BR><a href="http://www.javabloger.com/article/spring3-rest-java-on-google.html" target="_blank">Spring3 REST实现html伪静态分页效果 </A></P>
<P> </P>
<P>–end–<BR> </P></DIV>
页:
[1]