您好,欢迎访问三七文档
当前位置:首页 > 商业/管理/HR > 项目/工程管理 > spring3.0MVC注解(附实例)
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.0encoding=UTF-8?beansxmlns=:xsi=:schemaLocation=://=org.springframework.web.servlet.mvc.DefaultAnnotationHandlerMapping/beanclass=org.springframework.web.servlet.mvc.AnnotationMethodHandlerAdapter/...(controllerbeandefinitions).../beans例1:雁联zfpt-servlet.xml配置DefaultAnnotationHandlerMapping和/或AnnotationMethodHandlerAdaptercontext:component-scanbase-package=com.ylink.zfpt.web.spring/beanid=annotationHandlerMappingclass=org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMappingpropertyname=ordervalue1/value/propertypropertyname=interceptorslistrefbean=sessionInterceptor/refbean=superUserInterceptor//list/property/beanbeanclass=org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapterpropertyname=webBindingInitializerbeanclass=com.ylink.zfpt.web.spring.ZfptBindingInitializer//property/beanbeanid=sessionInterceptorclass=com.ylink.zfpt.web.intercepor.SessionInterceptor/beanbeanid=superUserInterceptorclass=com.ylink.zfpt.web.intercepor.SuperUserAccessInterceptor/bean例2:web.xml?xmlversion=1.0encoding=ISO-8859-1?web-appversion=2.4xmlns=:xsi=:schemaLocation=://java.sun.com/xml/ns/j2ee/web-app_2_4.xsddisplay-nameSpringPetClinic/display-namedescriptionSpringPetClinicsampleapplication/description2.1webAppRootKey!--Keyofthesystempropertythatshouldspecifytherootdirectoryofthiswebapp.AppliedbyWebAppRootListenerorLog4jConfigListener.--context-paramparam-namewebAppRootKey/param-nameparam-valuepetclinic.root/param-value/context-param2.3log4jConfigLocation!--LocationoftheLog4Jconfigfile,forinitializationandrefreshchecks.AppliedbyLog4jConfigListener.--context-paramparam-namelog4jConfigLocation/param-nameparam-value/WEB-INF/classes/log4j.properties/param-value/context-param2.4contextConfigLocation!---LocationoftheXMLfilethatdefinestherootapplicationcontext.-AppliedbyContextLoaderServlet.--Canbesetto:-/WEB-INF/applicationContext-hibernate.xmlfortheHibernateimplementation,-/WEB-INF/applicationContext-jpa.xmlfortheJPAone,or-/WEB-INF/applicationContext-jdbc.xmlfortheJDBCone.--context-paramparam-namecontextConfigLocation/param-nameparam-value/WEB-INF/spring/applicationContext-jdbc.xml/WEB-INF/applicationContext-security.xml/param-value!--param-value/WEB-INF/spring/applicationContext-hibernate.xml/param-valueparam-value/WEB-INF/spring/applicationContext-jpa.xml/param-value--!--TousetheJPAvariantabove,youwillneedtoenableSpringload-timeweavinginyourserverenvironment.Outofthebox,Springwilltrytodetecttherunningenvironmentandusetheappropriateweaverbutifthatfails,onemustenableonebyhandorusetheVM-wideweaver.SeePetClinic'sreadmeand/orSpring'sJPAdocumentationformoreinformation.--/context-param2.5springSecurityFilterChainfilterfilter-namespringSecurityFilterChain/filter-namefilter-classorg.springframework.web.filter.DelegatingFilterProxy/filter-class/filterfilter-mappingfilter-namespringSecurityFilterChain/filter-nameurl-pattern/*/url-pattern/filter-mapping2.6Log4jConfigListener!---ConfiguresLog4Jforthiswebapp.-Asthiscontextspecifiesacontext-paramlog4jConfigLocation,itsfilepath-isusedtoloadtheLog4Jconfiguration,includingperiodicrefreshchecks.--WouldfallbacktodefaultLog4Jinitialization(non-refreshing)ifnospecial-context-paramsaregiven.--Exportsawebapprootkey,i.e.asystempropertythatspecifiestheroot-directoryofthiswebapp,forusageinlogfilepaths.-Thiswebappspecifiespetclinic.root(seelog4j.propertiesfile).--!--Leavethelistenercommented-outifusingJBoss--listenerlistener-classorg.springframework.web.util.Log4jConfigListener/listener-class/listener2.7ContextLoaderListener!---Loadstherootapplicationcontextofthiswebappatstartup,-bydefaultfrom/WEB-INF/applicationContext.xml.-NotethatyouneedtofallbacktoSpring'sContextLoaderServletfor-J2EEserversthatdonotfollowtheServlet2.4initializationorder.--UseWebApplicationContextUtils.getWebApplicationContext(servletContext)-toaccessitanywhereinthewebapplication,outsideoftheframework.--Therootcontextistheparentofallservlet-specificcontexts.-Thismeansthatitsbeansareau
本文标题:spring3.0MVC注解(附实例)
链接地址:https://www.777doc.com/doc-4353730 .html