spring mvc 配置模版

首先是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-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>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值