1、先集成spring与hibernate(与上一节讲的相同) 文件beans.xml
- <?xml version="1.0" encoding="UTF-8"?>
- <beans xmlns=""
- xmlns:xsi=""
- xmlns:context=""
- xmlns:aop=""
- xmlns:tx=""
- xsi:schemaLocation="
- /spring-beans-2.5.xsd
- /spring-context-2.5.xsd
- /spring-aop-2.5.xsd
- /spring-tx-2.5.xsd">
- <context:annotation-config/>
- <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
- <property name="driverClassName" value="org.gjt.mm.mysql.Driver"/>
- <property name="url" value="jdbc:mysql://localhost:3306/itcast?useUnicode=true&characterEncoding=UTF-8"/>
- <property name="username" value="root"/>
- <property name="password" value="123456"/>
- <!-- 连接池启动时的初始值 -->
- <property name="initialSize" value="1"/>
- <!-- 连接池的最大值 -->
- <property name="maxActive" value="500"/>
- <!-- 最大空闲值.当经过一个高峰时间后,连接池可以慢慢将已经用不到的连接慢慢释放一部分,一直减少到maxIdle为止 -->
- <property name="maxIdle" value="2"/>
- <!-- 最小空闲值.当空闲的连接数少于阀值时,连接池就会预申请去一些连接,以免洪峰来时来不及申请 -->
- <property name="minIdle" value="1"/>
- </bean>
-
- <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
- <property name="dataSource" ref="dataSource"/>
- <property name="mappingResources">
- <list>
- <value>cn/itcast/bean/Person.hbm.xml</value>
- </list>
- </property>
- <property name="hibernateProperties">
- <value>
- hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
- hibernate.hbm2ddl.auto=update
- hibernate.show_sql=false
- hibernate.format_sql=false
- hibernate.cache.use_second_level_cache=true
- hibernate.cache.use_query_cache=false
- hibernate.cache.provider_class=org.hibernate.cache.EhCacheProvider
- </value>
- </property>
- </bean>
- <bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
- <property name="sessionFactory" ref="sessionFactory"/>
- </bean>
- <tx:annotation-driven transaction-manager="txManager"/>
- <bean id="personService" class="cn.itcast.service.impl.PersonServiceBean"/>
- <bean id="personList" class="cn.itcast.web.PersonAction"/>
- </beans>
2、配置struts2(struts.xml)
- <?xml version="1.0" encoding="UTF-8"?>
- <!DOCTYPE struts PUBLIC
- "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
- "">
- <struts>
- <!-- 指定Web应用的默认编码集,相当于调用HttpServletRequest的setCharacterEncoding方法 -->
- <constant name="struts.i18n.encoding" value="UTF-8"/>
- <!-- 该属性指定需要Struts 2处理的请求后缀,该属性的默认值是action,即所有匹配*.action的请求都由Struts2处理。
- 如果用户需要指定多个请求后缀,则多个后缀之间以英文逗号(,)隔开。 -->
- <constant name="struts.action.extension" value="do"/>
- <!-- 设置浏览器是否缓存静态内容,默认值为true(生产环境下使用),开发阶段最好关闭 -->
- <constant name="struts.serve.static.browserCache" value="false"/>
- <!-- 当struts的配置文件修改后,系统是否自动重新加载该文件,默认值为false(生产环境下使用),开发阶段最好打开 -->
- <constant name="struts.configuration.xml.reload" value="true"/>
- <!-- 开发模式下使用,这样可以打印出更详细的错误信息 -->
- <constant name="struts.devMode" value="true" />
- <!-- 默认的视图主题 -->
- <constant name="struts.ui.theme" value="simple" />
- <!-- action这些对象是由spring来负责创建 -->
- <constant name="struts.objectFactory" value="spring" />
-
- <package name="person" namespace="/person" extends="struts-default">
- <global-results>
- <result name="message">/WEB-INF/page/message.jsp</result>
- </global-results>
- <action name="action_*" class="personList" method="{1}">
- <result name="list">/WEB-INF/page/personlist.jsp</result>
- <result name="add">/WEB-INF/page/addperson.jsp</result>
- </action>
- </package>
- </struts>
3、把spring 与struts2注册到web容器中
通过listener实例化spring容器,这个实例放到什么范围里面去,放到servletContext对象去,那么应用一开始他就创建,应用关闭它才销毁。
- 在web容器中实例化spring容器,
- <!-- 指定spring的配置文件,默认从web根目录寻找配置文件,我们可以通过spring提供的classpath:前缀指定从类路径下寻找 -->
- <context-param>
- <param-name>contextConfigLocation</param-name>
- <param-value>classpath:beans.xml</param-value>
- </context-param>
- <!-- 对Spring容器进行实例化 -->
- <listener>
- <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
- </listener>
在web容器中配置struts2
- <servlet>
- <servlet-name>struts</servlet-name>
- <servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
- <init-param>
- <param-name>config</param-name>
- <param-value>/WEB-INF/struts-config.xml</param-value>
- </init-param>
- <load-on-startup>0</load-on-startup>
- </servlet>
- <servlet-mapping>
- <servlet-name>struts</servlet-name>
- <url-pattern>*.do</url-pattern>
- </servlet-mapping>
使用spring解决hibernate因session关闭导致的延迟加载例外问题。
- <filter>
- <filter-name>OpenSessionInViewFilter</filter-name>
- <filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
- </filter>
- <filter-mapping>
- <filter-name>OpenSessionInViewFilter</filter-name>
- <url-pattern>/*</url-pattern>
- </filter-mapping>
web.xml
- <?xml version="1.0" encoding="UTF-8"?>
- <web-app version="2.4"
- xmlns=""
- xmlns:xsi=""
- xsi:schemaLocation="
- /web-app_2_4.xsd">
- <context-param>
- <param-name>contextConfigLocation</param-name>
- <param-value>classpath:beans.xml</param-value>
- </context-param>
- <!-- 对Spring容器进行实例化 -->
- <listener>
- <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
- </listener>
-
- <filter>
- <filter-name>struts2</filter-name>
- <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
- </filter>
- <filter-mapping>
- <filter-name>struts2</filter-name>
- <url-pattern>/*</url-pattern>
- </filter-mapping>
- <filter>
- <filter-name>OpenSessionInViewFilter</filter-name>
- <filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
- </filter>
- <filter-mapping>
- <filter-name>OpenSessionInViewFilter</filter-name>
- <url-pattern>/*</url-pattern>
- </filter-mapping>
- <welcome-file-list>
- <welcome-file>index.jsp</welcome-file>
- </welcome-file-list>
- </web-app>
阅读(666) | 评论(0) | 转发(0) |