gcw633

          s2sh整合(二)

           

          s2sh整合總結(struts2.1.8-spring2.5-hibernate3.2

          1. 創建工程


          2. 建好包架構
           

          配置Struts2.0

          3. 加入struts2
          Commons-logging-1.0.4.jarFreemarker-2.3.13.jarOgnl-2.6.11.jarStruts2-core-2.1.6.jarXwork-2.1.2.jar,另外sqljdbc.jarcommons-fileupload-1.2.1.jar

          4. 修改WEB-INF下的web.xml文件,增加struts2的配置
              <!-- struts2過濾器 -->

              <filter>

                  <filter-name>struts2</filter-name>

                  <filter-class>

          org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter

                  </filter-class>

              </filter>

              <filter-mapping>

                  <filter-name>struts2</filter-name>

                  <url-pattern>/*</url-pattern>

          </filter-mapping>

          5.添加struts配置文件。WEB-INF/classes目錄下,新建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>

          6,測試看是否配置成功

          7.配置一個Action

                首先新建一個登陸頁面login.jsp,代碼如下:

          <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>

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

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

           </head>

           <body>

             <s:form name="form1" action="login" >

                 <s:textfield name="username" label="username" ></s:textfield>

                 <s:password name="password" label="password" ></s:password>

                 <s:submit label="submit"></s:submit>

             </s:form>

             <s:actionerror/>

           </body>

          </html>

          8.在我們已經建好的struts.xml中來配置登錄的action。這里定義登錄action的名字為login,配置代碼如下:

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

          <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">

          <struts>

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

                 <action name="login" class="com.test.actions.LoginAction">

                     <result name="success" type="redirect">/index.jsp</result>

                     <result name="input">/login.jsp</result>

                     <result name="error">/login.jsp</result>

                 </action>

              </package>

          </struts>

          9.下面就來編寫具體的action類了。代碼如下:

          package com.test.actions;

          import com.opensymphony.xwork2.ActionSupport;

          publicclass LoginAction extends ActionSupport {

              public String username;

              public String password;

             

              public String excute(){

                 if(!username.equals("admin")){

                     super.addFieldError("username", "用戶名錯誤!");

                     returnERROR;

                 }

                 if(!password.endsWith("sa")){

                     super.addFieldError("password","密碼錯誤!");

                     returnERROR;

                 }

                 returnSUCCESS;

              }

              //驗證

              publicvoid validate(){

                 if(username==null || username.length()==0){

                     super.addActionError("請輸入用戶名!");

                     }

                 if(password==null || password.length()==0){

                     super.addActionError("請輸入密碼!");

                 }

              }

          }

          10.測試,看效果(成功)

          配置Hibernate

          11.導入hibernate包:

                 hibernate3.jar-----------------------------核心類庫
                  
          antlr-2.7.6.jar-----------------------------代碼掃描器,用來翻譯HQL語句
                  
          commons-collections-3.1.jar----------- Apache Commons包中的一個,包含了一些Apache開發的集合類,功能比java.util.*強大
                  
          dom4j-1.6.1.jar----------------------------是一個JavaXML API,類似于jdom,用來讀寫XML文件的
                  
          javassist-3.4.GA.jar----------------------- Javassist 字節碼解釋器
                  
          jta-1.1.jar------------------------------------標準的JTA API
                slf4j-api-1.5.2.jar
                slf4j-nop-1.5.2.jar

          12.創建Hibernate配置文件。在WEB-INF"calsses目錄下(工程的src包下)新建hibernate.cfg.xml。這是hibernate連接數據庫的配置文件:

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

          <!DOCTYPE hibernate-configuration PUBLIC

                    "-//Hibernate/Hibernate Configuration DTD 3.0//EN"

                    "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

          <!-- Generated by MyEclipse Hibernate Tools.                   -->

          <hibernate-configuration>

              <session-factory>

                 <property name="connection.username">sa</property>

                 <property name="connection.url">jdbc:sqlserver://localhost:1433;databaseName=demo</property>

                 <property name="dialect">

                     org.hibernate.dialect.SQLServerDialect

                 </property>

                 <property name="myeclipse.connection.profile">s2shcon2</property>

                 <property name="connection.password">sa</property>

                 <property name="connection.driver_class">com.microsoft.sqlserver.jdbc.SQLServerDriver</property>

                 <property name="dialect">org.hibernate.dialect.SQLServerDialect</property>

                 <property name="show_sql">true</property>

                 <mapping resource="com/test/entity/Users.hbm.xml" />

              </session-factory>

          </hibernate-configuration>

          配置Spring2.5

          14.導入包:

          spring.jar,struts2-spring-plugin-2.1.8.1.jar (spring struts 整合映射用) ,cglib-2.1.3.jar, asm.jar

          15.配置web.xml文件。Jar包引入完成后,就開始配置spring了,首先修改web.xml文件,增加如下代碼:

                 <!-- 配置spring的監聽器 -->

              <listener>

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

              </listener>

              <context-param>

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

                 <param-value>classpath*:applicationContext-*.xml</param-value>

              </context-param>

          16.src下面新建applicationContext-common.xml文件。首先給這個文件加上spring的標頭

          <?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:p="http://www.springframework.org/schema/p"

              xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">

          </beans>

          17.2個類測試spring

                package test.spring;

          publicclass TUser implements java.io.Serializable {

              private String username;

              private String allname;

              private String address;

              public String getUsername() {

                 returnthis.username;

              }

              publicvoid setUsername(String username) {

                 this.username = username;

              }

              public String getAllname() {

                 returnthis.allname;

              }

              publicvoid setAllname(String allname) {

                 this.allname = allname;

              }

              public String getAddress() {

                 returnthis.address;

              }

              publicvoid setAddress(String address) {

                 this.address = address;

              }

          }

          package test.spring;

          import org.springframework.context.ApplicationContext;

          import org.springframework.context.support.ClassPathXmlApplicationContext;

          publicclass SpringTest {

              publicstaticvoid main(String[] args) {

                 // 加載spring配置文件,初始化IoC容器

                 ApplicationContext ac = new ClassPathXmlApplicationContext(

                        "applicationContext-common.xml");

                 // 從容器接管Bean

                 TUser user = (TUser) ac.getBean("TUser");

                 // 輸出歡迎信息

                 System.out.println("Hello:" + user.getUsername() + ";u is in "

                        + user.getAddress() + " ; and u is " + user.getAllname());

              }

          }

          18.創建完畢后,就剩最后一步了,在applicationContext.xml中配置一個bean,在xml中增加如下代碼:

              <bean id="TUser" class="test.spring.TUser">

                 <property name="username" value="小張"></property>

                 <property name="allname" value="張三"></property>

                 <property name="address" value="青島市"></property>

              </bean>

          19.測試結果:

          整合Struts
          20.web.xml中添加:

              <!-- 配置spring的監聽器 -->

              <listener>

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

              </listener>

              <context-param>

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

                 <param-value>classpath*:applicationContext-*.xml</param-value>

              </context-param>

          21.現在就來看如何把strutsaction交給spring。以struts示例中的login.action為例,首先創建一個LoginAction類的Bean。在applicationContext-common.xml中增加如下代碼:

              <bean id="loginAction" class="com.test.actions.LoginAction"

                 scope="prototype">

              </bean>

          22.接下來修改struts.xml文件,把原來login.action的配置做如下修改:

          原來:<action name="login" class="com.test.actions.LoginAction" >

          修改為:<action name="login" class="loginAction">

          class值設為了loginAction,即LoginAction類的beanID。這樣我們就把LoginAction類交給了spring管理。至于具體是怎么處理的,秘密在struts2-spring-plugin-2.1.6.jar中,

          整合Hibernate

          22.配置sessionFactoryspring來創建Session。在applicationContext-common.xml中增加如下代碼:

              <!-- 配置SessionFactory -->

              <bean id="sessionFactory"

                 class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">

                 <property name="configLocation"

                     value="classpath:hibernate.cfg.xml">

                 </property>

              </bean>

              <!-- 配置事務管理器 -->

              <bean id="transactionManager"

                 class="org.springframework.orm.hibernate3.HibernateTransactionManager">

                 <property name="sessionFactory" ref="sessionFactory" />

              </bean>

              <!-- 配置事務管理 -->

              <bean id="transactionBase"

              class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"

                 lazy-init="true" abstract="true">

                 <!-- 配置事務管理器 -->

                 <property name="transactionManager" ref="transactionManager" />

                 <!-- 配置事務屬性 -->

                 <property name="transactionAttributes">

                     <props>

                        <prop key="*">PROPAGATION_REQUIRED</prop>

                     </props>

                 </property>

              </bean>

          23.web.xml添加過濾

          <!-- hibernate過濾器 -->

              <filter>

                 <filter-name>hibernateFilter</filter-name>

                 <filter-class>

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

              </filter>

              <filter-mapping>

                 <filter-name>hibernateFilter</filter-name>

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

              </filter-mapping>

          將數據訪問層,邏輯層,action層的配置分開,applicationContext-common.xml只放公共配置

          最后配置文件:

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

             

              <!-- 配置spring的監聽器 -->

              <listener>

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

              </listener>

              <context-param>

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

                 <param-value>classpath*:applicationContext-*.xml</param-value>

              </context-param>

              <!-- hibernate過濾器 -->

              <filter>

                 <filter-name>hibernateFilter</filter-name>

                 <filter-class>

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

              </filter>

              <filter-mapping>

                 <filter-name>hibernateFilter</filter-name>

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

              </filter-mapping>

             

              <!-- struts2過濾器 -->

              <display-name>Struts 2 Fileupload</display-name>

              <filter >

                  <filter-name>struts-cleanup</filter-name>

                  <filter-class>

                      org.apache.struts2.dispatcher.ActionContextCleanUp

                  </filter-class>

              </filter >

             

              <filter>

                 <filter-name>struts2</filter-name>

                 <filter-class>

                     org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter

                 </filter-class>

              </filter>

             

              <filter-mapping >

                  <filter-name>struts-cleanup</filter-name>

                  <url-pattern >/*</url-pattern >

              </filter-mapping >

              <filter-mapping>

                 <filter-name>struts2</filter-name>

                 <url-pattern>/*</url-pattern>

              </filter-mapping>

              <!-- 編碼過濾器 -->

              <filter>

                 <filter-name>encodingFilter</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>encodingFilter</filter-name>

                 <url-pattern>/*</url-pattern>

              </filter-mapping>

              <welcome-file-list>

                 <welcome-file>index.jsp</welcome-file>

              </welcome-file-list>

          </web-app>

          Struts.xml為:

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

          <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">

          <struts>

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

                 <action name="login" class="loginAction">

                     <result name="success">/main.jsp</result>

                     <result name="input">/login.jsp</result>

                     <result name="error">/login.jsp</result>

                 </action>

                

                 <action name="comReport" class="comReportAction">

                     <result name="success">/index.jsp</result>

                     <result name="input">/investreport.jsp</result>

                 </action>

                

                  <action name ="fileUpload" class ="com.gt.test.FileUploadAction" >

                      <interceptor-ref name ="fileUploadStack" />

                      <result name ="success" >/test/FileUpload.jsp </result >

                  </action >

              </package>

          </struts>   

          Hibernate.cfg.xml:

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

          <!DOCTYPE hibernate-configuration PUBLIC

                    "-//Hibernate/Hibernate Configuration DTD 3.0//EN"

                    "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

          <!-- Generated by MyEclipse Hibernate Tools.                   -->

          <hibernate-configuration>

              <session-factory>

                 <property name="connection.username">IT03</property>

                 <property name="connection.url">

                     jdbc:sqlserver://localhost;databaseName=demo

                 </property>

                 <property name="dialect">

                     org.hibernate.dialect.SQLServerDialect

                 </property>

                 <property name="myeclipse.connection.profile">s2shcon</property>

                 <property name="connection.password">sa</property>

                 <property name="connection.driver_class">

                     com.microsoft.sqlserver.jdbc.SQLServerDriver

                 </property>

                 <property name="dialect">

                     org.hibernate.dialect.SQLServerDialect

                 </property>

                 <property name="show_sql">true</property>

              </session-factory>

          </hibernate-configuration>

          posted on 2010-04-19 14:41 淡淡的回憶 閱讀(979) 評論(0)  編輯  收藏


          只有注冊用戶登錄后才能發表評論。


          網站導航:
          博客園   IT新聞   Chat2DB   C++博客   博問  
           
          <2010年4月>
          28293031123
          45678910
          11121314151617
          18192021222324
          2526272829301
          2345678

          導航

          統計

          常用鏈接

          留言簿

          隨筆檔案

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 祁门县| 乌兰察布市| 宝丰县| 黔南| 承德市| 临夏市| 洪洞县| 保山市| 北京市| 阳城县| 徐闻县| 康定县| 云林县| 红桥区| 贡嘎县| 易门县| 洞口县| 武冈市| 曲阳县| 延川县| 邵阳县| 丰镇市| 察隅县| 湘阴县| 壶关县| 内乡县| 长海县| 通海县| 东乡| 固始县| 玛曲县| 分宜县| 台北市| 安远县| 城口县| 嘉义市| 博野县| 延庆县| 沧州市| 阳高县| 兰考县|