潛心學習 技術強身

            BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理 ::
            14 隨筆 :: 0 文章 :: 8 評論 :: 0 Trackbacks
           

          二、Struts2Spring2.5的整合

          1)在web.xml中加入Struts2過濾器

          <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>

          2)導入struts2.1.6jar

          這個里要導入7jar包,可能很多教程上說只要加如5個核心包就可以了,但是2.1.6卻要另外導入commons-io-1.3.2.jarcommons-fileupload-1.2.1.jar兩個包,而且這個說明在官網的doc中也沒有說明,這是我們需要注意一點的。當然我們也可以把struts2的所有包導入。

          3)加入struts2spring整合的jar

          struts2-spring-plugin-2.1.6.jar導入項目中。

          4)在src下建一個struts.xml,默認格式如下:

          <?xml version="1.0" encoding="UTF-8" ?>

          <!DOCTYPE struts PUBLIC

              "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"

              "http://struts.apache.org/dtds/struts-2.0.dtd">

          <struts>

             

          </struts>

           <struts></struts>之間添加一個常量,將struts交給spring管理。

          <struts>

              <constant name="struts.objectFactory" value="spring"/>

          </struts>

          5)修改web.xml文件,配置Spring監聽器和上下文變量

          <context-param>

            <param-name>contextConfigLocation</param-name>

            <param-value>/WEB-INF/applicationContext*.xml</param-value>

           </context-param>

           <listener>

               <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>

           </listener>

          注意:此配置需要放在過濾器之前。

                 增加OpenSessionInView過濾器的設置,放在struts過濾器之前

          作用:

          Spring中對OpenSessionInViewFilter的描述:它是一個Servlet2.3過濾器,用來把一個Hibernate Session和一次完整的請求過程對應的線程相綁定。目的是為了實現"Open Session in View"的模式。例如: 它允許在事務提交之后延遲加載顯示所需要的對象。






           

          <filter>

            <filter-name>lazyLoadingFilter</filter-name>

               <filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>

           </filter>

          <filter-mapping>

            <filter-name>lazyLoadingFiter</filter-name>

            <url-pattern>*.action</url-pattern>

           </filter-mapping>

          6)編寫Action類繼承ActionSupport

          package cn.zhang.crm.action;

          import java.util.List;

          import cn.zhang.crm.model.pojo.Employee;

          import cn.zhang.crm.service.EmployeeManager;

          import com.opensymphony.xwork2.ActionSupport;

          publicclass EmployeeAction extends ActionSupport {

              privatestaticfinallongserialVersionUID = -3117990268615208697L;

              private EmployeeManager employeeManager;

              private Employee employee;

              private List employees;

              privateintid;

             

              publicint getId() {

                 returnid;

              }

              publicvoid setId(int id) {

                 this.id = id;

              }

              publicvoid setEmployeeManager(EmployeeManager employeeManager) {

                 this.employeeManager = employeeManager;

              }

             

              public String add(){

                 employeeManager.addEmployee(employee);

                 returnSUCCESS;

              }

             

              public String list(){

                 this.employees = employeeManager.listEmployee();

                 returnSUCCESS;

              }

             

              public String delete(){

                 employeeManager.deleteEmployee(id);

                 returnSUCCESS;

              }

              public List getEmployees() {

                 returnemployees;

              }

              publicvoid setEmployees(List employees) {

                 this.employees = employees;

              }

              publicvoid setEmployee(Employee employee) {

                 this.employee = employee;

              }

              public Employee getEmployee() {

                 returnemployee;

              }

          }

          7)配置struts.xml,將action寫入

          <package name="crm_employee" extends="struts-default" namespace="/emp">

              <action name="add" class="addBean" method="add">

                  <result type="chain">

                      <param name="actionName">list</param>

                  </result>

              </action>

                 <action name="list" class="listBean" method="list">

                     <result>list.jsp</result>

                 </action>

                 <action name="delete" class="deleteBean" method="delete">

                     <result>list.action</result>

                 </action>

              <!-- Add actions here -->

              </package>

          8)在applicationContext.xml中增加bean映射

          <bean id="addBean" class="cn.zhang.crm.action.EmployeeAction" scope="prototype">

                 <property name="employeeManager">

                     <ref bean="employeeManager" />

                 </property>

              </bean>

              <bean id="listBean" class="cn.zhang.crm.action.EmployeeAction" scope="prototype">

                 <property name="employeeManager">

                     <ref bean="employeeManager" />

                 </property>

              </bean>

              <bean id="deleteBean" class="cn.zhang.crm.action.EmployeeAction" scope="prototype">

                 <property name="employeeManager">

                     <ref bean="employeeManager" />

                 </property>

              </bean>

          同時增加事務的處理

          <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">

                 <property name="sessionFactory">

                     <ref local="sessionFactory"/>

                 </property>

              </bean>

              <!-- 配置事務特性,處理adddeleteupdate等開始方法,傳播特性為REQUIRED-->

              <tx:advice id="txAdvice" transaction-manager="transactionManager">

                  <tx:attributes>

                      <tx:method name="add*" propagation="REQUIRED" />

                      <tx:method name="delete*" propagation="REQUIRED" />

                      <tx:method name="update*" propagation="REQUIRED" />

                      <tx:method name="list*" propagation="REQUIRED" />

                      <tx:method name="*" read-only="true" />

                  </tx:attributes>

              </tx:advice>

              <aop:config>

                  <aop:pointcut id="allManagerMethod"

                      expression="execution(* cn.zhang.crm.service.*.*(..))" />

                  <aop:advisor advice-ref="txAdvice"

                      pointcut-ref="allManagerMethod" />

              </aop:config>

          要讓applicationContext.xml認得事務處理,我們需要將beans頭識別修改一下,如下:

          <beans

              xmlns="http://www.springframework.org/schema/beans"

              xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

              xmlns:aop="http://www.springframework.org/schema/aop"

              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/tx

                                   http://www.springframework.org/schema/tx/spring-tx-2.5.xsd

                                http://www.springframework.org/schema/aop

                                   http://www.springframework.org/schema/aop/spring-aop-2.5.xsd ">

          9)添加jsp文件

          emp/add.jsp

          <%@ page language="java" pageEncoding="UTF-8"%>

          <%@ taglib uri="/struts-tags" prefix="s"%>

          <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

          <html>

           <head>

             

              <title>雇員用戶信息增加</title>

             

              <meta http-equiv="pragma" content="no-cache">

              <meta http-equiv="cache-control" content="no-cache">

              <meta http-equiv="expires" content="0">   

              <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">

              <meta http-equiv="description" content="This is my page">

              <!--

              <link rel="stylesheet" type="text/css" href="styles.css">

              -->

           </head>

           

           <body>

              <h1>雇員用戶信息輸入</h1>

              <s:form action="add">

              <s:textfield name="employee.name" label="姓名" />

              <s:textfield name="employee.address" label="地址"/>

              <s:textfield name="employee.phone" label="電話" />

              <s:submit/>

              </s:form>

           </body>

          </html>

          emp/list.jsp

          <%@ page language="java" pageEncoding="UTF-8"%>

          <%@ taglib uri="/struts-tags" prefix="s"%>

          <%

          String path = request.getContextPath();

          String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";

          %>

          <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

          <html>

           <head>

              <base href="<%=basePath%>">

             

              <title>雇員用戶信息顯示</title>

             

              <meta http-equiv="pragma" content="no-cache">

              <meta http-equiv="cache-control" content="no-cache">

              <meta http-equiv="expires" content="0">   

              <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">

              <meta http-equiv="description" content="This is my page">

              <!--

              <link rel="stylesheet" type="text/css" href="styles.css">

              -->

           </head>

           

           <body>

              <h1>雇員用戶信息顯示</h1>

              <table>

              <tr>

                  <td>編號</td>

                  <td>姓名</td>

                  <td>地址</td>

                  <td>電話</td>

              </tr>

              <s:iterator value="employees">

              <tr>

                  <td><s:property value="id"/></td>

                  <td><s:property value="name"/></td>

                  <td><s:property value="address"/></td>

                  <td><s:property value="phone"/></td>

              </tr>

              </s:iterator>

              </table>

           </body>

          </html>

          10)部署,測試效果

          http://localhost:8088/crm/emp/add.jsp

          到這里,整個實例算運行起來了。至于其他的幾個操作,如修改、刪除就自己做了。
          posted on 2009-07-22 17:02 平濤 閱讀(1663) 評論(0)  編輯  收藏 所屬分類: 學習筆記
          主站蜘蛛池模板: 独山县| 双峰县| 保亭| 怀远县| 大宁县| 古丈县| 富民县| 淮南市| 肇东市| 阳东县| 武穴市| 桑植县| 江永县| 桦南县| 双辽市| 大同县| 华坪县| 荥阳市| 武川县| 南部县| 通榆县| 陆丰市| 大姚县| 大冶市| 德兴市| 彰化县| 龙山县| 腾冲县| 菏泽市| 民丰县| 砀山县| 修武县| 苍梧县| 丰顺县| 佛坪县| 陇南市| 镇康县| 堆龙德庆县| 娄烦县| 马关县| 阳山县|