0.提示
1) Spring发行版本附带了PetClinic 示例,它是一个在简单的表单处理的上下文中, 利用了本节中说明的注解支持的Web应用程序。 可以在“samples/petclinic ”目录中到PetClinic 应用程序。
2) 另外一个建立在基于注解的Web MVC上的示例应用程序,请见imagedb 。
这个示例集中在无状态的multi-action控制器,包括多段文件上传的处理。
可以在“samples/imagedb ”目录到imagedb 应用程序。
1.建立dispatcher实现注解支持
只有对应的HandlerMapping (为了实现类型级别的注解)和/ 或HandlerAdapter (为了实现方法级别的注解)出现在 dispatcher中时, @RequestMapping 才会被处理。 这在DispatcherServlet 和DispatcherPortlet 中都是缺省的行为。
然而,如果是在定义自己的HandlerMappings 或HandlerAdapters , 就需要确保一个对应的自定义的DefaultAnnotationHandlerMapping 和 /或AnnotationMethodHandlerAdapter 同样被定义——假设想要使用@RequestMapping 。
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="/schema/beans" xmlns:xsi="/2001/XMLSchema-instance" xsi:schemaLocation="/schema/beans /schema/beans/spring-beans-2.5.xsd"> <bean class="org.springframework.web.servlet.mvc. DefaultAnnotationHandlerMapping"/> <bean class="org.springframework.web.servlet.mvc. AnnotationMethodHandlerAdapter"/> ... (controller bean definitions) ... </beans> |
例1:雁联l
配置DefaultAnnotationHandlerMapping 和 /或AnnotationMethodHandlerAdapter
<context:component-scan base-package="com.ylink.zfpt.web.spring"/> |
<bean id="annotationHandlerMapping" class="org.springframework.web.servlet.mvc.annotation. DefaultAnnotationHandlerMapping"> <property name="order"> <value>1</value> </property> <property name="interceptors"> <list> <ref bean="sessionInterceptor"/> <ref bean="superUserInterceptor"/> </list> </property> </bean> |
<bean class="org.springframework.web.servlet.mvc.annotation. AnnotationMethodHandlerAdapter"> <property name="webBindingInitializer"> <bean class="com.ylink.zfpt.web.spring.ZfptBindingInitializer"/> </property> </bean> |
<bean id="sessionInterceptor" class="com.ylink.zfpt.web.intercepor.SessionInterceptor"> </bean> <bean id="superUserInterceptor" class="com.ylink.zfpt.web.intercepor.SuperUserAccessInterceptor"></bean> |
例2:l
<?xml version="1.0" encoding="ISO-8859-1"?> <web-app version="2.4" xmlns=java.sun/xml/ns/j2ee xmlns:xsi="/2001/XMLSchema-instance" xsi:schemaLocation="java.sun/xml/ns/j2ee java.sun/xml/ns/j2ee/web-app_2_4.xsd"> |
<display-name>Spring PetClinic</display-name> <description>Spring PetClinic sample application</description> |
2.1 webAppRootKey
<!-- Key of the system property that should specify the root directory of this web app. Applied by WebAppRootListener or Log4jConfigListener. --> <context-param> <param-name>webAppRootKey</param-name> <param-value>petclinic.root</param-value> </context-param> |
2.3 log4jConfigLocation
<!-- Location of the Log4J config file, for initialization and refresh checks. Applied by Log4jConfigListener. --> <context-param> <param-name>log4jConfigLocation</param-name> <param-value>/WEB-INF/classes/log4j.properties</param-value> </context-param> |
2.4 contextConfigLocation
<!-- - Location of the XML file that defines the root application context. - Applied by ContextLoaderServlet. - - Can be set to: - "/WEB-INF/applicationContext-hibernate.xml" for the Hibernate implementation, - "/WEB-INF/applicationContext-jpa.xml" for the JPA one, or - "/WEB-INF/applicationContext-jdbc.xml" for the JDBC one. --> <context-param> <param-name>contextConfigLocation</param-name> <param-value> /WEB-INF/spring/applicationContext-jdbc.xml /WEB-INF/applicationContext-security.xml </param-value> <!-- <param-value>/WEB-INF/spring/applicationContext-hibernate.xml</param-value> <param-value>/WEB-INF/spring/applicationContext-jpa.xml</param-value> --> <!-- To use the JPA variant above, you will need to enable Spring load-time weaving in your server environment. Out of the box, Spring will try to detect the running environment and use the appropriate weaver but if that fails, one must enable one by hand or use the VM-wide weaver. See PetClinic's readme and/or Spring's JPA documentation for more information. --> </context-param> |
2.5 springSecurityFilterChain
<filter> <filter-name>springSecurityFilterChain</filter-name> <filter-class> org.springframework.web.filter.DelegatingFilterProxy </filter-class> </filter> |
<filter-mappingspring ioc注解> <filter-name>springSecurityFilterChain</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> |
2.6 Log4jConfigListener
<!-- - Configures Log4J for this web app. - As this context specifies a context-param "log4jConfigLocation", its file path - is used to load the Log4J configuration, including periodic refresh checks. - - Would fall back to default Log4J initialization (non-refreshing) if no special - context-params are given. - - Exports a "web app root key", i.e. a system property that specifies the root - directory of this web app, for usage in log file paths. - This web app specifies "petclinic.root" (see log4j.properties file). --> <listener> <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class> </listener> |
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论