SpringMVC配置文件
2015-09-04 12:48
639 查看
1.web.xml
<span style="font-family:Microsoft YaHei;"><?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"> <display-name></display-name> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <!-- 监听器 --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- spring 配置文件的加载 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:com/bdqn/resources/spring/applicationContext*.xml</param-value> </context-param> <!-- Spring MVC配置 --> <servlet> <servlet-name>spring-mvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <!-- 可以自定义servlet.xml配置文件的位置和名称,默认为WEB-INF目录下,名称为[<servlet-name>]-servlet.xml,如spring-servlet.xml--> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:com/bdqn/resources/spring/spring-mvc.xml</param-value> </init-param> <!-- 是启动顺序,1表示启动容器时初始化该Servlet --> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>spring-mvc</servlet-name> <!-- /是拦截所有请求,包括静态文件,*.do表示拦截所有以.do结尾的请求 --> <url-pattern>*.do</url-pattern> </servlet-mapping> <!-- 配置字符集 --> <filter> <filter-name>encodingFilter</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> <init-param> <param-name>forceEncoding</param-name> <param-value>true</param-value> </init-param> </filter> <filter-mapping> <filter-name>encodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!-- 控制Session的开关,hibernate对对象的管理是基于 session的,如果开启了延迟加载, 对于关联对象的查询可能会在渲染jsp的时候才发生,但是这个时候hibernate的当前session默认已 经关闭了 ,就会抛异常,所以我们经常需要hibernate在渲染jsp页面的时候还开着session, 这就需要在web.xml中配置一个拦截所有请 求的filter --> <filter> <filter-name>openSession</filter-name> <filter-class>org.springframework.orm.hibernate4.support.OpenSessionInViewFilter</filter-class> </filter> <filter-mapping> <filter-name>openSession</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app></span>2.application.xml
<span style="font-family:Microsoft YaHei;"><?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:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:task="http://www.springframework.org/schema/task" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd" default-lazy-init="true"> <!-- 加载配置属性文件 --> <context:property-placeholder location="classpath*:com/bdqn/resources/properties/application.properties" /> <!-- 自动扫描包 --> <context:component-scan base-package="com.bdqn" /> <!-- 配置数据源 --> <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close"> <!-- 连接信息 --> <property name="driverClassName" value="${jdbc.driver}" /> <property name="url" value="${jdbc.url}" /> <property name="username" value="${jdbc.username}" /> <property name="password" value="${jdbc.password}" /> <!-- 连接池信息 --> <property name="maxActive" value="50" /> <property name="initialSize" value="10" /> <property name="maxWait" value="60000" /> <property name="minIdle" value="10" /> <property name="timeBetweenEvictionRunsMillis" value="3000" /> <property name="minEvictableIdleTimeMillis" value="300000" /> <property name="testWhileIdle" value="true" /> <property name="testOnBorrow" value="false" /> <property name="testOnReturn" value="false" /> <property name="poolPreparedStatements" value="true" /> <property name="maxPoolPreparedStatementPerConnectionSize" value="20" /> <property name="timeBetweenLogStatsMillis" value="300000" /> </bean> <!-- 配置hibernate(SessionFactory)--> <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"> <property name="dataSource" ref="dataSource" /> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect">${hibernate.dialect}</prop> <prop key="hibernate.show_sql">${hibernate.show_sql}</prop> <prop key="hibernate.format_sql">${hibernate.format_sql}</prop> <prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop> <prop key="hibernate.jdbc.use_scrollable_resultset">false</prop> </props> </property> <property name="packagesToScan"> <list> <!-- 自动扫描该位置下的实体进行映射.packagesToScan表示配置你需要sping帮你扫描的包名,也就是只需要详细到包 --> <value>com.bdqn.entity</value> </list> </property> </bean> <!-- 数据源事务管理器 --> <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory" /> </bean> <!-- 注解驱动事务 --> <tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true" /> </beans></span>3.application.properties
<span style="font-family:Microsoft YaHei;">jdbc.driver=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8 jdbc.username=root jdbc.password= hibernate.dialect=org.hibernate.dialect.MySQLDialect hibernate.show_sql=true hibernate.format_sql=false hibernate.hbm2ddl.auto=update</span>4.spring-mvc.xml
<span style="font-family:Microsoft YaHei;"><?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:p="http://www.springframework.org/schema/p" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 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/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd "> <!--注解扫描包,主要扫描的是controller,把@Controller注解的类转换为bean --> <context:component-scan base-package="com.bdqn.controller"/> <!-- 添加注解驱动(开启注解) --> <mvc:annotation-driven/> <!-- 静态资源的访问 --> <mvc:resources location="/jsPlugins/" mapping="/jsPlugins/**"/> <!-- 定义视图解析器 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/view/" /> <property name="suffix" value=".jsp" /> </bean> <!-- 上传文件配置 、多了个多请求的处理、目的是为了支持多文件上传 --> <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> <property name="defaultEncoding" value="utf-8" /> <property name="maxUploadSize" value="10485760000" /> <property name="maxInMemorySize" value="40960" /> </bean> <!-- 用来处理json格式转换 --> <bean id="mappingJacksonHttpMessageConverter" class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"> <property name="supportedMediaTypes"> <list> <value>application/json;charset=UTF-8</value> </list> </property> </bean> </beans></span>
相关文章推荐
- 如何在Ubuntu中安装JDK
- Eclipse 开发利器 Jrebel热部署配置 基于Jetty
- java中的分页(最基础的方法)
- java类加载器-(1)
- 网页爬虫的设计与实现(Java版)
- java spring mvc之文件上传
- Java Excel API简介
- 学习日志---非递归二叉树游标遍历(前中后层序)
- hadoop学习之路——12、win7下eclipse连接hadoop测试环境
- java(1)
- Java实现excel数据导入和导出
- struts2的拦截器
- 取消Eclipse对于某些代码的格式化
- Java中堆内存和栈内存详解
- Spring学习笔记之五----Spring MVC
- Spring MVC DispatcherServlet绑定多种URL
- JAVAmap容器基本使用
- java基本知识小记(1)
- Spring事务管理
- java读取xml文件