到目前,struts、hibernate、spring在总体上整合到一块了,但是struts组件还没有整合进来
14、让spring接管struts(就是接管Action控件)
14.1)在struts-config.xml文件中添加如下代码配置:
<!-- 配置代理请求处理 DelegatingRequestProcessor它的用处是将请求处理转到DelegatingRequestProcessor,从而在spring中查找action,从而由spring管理action --> <controller> <set-property property="processorClass" value="org.springframework.web.struts.DelegatingRequestProcessor" /> </controller>
<!-- 配置struts的组件:Action --> <bean name="/login" class="com.cdtax.web.action.LoginAction"> </bean>这里的name,其值要与struts-congfig.xml中的action的path值相同,class就是action的具体类名,因为这里已经有了class了,struts-config.xml中的action的type就可以去掉。
struts-config.xml文件:
<?xml version="1.0" encoding="utf-8"?> <!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.3//EN" "http://struts.apache.org/dtds/struts-config_1_3.dtd"> <struts-config> <form-beans> <form-bean name="employeeForm" type="com.cdtax.web.forms.EmployeeForm" /> </form-beans> <action-mappings> <action path="/login" parameter="flag" name="employeeForm"> <forward name="ok" path="/WEB-INF/mainFrame.jsp" /> <forward name="err" path="/WEB-INF/login.jsp" /> </action> </action-mappings> <!-- 配置代理请求处理 DelegatingRequestProcessor它的用处是 --> <controller> <set-property property="processorClass" value="org.springframework.web.struts.DelegatingRequestProcessor" /> </controller> </struts-config>
14.3)这样我们就可以通过spring容器来获取action,并且可以同时配置action的一些属性
要通过spring的方式管理action,对action进行一些改造:
package com.cdtax.web.action; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.struts.action.ActionForm; import org.apache.struts.action.ActionForward; import org.apache.struts.action.ActionMapping; import org.apache.struts.actions.DispatchAction; import org.springframework.web.context.WebApplicationContext; import org.springframework.web.context.support.WebApplicationContextUtils; import com.cdtax.domain.Employee; import com.cdtax.service.interfaces.EmployeeServiceInter; import com.cdtax.web.forms.EmployeeForm; public class LoginAction extends DispatchAction { // ApplicationContext ac = new ClassPathXmlApplicationContext(""); //为了使用spring属性注入,定义变量及set方法 private EmployeeServiceInter employeeServiceInter; public void setEmployeeServiceInter(EmployeeServiceInter employeeServiceInter) { System.out.println("setEmployeeServiceInter 方法被调用"); this.employeeServiceInter = employeeServiceInter; } //响应登陆请求 public ActionForward login(ActionMapping arg0, ActionForm arg1, HttpServletRequest arg2, HttpServletResponse arg3) throws Exception { System.out.println("------通过新的方式响应请求:spring管理-----"); //通过下面语句,可以直接获取到spring容器实例,即我们前面讲的ApplicationContext //当通过spring来进行action的管理时,就不需要下面这行代码了,注释掉 // WebApplicationContext ctx = // WebApplicationContextUtils // .getWebApplicationContext(this.getServlet().getServletContext()); //从spring容器中获取bean //当使用spring来进行action的管理时,这个实例可以通过spring的依赖注入来注入进来,定义一个变量employeeServiceInter,生成set方法 //然后在spring配置文件中的bean中进行属性配置,如下: // <bean name="/login" class="com.cdtax.web.action.LoginAction"> // <property name="employeeServiceInter" ref="employeeService" /> // </bean> // EmployeeServiceInter employeeServiceInter = (EmployeeServiceInter) ctx.getBean("employeeService"); //取出表单,我们先打通练习,我们简单验证 EmployeeForm employeeForm = (EmployeeForm)arg1; //构建一个Employee对象 Employee e = new Employee(); e.setId(Integer.parseInt(employeeForm.getId())); e.setPwd(employeeForm.getPwd()); e = employeeServiceInter.checkEmployee(e); if(e != null) { //把雇员信息放入session,后面可以使用 arg2.getSession().setAttribute("loginer", e); return arg0.findForward("ok"); } else { return arg0.findForward("err"); } } //响应注销请求 public ActionForward logout(ActionMapping arg0, ActionForm arg1, HttpServletRequest arg2, HttpServletResponse arg3) throws Exception { // TODO Auto-generated method stub return super.execute(arg0, arg1, arg2, arg3); } }当通过spring来进行action的管理时,就不需要下面这行代码来手动获取applicationContext实例了,注释掉
当使用spring来进行action的管理时,employeeServiceInter这个实例可以通过spring的依赖注入来注入进来,定义一个变量employeeServiceInter,生成set方法,/然后在spring配置文件中的bean中进行属性配置,如下:
<bean name="/login" class="com.cdtax.web.action.LoginAction">
<property name="employeeServiceInter" ref="employeeService" />
</bean>
这一行就注释掉// EmployeeServiceInter employeeServiceInter = (EmployeeServiceInter) ctx.getBean("employeeService");
我们在employeeServiceInter的set方法中添加打印语句,看一下它的调用时机。
然后再看看applicationContext.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:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"> <!-- 配置一个testService对象,测试spring集成是否成功用 --> <bean id="testService" class="com.cdtax.test.TestService"> <property name="name" value="小明"></property> </bean> <!-- 配置数据源 --> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName" value="com.mysql.jdbc.Driver" /> <property name="url" value="jdbc:mysql://localhost:3306/hibernate" /> <property name="username" value="root" /> <property name="password" value="root" /> <!-- 连接池启动时的初始值 --> <property name="initialSize" value="3" /> <!-- 连接池的最大值 --> <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" /><!-- 应该想到类中有setDataSource()方法 --> <!-- 接管了haibernate的对象映射文件 --> <property name="mappingResources"><!-- 应该想到类中有setMappingResources()方法 --> <list> <value>com/cdtax/domain/Employee.hbm.xml</value> </list> </property> <property name="hibernateProperties"> <value> hibernate.dialect=org.hibernate.dialect.MySQLDialect hibernate.hbm2ddl.auto=update hibernate.show_sql=true hibernate.format_sql=true <!-- 配置hibernate二级缓存 --> hibernate.cache.use_second_level_cache=true hibernate.cache.provider_class=org.hibernate.cache.EhCacheProvider hibernate.generate_statistics=true </value> </property> </bean> <!-- 配置EmployeeService对象 --> <bean id="employeeService" class="com.cdtax.service.impl.EmployeeService"> <property name="sessionFactory" ref="sessionFactory"></property> </bean> <!-- 配置struts的组件:Action --> <bean name="/login" class="com.cdtax.web.action.LoginAction"> <property name="employeeServiceInter" ref="employeeService" /> </bean> <!-- 配置事务管理器,统一管理sessionFactory的事务 --> <bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory" /> </bean> <!-- 启用事务注解 --> <tx:annotation-driven transaction-manager="txManager"/> </beans>注意看配置struts的组件:action的配置项
这些完成后,进行重新部署,然后启动tomcat,这时请注意观察,虽然我们还没有进行任何登陆操作(如打开登陆页面等),也就是没有向web服务器发送任何请求,但是:setEmployeeServiceInter 方法被调用这一句已经被打印出来了,说明在初始化时,action就被创建了,并且其属性employeeServiceInter也被注入了。这就是spring容器初始化的工作。
至此,struts与spring整合完成,struts的action由spring管理
14.4)通过使用spring来接管我们的Action还有一个好处,可以解决action是单例的问题。对于struts框架,action都是单例的,也就是说,在整个web生命周期中,同一个action只存在一个实例,存在线程安全与处理效率方面的问题,通过在applicationContext.xml文件中配置属性
<bean scope="singleton/prototype/request/session/globalsession" />,可以定义bean是单例的(singleton)还是每次请求都生成一个新的实例(如prototype)等。
举例测试,修改LoginAction:
package com.cdtax.web.action; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.struts.action.ActionForm; import org.apache.struts.action.ActionForward; import org.apache.struts.action.ActionMapping; import org.apache.struts.actions.DispatchAction; import org.springframework.web.context.WebApplicationContext; import org.springframework.web.context.support.WebApplicationContextUtils; import com.cdtax.domain.Employee; import com.cdtax.service.interfaces.EmployeeServiceInter; import com.cdtax.web.forms.EmployeeForm; public class LoginAction extends DispatchAction { // ApplicationContext ac = new ClassPathXmlApplicationContext(""); //为了使用spring属性注入,定义变量及set方法 private EmployeeServiceInter employeeServiceInter; //测试单例与否的变量 private int a = 0; public void setEmployeeServiceInter(EmployeeServiceInter employeeServiceInter) { System.out.println("setEmployeeServiceInter 方法被调用"); this.employeeServiceInter = employeeServiceInter; } //响应登陆请求 public ActionForward login(ActionMapping arg0, ActionForm arg1, HttpServletRequest arg2, HttpServletResponse arg3) throws Exception { System.out.println("------通过新的方式响应请求:spring管理-----"); //如果每次请求打印a都相同,证明不是单例,如果每次都是递增,说明是单例 System.out.println("a = " + (++a)); //通过下面语句,可以直接获取到spring容器实例,即我们前面讲的ApplicationContext //当通过spring来进行action的管理时,就不需要下面这行代码了,注释掉 // WebApplicationContext ctx = // WebApplicationContextUtils // .getWebApplicationContext(this.getServlet().getServletContext()); //从spring容器中获取bean //当使用spring来进行action的管理时,这个实例可以通过spring的依赖注入来注入进来,定义一个变量employeeServiceInter,生成set方法 //然后在spring配置文件中的bean中进行属性配置,如下: // <bean name="/login" class="com.cdtax.web.action.LoginAction"> // <property name="employeeServiceInter" ref="employeeService" /> // </bean> // EmployeeServiceInter employeeServiceInter = (EmployeeServiceInter) ctx.getBean("employeeService"); //取出表单,我们先打通练习,我们简单验证 EmployeeForm employeeForm = (EmployeeForm)arg1; //构建一个Employee对象 Employee e = new Employee(); e.setId(Integer.parseInt(employeeForm.getId())); e.setPwd(employeeForm.getPwd()); e = employeeServiceInter.checkEmployee(e); if(e != null) { //把雇员信息放入session,后面可以使用 arg2.getSession().setAttribute("loginer", e); return arg0.findForward("ok"); } else { return arg0.findForward("err"); } } //响应注销请求 public ActionForward logout(ActionMapping arg0, ActionForm arg1, HttpServletRequest arg2, HttpServletResponse arg3) throws Exception { // TODO Auto-generated method stub return super.execute(arg0, arg1, arg2, arg3); } }spring的配置文件applicationContext.xml不变,部署运行,第一次请求结果
------通过新的方式响应请求:spring管理-----
a = 1
第二次请求:
------通过新的方式响应请求:spring管理-----
a = 2
第三次请求
------通过新的方式响应请求:spring管理-----
a = 3
说明是同一个action在为我们服务,是单例的
修改applicationContext.xml中的action的bean配置:
<!-- 配置struts的组件:Action --> <bean name="/login" scope="prototype" class="com.cdtax.web.action.LoginAction"> <property name="employeeServiceInter" ref="employeeService" /> </bean>
------通过新的方式响应请求:spring管理-----
a = 1
说明每次为我们服务的action都是新生成的实例。
spring管理struts的action的流程图:
15、解决中文乱码问题
思路1:自己配置过滤器
步骤:(1)开发一个Filter(过滤器,过滤器属于web层的,因为他的本质也是一个servlet)
package com.cdtax.web.filter; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.Filter; import javax.servlet.FilterChain; import javax.servlet.FilterConfig; import javax.servlet.ServletException; import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class MyFilter extends HttpServlet implements Filter { public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { request.setCharacterEncoding("utf-8"); chain.doFilter(request, response); } public void init(FilterConfig filterConfig) throws ServletException { // TODO Auto-generated method stub } }
(2)在web.xml中配置过滤器
<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" 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_2_5.xsd"> <!-- 配置struts --> <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> <description>This is the description of my J2EE component</description> <display-name>This is the display name of my J2EE component</display-name> <servlet-name>MyFilter</servlet-name> <servlet-class>com.cdtax.web.filter.MyFilter</servlet-class> </servlet> <servlet-mapping> <servlet-name>struts</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>MyFilter</servlet-name> <url-pattern>/MyFilter</url-pattern> </servlet-mapping> <!-- 指定spring的配置文件,默认从web根目录寻找配置文件,我们可以通过spring提供的classpath:前缀指定从类路径下寻找 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> </context-param> <!-- 对Spring容器进行实例化 --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- 配置自己写的过滤器解决乱码问题 --> <filter> <filter-name>MyFilter</filter-name> <filter-class>com.cdtax.web.filter.MyFilter</filter-class> </filter> <filter-mapping> <filter-name>MyFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app>思路2:使用spring框架提供的处理中文乱码的过滤器
<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" 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_2_5.xsd"> <!-- 配置struts --> <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> <description>This is the description of my J2EE component</description> <display-name>This is the display name of my J2EE component</display-name> <servlet-name>MyFilter</servlet-name> <servlet-class>com.cdtax.web.filter.MyFilter</servlet-class> </servlet> <servlet-mapping> <servlet-name>struts</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>MyFilter</servlet-name> <url-pattern>/MyFilter</url-pattern> </servlet-mapping> <!-- 指定spring的配置文件,默认从web根目录寻找配置文件,我们可以通过spring提供的classpath:前缀指定从类路径下寻找 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> </context-param> <!-- 对Spring容器进行实例化 --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- 配置自己写的过滤器解决乱码问题 --> <!-- <filter> <filter-name>MyFilter</filter-name> <filter-class>com.cdtax.web.filter.MyFilter</filter-class> </filter> <filter-mapping> <filter-name>MyFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> --> <!-- 使用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> </web-app>
org.springframework.web.filter.CharacterEncodingFilter的作用与我们自己写的filter作用是一样的。