一:web.xml
(1)spring mvc的配置
<servlet> <description>spring mvc servlet</description> <servlet-name>springMvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <!-- 初始化参数 --> <description>spring mvc 配置文件</description> <param-name>contextConfigLocation</param-name> <!-- 文件存放位置 --> <param-value>classpath:spring-mvc.xml</param-value> <!-- classes目录下 --> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>springMvc</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping>
(2)示例
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0"> <display-name>jeecg-framework-3.4.3 GA</display-name> <context-param> <param-name>webAppRootKey</param-name> <param-value>WeChatCMS</param-value> </context-param> <context-param> <param-name>log4jConfigLocation</param-name> <param-value>classpath:log4j.properties</param-value> </context-param> <listener> <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class> </listener> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring-*.xml</param-value> </context-param> <filter> <filter-name>openSessionInViewFilter</filter-name> <filter-class>org.springframework.orm.hibernate4.support.OpenSessionInViewFilter</filter-class> <init-param> <param-name>singleSession</param-name> <param-value>true</param-value> </init-param> </filter> <filter-mapping> <filter-name>openSessionInViewFilter</filter-name> <url-pattern>*.do</url-pattern> </filter-mapping> <filter> <description>字符集过滤器</description> <filter-name>encodingFilter</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <init-param> <description>字符集编码</description> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> </filter> <filter-mapping> <filter-name>encodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <filter> <filter-name>druidWebStatFilter</filter-name> <filter-class>com.alibaba.druid.support.http.WebStatFilter</filter-class> <init-param> <param-name>exclusions</param-name> <param-value>/css/*,/context/*,/plug-in/*,*.js,*.css,*/druid*,/attached/*,*.jsp</param-value> </init-param> <init-param> <param-name>principalSessionName</param-name> <param-value>sessionInfo</param-value> </init-param> <init-param> <param-name>profileEnable</param-name> <param-value>true</param-value> </init-param> </filter> <filter-mapping> <filter-name>druidWebStatFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!-- <filter> <filter-name>sessionFilter</filter-name> <filter-class>org.jeecgframework.web.system.filter.SessionFilter</filter-class> </filter> <filter-mapping> <filter-name>sessionFilter</filter-name> <url-pattern>*.do</url-pattern> </filter-mapping> --> <listener> <description>spring监听器</description> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <listener> <description>Introspector缓存清除监听器</description> <listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class> </listener> <listener> <description>request监听器</description> <listener-class>org.springframework.web.context.request.RequestContextListener</listener-class> </listener> <listener> <description>系统初始化监听器</description> <listener-class>org.jeecgframework.web.system.listener.InitListener</listener-class> </listener> <listener> <listener-class>org.jeecgframework.web.system.listener.OnlineListener</listener-class> </listener> <servlet> <description>spring mvc servlet</description> <servlet-name>springMvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <description>spring mvc 配置文件</description> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring-mvc.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>springMvc</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping> <servlet> <servlet-name>druidStatView</servlet-name> <servlet-class>com.alibaba.druid.support.http.StatViewServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>springMvc</servlet-name> <url-pattern>*.action</url-pattern> </servlet-mapping> <servlet> <servlet-name>restSpringMvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <description>spring mvc 配置文件</description> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring-mvc.xml</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>restSpringMvc</servlet-name> <url-pattern>/rest/*</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>druidStatView</servlet-name> <url-pattern>/webpage/system/druid/*</url-pattern> </servlet-mapping> <servlet> <servlet-name>ConnectorServlet</servlet-name> <servlet-class>com.ckfinder.connector.ConnectorServlet</servlet-class> <init-param> <param-name>XMLConfig</param-name> <param-value>/WEB-INF/classes/CKfinderConfig.xml</param-value> </init-param> <init-param> <param-name>debug</param-name> <param-value>false</param-value> </init-param> <init-param> <param-name>configuration</param-name> <param-value>org.jeecgframework.web.system.listener.CkfinderConfiguration</param-value> </init-param> <load-on-startup>2</load-on-startup> </servlet> <servlet-mapping> <servlet-name>ConnectorServlet</servlet-name> <url-pattern>/plug-in/ckfinder/core/connector/java/connector.java</url-pattern> </servlet-mapping> <servlet> <servlet-name>RandCodeImage</servlet-name> <servlet-class>org.jeecgframework.web.system.servlet.RandCodeImageServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>RandCodeImage</servlet-name> <url-pattern>/randCodeImage</url-pattern> </servlet-mapping> <filter> <filter-name>FileUploadFilter</filter-name> <filter-class>com.ckfinder.connector.FileUploadFilter</filter-class> <init-param> <param-name>sessionCookieName</param-name> <param-value>jsessionid</param-value> </init-param> <init-param> <param-name>sessionParameterName</param-name> <param-value>JSESSIONID</param-value> </init-param> </filter> <filter-mapping> <filter-name>FileUploadFilter</filter-name> <url-pattern>/plug-in/ckfinder/core/connector/java/connector.java</url-pattern> </filter-mapping> <error-page> <error-code>404</error-code> <location>/webpage/common/404.jsp</location> </error-page> <session-config> <session-timeout>120</session-timeout> </session-config> <welcome-file-list> <welcome-file>/webpage/login/login.jsp</welcome-file> </welcome-file-list> </web-app>
二:spring-mvc.xml的配置
(1)扫描控制器
(1.1)头部beans中加入:xmlns:context="http://www.springframework.org/schema/context"
(1.2)
<!-- 加载controller的时候,不加载service,因为此时事物并未生效,若此时加载了service,那么事物无法对service进行拦截 -->
<context:component-scan base-package="org.jeecgframework.web.*"> <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Service" /> </context:component-scan>
(2)默认视图解析器
<bean id="defaultViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver" p:order="3"> <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" /> <property name="contentType" value="text/html" /> <property name="prefix" value="/webpage/" /> <property name="suffix" value=".jsp" /> </bean>
(3)拦截器
<!-- 拦截器 --> <mvc:interceptors> <mvc:interceptor> <mvc:mapping path="/**" /> <bean class="org.jeecgframework.core.interceptors.EncodingInterceptor" /> </mvc:interceptor> <mvc:interceptor> <mvc:mapping path="/**" /> <bean class="org.jeecgframework.core.interceptors.AuthInterceptor"> <property name="excludeUrls"> <list> <value>loginController.do?goPwdInit</value> <value>loginController.do?pwdInit</value> <value>loginController.do?login</value> </list> </property> <!-- 模糊匹配 --> <property name="excludeContainUrls"> <list> <value>rest/openwx</value> <value>openDataController</value> </list> </property> </bean> </mvc:interceptor> </mvc:interceptors>
(4)示例
1.spring-mvc.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" 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 "> <!-- 自动扫描controller包下的所有类,使其认为spring mvc的控制器 --> <!-- 加载controller的时候,不加载service,因为此时事物并未生效,若此时加载了service,那么事物无法对service进行拦截 --> <context:component-scan base-package="org.jeecgframework.web.*,weixin.*"> <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Service" /> </context:component-scan> <!-- ApplicationContext --> <bean class="org.jeecgframework.core.util.ApplicationContextUtil"></bean> <!-- 避免IE执行AJAX时,返回JSON出现下载文件 --> <bean id="mappingJacksonHttpMessageConverter" class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"> <property name="supportedMediaTypes"> <list> <value>text/html;charset=UTF-8</value> </list> </property> </bean> <!-- 启动Spring MVC的注解功能,完成请求和注解POJO的映射, 配置一个基于注解的定制的WebBindingInitializer,解决日期转换问题,方法级别的处理器映射 --> <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"> <property name="cacheSeconds" value="0" /> <property name="messageConverters"> <list> <ref bean="mappingJacksonHttpMessageConverter" /><!-- json转换器 --> </list> </property> <property name="webBindingInitializer"> <bean class="org.jeecgframework.core.interceptors.MyWebBinding" /> </property> </bean> <!-- 默认的视图解析器 在上边的解析错误时使用 (默认使用html)- --> <bean id="defaultViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver" p:order="3"> <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" /> <property name="contentType" value="text/html" /> <property name="prefix" value="/webpage/" /> <property name="suffix" value=".jsp" /> </bean> <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver" p:defaultEncoding="UTF-8"> <property name="maxUploadSize"> <value>104857600</value> </property> <property name="maxInMemorySize"> <value>4096</value> </property> </bean> <!-- Bean解析器,级别高于默认解析器,寻找bean对象进行二次处理 --> <bean id="beanNameViewResolver" class="org.springframework.web.servlet.view.BeanNameViewResolver" p:order="0"> </bean> <!-- Excel 处理 根据用户输入进行对象处理 --> <bean id="jeecgExcelView" class="org.jeecgframework.poi.excel.view.JeecgSingleExcelView" /> <!-- 方言 --> <bean id="dialect" class="org.jeecgframework.core.common.hibernate.dialect.DialectFactoryBean"> <property name="dbType" value="${jdbc.dbType}" /> </bean> <!-- 异常处理类 --> <bean id="exceptionHandler" class="org.jeecgframework.core.common.exception.MyExceptionHandler" /> <!-- 系统错误转发配置[并记录错误日志] --> <bean class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver"> <property name="defaultErrorView" value="500"></property> <!-- 默认为500,系统错误(error.jsp) --> <property name="defaultStatusCode" value="404"></property> <property name="statusCodes"><!-- 配置多个statusCode --> <props> <prop key="error">500</prop> <!-- error.jsp --> <prop key="error1">404</prop> <!-- error1.jsp --> </props> </property> <property name="exceptionMappings"> <props> <!-- 这里你可以根据需要定义N多个错误异常转发 --> <prop key="java.sql.SQLException">dbError</prop> <!-- 数据库错误(dbError.jsp) --> <prop key="org.springframework.web.bind.ServletRequestBindingException">bizError</prop> <!-- 参数绑定错误(如:必须参数没传递)(bizError.jsp) --> <prop key="java.lang.IllegalArgumentException">bizError</prop> <!-- 参数错误(bizError.jsp) --> <prop key="org.springframework.validation.BindException">bizError</prop> <!-- 参数类型有误(bizError.jsp) --> <prop key="java.lang.Exception">unknowError</prop> <!-- 其他错误为'未定义错误'(unknowError.jsp) --> </props> </property> </bean> <!-- 拦截器 --> <mvc:interceptors> <mvc:interceptor> <mvc:mapping path="/**" /> <bean class="org.jeecgframework.core.interceptors.EncodingInterceptor" /> </mvc:interceptor> <mvc:interceptor> <mvc:mapping path="/**" /> <bean class="org.jeecgframework.core.interceptors.AuthInterceptor"> <property name="excludeUrls"> <list> <value>loginController.do?goPwdInit</value> <value>loginController.do?pwdInit</value> <value>loginController.do?login</value> <value>loginController.do?checkuser</value> <value>wechatController.do?wechat</value> <value>wechatController.do?token</value> <value>weixinCmsStyleController.do?upload</value> <value>newsItemController.do?goContent</value> <!-- 保存用户 --> <value>userController.do?saveUser</value> <value>weixinLinksucaiController.do?link</value> </list> </property> <!-- 模糊匹配 --> <property name="excludeContainUrls"> <list> <value>rest/openwx</value> <value>openDataController</value> </list> </property> </bean> </mvc:interceptor> </mvc:interceptors> </beans>
2.spring-mvc-hibernate.xml
<?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:util="http://www.springframework.org/schema/util" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:task="http://www.springframework.org/schema/task" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-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/tx http://www.springframework.org/schema/tx/spring-tx-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/task http://www.springframework.org/schema/task/spring-task-3.0.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd" default-autowire="byName" default-lazy-init="false"> <!-- 自动扫描dao和service包(自动注入) --> <context:component-scan base-package="org.jeecgframework.core.common.dao.*" /> <context:component-scan base-package="org.jeecgframework.core.common.service.*" /> <!-- 加载service,此时要排除要controller,因为controller已经spring-mvc中加载过了 --> <context:component-scan base-package="org.jeecgframework.web.*,weixin.*"> <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller" /> </context:component-scan> <!-- 引入属性文件 --> <context:property-placeholder location="classpath:dbconfig.properties" /> <!-- 配置数据源1 --> <bean name="dataSource_jeecg" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close"> <property name="url" value="${jdbc.url.jeecg}" /> <property name="username" value="${jdbc.username.jeecg}" /> <property name="password" value="${jdbc.password.jeecg}" /> <!-- 初始化连接大小 --> <property name="initialSize" value="0" /> <!-- 连接池最大使用连接数量 --> <property name="maxActive" value="20" /> <!-- 连接池最大空闲 --> <property name="maxIdle" value="20" /> <!-- 连接池最小空闲 --> <property name="minIdle" value="5" /> <!-- 获取连接最大等待时间 --> <property name="maxWait" value="60000" /> <!-- <property name="poolPreparedStatements" value="true" /> <property name="maxPoolPreparedStatementPerConnectionSize" value="33" /> --> <property name="validationQuery" value="${validationQuery.sqlserver}" /> <property name="testOnBorrow" value="false" /> <property name="testOnReturn" value="false" /> <property name="testWhileIdle" value="true" /> <!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 --> <property name="timeBetweenEvictionRunsMillis" value="60000" /> <!-- 配置一个连接在池中最小生存的时间,单位是毫秒 --> <property name="minEvictableIdleTimeMillis" value="252000" /> <!-- 打开removeAbandoned功能 --> <property name="removeAbandoned" value="true" /> <!-- 1800秒,也就是30分钟 --> <property name="removeAbandonedTimeout" value="1800" /> <!-- 关闭abanded连接时输出错误日志 --> <property name="logAbandoned" value="true" /> <!-- 开启Druid的监控统计功能 --> <property name="filters" value="stat" /> <!--<property name="filters" value="mergeStat" /> --> <!-- Oracle连接是获取字段注释 --> <property name="connectProperties"> <props> <prop key="remarksReporting">true</prop> </props> </property> </bean> <!-- 数据源集合 --> <bean id="dataSource" class="org.jeecgframework.core.extend.datasource.DynamicDataSource"> <property name="targetDataSources"> <map key-type="org.jeecgframework.core.extend.datasource.DataSourceType"> <entry key="dataSource_jeecg" value-ref="dataSource_jeecg" /> <!-- <entry key="mapdataSource" value-ref="mapdataSource" /> --> </map> </property> <property name="defaultTargetDataSource" ref="dataSource_jeecg" /> </bean> <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"> <property name="dataSource" ref="dataSource" /> <property name="entityInterceptor" ref="hiberAspect" /> <property name="hibernateProperties"> <props> <!--<prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop> --> <prop key="hibernate.dialect">${hibernate.dialect}</prop> <prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop> <prop key="hibernate.show_sql">true</prop> <prop key="hibernate.format_sql">true</prop> <prop key="hibernate.temp.use_jdbc_metadata_defaults">false</prop> </props> </property> <!-- 注解方式配置 --> <property name="packagesToScan"> <list> <value>org.jeecgframework.web.system.pojo.*</value> <value>org.jeecgframework.web.demo.entity.*</value> <value>org.jeecgframework.web.test.entity.*</value> <value>org.jeecgframework.web.cgform.entity.*</value> <value>org.jeecgframework.web.cgreport.entity.*</value> <value>org.jeecgframework.web.rest.*</value> <value>weixin.*</value> </list> </property> </bean> <!-- JDBC配置 --> <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"> <property name="dataSource"> <ref bean="dataSource" /> </property> </bean> <!-- JDBC配置 --> <bean id="namedParameterJdbcTemplate" class="org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate"> <constructor-arg ref="dataSource" /> </bean> <!-- 配置事物管理器,在*ServiceImpl里写@Transactional就可以启用事物管理 --> <bean name="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory"></property> </bean> <tx:annotation-driven transaction-manager="transactionManager" /> <!-- JSR303 Validator定义 --> <bean id="validator" class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean" /> </beans>