技术中心

这里象征着我们的态度和能力

>Spring MVC常用配置模板详解
作者:中国IT实验室    来源:中国IT实验室    发布时间:2012-01-01      浏览次数:7899
分享到:
欢迎进入Java社区论坛,与200万技术人员互动交流 >>进入

  首先是web.xml

  view plain <?xml version="1.0" encoding="UTF-8"?> <web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"> <!――设置spring在本应用程序中的配置加载监听器 ――> <!――监听器会在应用程序启动时自动实例化 ――> <!――ContextLoaderListener实例化后自动寻找WEB-INF下的applicationContext.xml配置文件 ――> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener>

  <!――解决spring乱码问题 ――> <filter> <filter-name>encoding</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> </filter> <filter-mapping> <filter-name>encoding</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>

  <!――全局Servlet调度配置 ――> <servlet> <!――若设置 servlet-name为[name] ――> <!――则DispatcherServlet在实例化后会自动加载[name]-servlet.xml ――> <servlet-name>spring</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <!――随服务器一同启动 ――> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>spring</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping>

  <!――错误页转向配置 ――> <error-page> <error-code>404</error-code> <location>/error.html</location> </error-page>

  <!――首页文件名设置 ――> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.jsp</welcome-file> </welcome-file-list>

  <!――设置持久化单元引用 ――> <!――此配置仅当Servlet容器支持Servlet 2.5时有效 ――> <persistence-unit-ref> <persistence-unit-ref-name>jdbc/spring</persistence-unit-ref-name> <persistence-unit-name>spring</persistence-unit-name> </persistence-unit-ref> </web-app>

  其次是spring-servlet.xml

  view plain <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi=<a href="http://www.w3.org/2001/XMLSchema-instancehttp://www.w3.org/2001/XMLSchema-instance">http://www.w3.org/2001/XMLSchema-instance</a> xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"> <!――针对控制器返回视图字符串的修饰 最终视图路径为 [prefix][returnValue][suffix] ――> <!――例如prefix="/WEB-INF/" returnValue="admin/login" suffix=".jsp" ――> <!――最终的URL为 /WEB-INF/admin/login.jsp ――> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" /> <property name="prefix" value="/WEB-INF/pages/" /> <property name="suffix" value=".jsp" /> </bean>

  <!――此处配置将 controller包和util包及其子包内部 ――> <!――包含注解 @Service @Component @Controller @Repository的类全部自动注入 ――> <context:component-scan base-package="controller,util" />

  <!――为spring设置注解映射路径支持 @RequestMapping ――> <bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" /> <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />

  <!――设置spring拦截器 ――> <mvc:interceptors> <!――若mvc:mapping的path相同则按照配置先后形成拦截器链 ――> <mvc:interceptor> <mvc:mapping path="/*" /> <bean class="test.interceptor.TestInterceptor1" /> </mvc:interceptor> <mvc:interceptor> <mvc:mapping path="/*" /> <bean class="test.interceptor.TestInterceptor2" /> </mvc:interceptor> </mvc:interceptors> </beans>

  最后是applicationContext.xml

  view plain <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">

  <context:component-scan base-package="test" /> <!――与持久化相关的配置 ――> <!――设置从应用服务器的JNDI树种查找哪一个具体的JDBC资源 ――> <!――jndi-name用于指示JDBC资源的jndi名称 ――> <jee:jndi-lookup id="entityManagerFactory" jndi-name="jdbc/spring" /> <!――开启从应用服务器的JNDI树中自动获取事务性DataSource ――> <tx:jta-transaction-manager /> <!――激活@Transactional注解驱动的事务行为 ――> <!――此处设置的transaction-manager为transactionManager ――> <tx:annotation-driven transaction-manager="transactionManager" /> </beans>

4000-880-989
(24小时热线)
联系客服
微信公众号

官方公众号

小程序

©2008-2022 CORPORATION ALL Rights Reserved. 昆明奥远科技有限公司版权所有 滇ICP备09003328号-1 滇公网安备 53011102000818号 增值电信业务经营许可证号:滇B2-20110045
昆明那家网络公司好,新媒体运营,网站优化,网络推广,网站建设,网页设计,网站设计,网站推广,云南网站公司,昆明新媒体公司,云南网红主播,昆明SEO公司,昆明网站建设,昆明网络推广,昆明网站优化,昆明网站推广,红河网站建设,大理网络公司,曲靖网络公司,丽江网站设计,昭通网络公司,保山大数据服务,智慧高速建设,智慧校园服务,云南IDC服务商,网络安全测评,等保测评,网站关键词排名优化服务,服务客户尽超2000余家,一切尽在奥远科技,服务电话:13888956730