springweb30MVC注解附实例.docx
- 文档编号:2568951
- 上传时间:2023-05-04
- 格式:DOCX
- 页数:53
- 大小:74.95KB
springweb30MVC注解附实例.docx
《springweb30MVC注解附实例.docx》由会员分享,可在线阅读,更多相关《springweb30MVC注解附实例.docx(53页珍藏版)》请在冰点文库上搜索。
springweb30MVC注解附实例
SPRINGWEBMVC注解实例说明
0.提示
1)Spring发行版本附带了PetClinic示例,它是一个在简单的表单处理的上下文中,利用了本节中说明的注解支持的Web应用程序。
可以在“samples/petclinic”目录中找到PetClinic应用程序。
2)另外一个建立在基于注解的WebMVC上的示例应用程序,请见imagedb。
这个示例集中在无状态的multi-action控制器,包括多段文件上传的处理。
可以在“samples/imagedb”目录找到imagedb应用程序。
1.建立dispatcher实现注解支持
只有对应的HandlerMapping(为了实现类型级别的注解)和/或HandlerAdapter(为了实现方法级别的注解)出现在dispatcher中时,@RequestMapping才会被处理。
这在DispatcherServlet和DispatcherPortlet中都是缺省的行为。
然而,如果是在定义自己的HandlerMappings或HandlerAdapters,就需要确保一个对应的自定义的DefaultAnnotationHandlerMapping和/或AnnotationMethodHandlerAdapter同样被定义——假设想要使用@RequestMapping。
xmlversion="1.0"encoding="UTF-8"?
>
//www.springframework.org/schema/beans" xmlns: xsi="http: //www.w3.org/2001/XMLSchema-instance" xsi: schemaLocation="http: //www.springframework.org/schema/beans http: //www.springframework.org/schema/beans/spring-beans-2.5.xsd"> DefaultAnnotationHandlerMapping"/> AnnotationMethodHandlerAdapter"/> ...(controllerbeandefinitions)... 例1: 雁联zfpt-servlet.xml 配置DefaultAnnotationHandlerMapping和/或AnnotationMethodHandlerAdapter component-scanbase-package="com.ylink.zfpt.web.spring"/> DefaultAnnotationHandlerMapping"> AnnotationMethodHandlerAdapter"> class="com.ylink.zfpt.web.intercepor.SuperUserAccessInterceptor"> 例2: web.xml xmlversion="1.0"encoding="ISO-8859-1"? > xmlns=xmlns: xsi="http: //www.w3.org/2001/XMLSchema-instance" xsi: schemaLocation=" 2.1webAppRootKey -- Keyofthesystempropertythatshouldspecifytherootdirectoryofthis webapp.AppliedbyWebAppRootListenerorLog4jConfigListener. --> 2.3log4jConfigLocation -- LocationoftheLog4Jconfigfile,forinitializationandrefreshchecks. AppliedbyLog4jConfigListener. --> 2.4contextConfigLocation -- -LocationoftheXMLfilethatdefinestherootapplicationcontext. -AppliedbyContextLoaderServlet. - -Canbesetto: -"/WEB-INF/applicationContext-hibernate.xml"fortheHibernateimplementation, -"/WEB-INF/applicationContext-jpa.xml"fortheJPAone,or -"/WEB-INF/applicationContext-jdbc.xml"fortheJDBCone. --> /WEB-INF/spring/applicationContext-jdbc.xml /WEB-INF/applicationContext-security.xml -- --> -- TousetheJPAvariantabove,youwillneedtoenableSpringload-time weavinginyourserverenvironment.Outofthebox,Springwilltryto detecttherunningenvironmentandusetheappropriateweaverbutifthat fails,onemustenableonebyhandorusetheVM-wideweaver. SeePetClinic'sreadmeand/orSpring'sJPAdocumentationformoreinformation. --> 2.5springSecurityFilterChain org.springframework.web.filter.DelegatingFilterProxy 2.6Log4jConfigListener -- -ConfiguresLog4Jforthiswebapp. -Asthiscontextspecifiesacontext-param"log4jConfigLocation",itsfilepath -isusedtoloadtheLog4Jconfiguration,includingperiodicrefreshchecks. - -WouldfallbacktodefaultLog4Jinitialization(non-refreshing)ifnospecial -context-paramsaregiven. - -Exportsa"webapprootkey",i.e.asystempropertythatspecifiestheroot -directoryofthiswebapp,forusageinlogfilepaths. -Thiswebappspecifies"petclinic.root"(seelog4j.propertiesfile). --> --Leavethelistenercommented-outifusingJBoss--> 2.7ContextLoaderListener -- -Loadstherootapplicationcontextofthiswebappatstartup, -bydefaultfrom"/WEB-INF/applicationContext.xml". -NotethatyouneedtofallbacktoSpring'sContextLoaderServletfor -J2EEserversthatdonotfollowtheServlet2.4initializationorder. - -UseWebApplicationContextUtils.getWebApplicationContext(servletContext) -toaccessitanywhereinthewebapplication,outsideoftheframework. - -Therootcontextistheparentofallservlet-specificcontexts. -Thismeansthatitsbeansareautomaticallyavailableinthesechildcontexts, -bothforgetBean(name)callsand(external)beanreferences. --> org.springframework.web.context.ContextLoaderListener 2.8DispatcherServlet -- -Servletthatdispatchesrequesttoregisteredhandlers(Controllerimplementations). -Hasitsownapplicationcontext,bydefaultdefinedin"{servlet-name}-servlet.xml", -i.e."petclinic-servlet.xml". - -Awebappcancontainanynumberofsuchservlets. -Notethatthiswebapphasasharedrootapplicationcontext,servingasparent -ofallDispatcherServletcontexts. --> org.springframework.web.servlet.DispatcherServlet -- -Mapsthepetclinicdispatcherto"*.do".Allhandlermappingsin -petclinic-servlet.xmlwillbydefaultbeappliedtothissubpath. -Ifamappingisn'ta/*subpath,thehandlermappingsareconsidered -relativetothewebapproot. - -NOTE: Asingledispatchercanbemappedtomultiplepaths,likeanyservlet. --> 2.9exception.java --Displaysastacktrace--> 例3: 雁联web.xml 如果你想要自定义映射策略,显式的定义一个DefaultAnnotationHandlerMapping和/或AnnotationMethodHandlerAdapter也有实际意义。 例如,指定一个自定义的PathMatcher或者WebBindingInitializer 1.一个简单的基于注解的Controller 使用过低版本SpringMVC的读者都知道: 1.当创建一个Controller时,我们需要直接或间接地实现org.springframework.web.servlet.mvc.Controller接口。 一般情况下,我们是通过继承SimpleFormController或MultiActionController来定义自己的Controller的。 2.在定义Controller后,一个重要的事件是在SpringMVC的配置文件中通过HandlerMapping定义请求和控制器的映射关系,以便将两者关联起来。 3.来看一下基于注解的Controller是如何定义做到这一点的,下面是使用注解的BbtForumController: 实现效果: 启动Tomcat,发送http: //localhost/forum.doURL请求,BbtForumController的listAllBoard()方法将响应这个请求,并转向WEB-INF/jsp/listBoard.jsp的视图页面。 清单1.BbtForumController.java packagecom.baobaotao.web; importcom.baobaotao.service.BbtForumService; importorg.springframework.beans.factory.annotation.Autowired; importorg.springframework.stereotype.Controller; importorg.springframework.web.bind.annotation.ModelAttribute; importorg.springframework.web.bind.annotation.RequestMapping; importorg.springframework.web.bind.annotation.RequestMethod; importjava.util.Collection; @Controller//<——① @RequestMapping("/forum.do") publicclassBbtForumController{ @Autowired privateBbtForumServicebbtForumService; @RequestMapping//<——② publicStringlistAllBoard(){ bbtForumService.getAllBoard(); System.out.println("calllistAllBoardmethod."); return"listBoard"; } } 在①处使用了两个注解,分别是@Controller和@RequestMapping。 在“使用Spring2.5基于注解驱动的IoC”这篇文章里,笔者曾经指出过@Controller、@Service以及@Repository和@Component注解的作用是等价的: 将一个类成为Spring容器的Bean。 由于SpringMVC的Controller必须事先是一个Bean,所以@Controller注解是不可缺少的。 真正让BbtForumController具备SpringMVCController功能的是@RequestMapping这个注解。 @RequestMapping可以标注在类定义处,将Controller和特定请求关联起来;还可以标注在方法签名处,以便进一步对请求进行分流。 在①处,我们让BbtForumController关联“/forum.do”的请求,而②处,我们具体地指定listAllBoard()方法来处理请求。 所以在类声明处标注的@RequestMapping相当于让POJO实现了Controller接口,而在方法定义处的@RequestMapping相当于让POJO扩展Spring预定义的Controller(如SimpleFormController等)。 清单2.web.xml: 启用Spring容器和SpringMVC框架 为了让基于注解的SpringMVC真正工作起来,需要在SpringMVC对应的xxx-servlet.xml配置文件中做一些手脚。 在此之前,还是先来看一下web.xml的配置吧 xmlversion="1.0"encoding="UTF-8"? > xmlns: xsi="http: //www.w3.org/2001/XMLSchema-instance" xsi: schemaLocation=" version="2.5"> --Spring服务层的配置文件-->
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- springweb30MVC 注解 实例