struts2.1.8+spring2.5+hibernate3.2+ext3.0框架整合筆記

一、struts2搭建

1、先導入xwork-core-2.1.6.jar,commons-fileupload-1.2.1.jar,commons-logging.jar,freemarker-2.3.15.jar,struts2-core-2.1.8.1.jar,ognl-2.6.11.jar這六個包

2、在web.xml中添加如下代碼:

  <filter>
   <filter-name>struts2</filter-name>
   <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
   <!-- <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class> -->
  </filter>
  <filter-mapping>
   <filter-name>struts2</filter-name>
   <url-pattern>*.action</url-pattern>
  </filter-mapping>

3、添加一個action的java類

例如:org.struts2.action.AddAction

4、在struts.xml中配置action類

  <action name="calc" class="org.struts2.action.AddAction" >
   <result name="success">/WEB-INF/test/add.jsp</result>
  </action>

5、編寫jsp結果頁面

6、測試,完成。源碼見於:http://download.csdn.net/detail/phoenixdsf/4360928

 

二、整合struts2和spring2.5(使用myeclipse自動導包)

1、在web.xml中新添加一段,如下:

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

2、改變autowiring插件裝配方式;1表示按名稱裝配,2表示按類型裝配,3表示按構造方法裝配,4表示自動裝配。我適用的是2表示按類型裝配。在struts.xml文件中加入如下代碼:

  <interceptors>
   <interceptor-stack name="extstack">
    <interceptor-ref name="autowiring">
     <param name="autowireStrategy">2</param>
    </interceptor-ref>
    <interceptor-ref name="defaultStack"></interceptor-ref>
   </interceptor-stack>
  </interceptors>

3、在action元素中添加攔截器棧

  <action name="springCalc" class="org.struts2.action.SpringCalcAction" >
   <interceptor-ref name="extstack"></interceptor-ref>
   <result name="success">/WEB-INF/test/calc.jsp</result>
  </action>

4、寫接口Calculator.java

5、寫實現CalculatorMessage.java

6、寫繼承AdditionCalc.java和SubtractionCalc.java

7、寫一個新的action,SpringCalcAction.java

8、裝配SpringCalcAction

 <bean id="additionCalc" class="org.spring2_5.bean.extend.AdditionCalc">
  <property name="message">
   <value>{0}+{1}={2}</value>
  </property>
 </bean>

9、在web.xml中加入contextConfigLocation來裝入其他裝配文件

  <context-param>
   <param-name>contextConfigLocation</param-name>
   <param-value>/WEB-INF/classes/applicationContext.xml,/WEB-INF/classes/applicationContext-calculator.xml,/WEB-INF/classes/applicationContext-*.xml</param-value>
  </context-param>

10、或者在applicationContext.xml中引入裝配文件,9和10中使用其中一種方法即可

11、測試,完成。源碼見於:http://download.csdn.net/detail/phoenixdsf/4360995

 三、整合SSH

1、使用myeclipse自動導包

2、在myeclipse裏的DB Browser新建一個數據庫連接(用於自動生成代碼,不是必要步驟)

3、新建實體-hbm.xml-DAO

4、配置applicationContext.xml

<bean id="dataSource"
  class="org.apache.commons.dbcp.BasicDataSource">
  <property name="driverClassName"
   value="com.mysql.jdbc.Driver">
  </property>
  <property name="url" value="jdbc:mysql://127.0.0.1:3306/test"></property>
  <property name="username" value="root"></property>
  <property name="password" value="321654"></property>
 </bean>
 <bean id="sessionFactory"
  class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
  <property name="dataSource">
   <ref bean="dataSource" />
  </property>
  <property name="hibernateProperties">
   <props>
    <prop key="hibernate.dialect">
     org.hibernate.dialect.MySQLDialect
    </prop>
   </props>
  </property>
  <property name="mappingResources">
   <list>
    <value>org/hibernate3_2/entity/TUser.hbm.xml</value>
   </list>
  </property>
 </bean>
 <bean id="TUserDAO" class="org.hibernate3_2.entity.TUserDAO">
  <property name="sessionFactory">
   <ref bean="sessionFactory" />
  </property>
 </bean>

5、配置bean參數,使用spring來管理action

applicationContext.xml--

 <bean id="sshAction" class="org.struts2.action.MysshAction">
  <property name="userDAO">
   <ref bean="TUserDAO" />
  </property>
 </bean>

struts.xml--

  <action name="ssh" class="sshAction">
   <result name="success">/WEB-INF/test/ssh.jsp</result>
  </action>

6、測試,完成。源碼見於:http://download.csdn.net/detail/phoenixdsf/4361091

 

四、ext整合部分

1、在工程文件夾裏面加入EXTJS下載包提供的adapter文件夾和resources文件夾,另外在加入ext-all.js和ext-lang-zh_CN.js(支持中文用)

 

2、在頁面開頭部分加入

<%
String path = request.getContextPath();
%>
<link rel="stylesheet" type="text/css" href="<%=path %>/extjs/resources/css/ext-all.css" />

<script type="text/javascript" src="<%=path %>/extjs/adapter/ext/ext-base.js"></script>
<script type="text/javascript" src="<%=path %>/extjs/ext-all.js"></script>
<script type="text/javascript" src="<%=path %>/extjs/ext-lang-zh_CN.js"></script>

 

3、在使用EXTJS的頁面裏面引入<jsp:include page="../include/ext_header.jsp" flush="true" />以後即可使用EXTJS,最終代碼見於:http://download.csdn.net/detail/phoenixdsf/4381994

 

附錄:MYSQL表結構

id INTEGER,

name VARCHAR(45),

pwd VARCHAR(45)

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章