posts - 70,comments - 408,trackbacks - 0
          <2007年8月>
          2930311234
          567891011
          12131415161718
          19202122232425
          2627282930311
          2345678

          新浪微博http://weibo.com/javamail
          Java in Action QQ群:9028409

          常用鏈接

          留言簿(19)

          隨筆檔案(71)

          文章檔案(9)

          積分與排名

          • 積分 - 962425
          • 排名 - 36

          最新評(píng)論

          閱讀排行榜

          首選創(chuàng)建web.xml 主要是配置Struts的ActionServlet和Spring的字符過(guò)濾器

          <?xml version="1.0" encoding="UTF-8"?>
          <web-app xmlns="然后創(chuàng)建struts.xml(String配置文件) 要注意這里集成了Spring插件,把全部Spring配置文件注入到ContextLoaderPlugIn中

          <?xml version="1.0" encoding="UTF-8"?>
          <!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN" "
          <struts-config>

            <form-beans>
           <form-bean name="loginVO" type="com.vo.LoginVO" />
            </form-beans>

            <global-forwards>
             <forward name="error" path="/error.jsp"/>
            </global-forwards>

            <action-mappings>
             <action path="/login"
               name="loginVO"
                type="org.springframework.web.struts.DelegatingActionProxy"
                parameter="action"
                scope="request">
                <forward name="login" path="/login.jsp"/>
                <forward name="index" path="/index.jsp"/>
              </action>
            </action-mappings>

            <!-- 集成Spring插件 -->
            <plug-in className="org.springframework.web.struts.ContextLoaderPlugIn">
              <set-property property="contextConfigLocation" value="/WEB-INF/spring/*.xml" />
            </plug-in>

          </struts-config>


          配置Spring配置文件

          <?xml version="1.0" encoding="UTF-8"?>
          <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "
          <beans>
           <!-- 讀入屬性文件 -->
           <bean id="propertyConfig" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
            <property name="locations">
             <list>
              <value>classpath:hibernate.properties</value>
             </list>
            </property>
           </bean>

           <!-- 配置數(shù)據(jù)源,可以其他方式 -->
           <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
               <property name="driverClassName" value="${hibernate.driverClassName}" />
            <property name="url" value="${hibernate.url}" />
            <property name="username" value="${hibernate.username}" />
            <property name="password" value="${hibernate.password}" />
               <property name="maxActive" value="${hibernate.maxActive}" />
            <property name="maxIdle" value="${hibernate.maxIdle}" />
            <property name="maxWait" value="${hibernate.maxWait}" />
              </bean>

           <!-- 配置Hibernate的Session工廠,注入數(shù)據(jù)源、映射文件 -->
              <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
               <property name="dataSource">
                  <ref local="dataSource"/>
               </property>
                  <property name="mappingResources">
                      <list>
                          <value>com/po/login.hbm.xml</value>
                      </list>
                  </property>
                  <property name="hibernateProperties">
                 <props>
                   <prop key="hibernate.dialect">${hibernate.dialect}</prop>
                   <prop key="hibernate.show_sql">${hibernate.showSQL}</prop>
                 </props>
               </property>
              </bean>

           <!-- 聲明Hibernate事務(wù)管理,注入Session工廠 -->
              <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
               <property name="sessionFactory">
                 <ref local="sessionFactory"/>
               </property>
             </bean>

           <!-- 配置事務(wù)代理,注入事務(wù)管理transactionManager,由Spring來(lái)代理事務(wù),設(shè)置事務(wù)屬性 -->
              <bean id="transactionProxy" abstract="true" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
               <property name="transactionManager">
                 <ref bean="transactionManager"/>
               </property>
               <property name="transactionAttributes">
                 <props>
                     <prop key="save*">PROPAGATION_REQUIRED,-Exception</prop>
                         <prop key="add*">PROPAGATION_REQUIRED,-Exception</prop>
                         <prop key="remove*">PROPAGATION_REQUIRED,-Exception</prop>
                         <prop key="delete*">PROPAGATION_REQUIRED,-Exception</prop>
                         <prop key="update*">PROPAGATION_REQUIRED,-Exception</prop>
                         <prop key="create*">PROPAGATION_REQUIRED,-Exception</prop>
                         <prop key="query*">PROPAGATION_REQUIRED,readOnly</prop>
                         <prop key="find*">PROPAGATION_REQUIRED,readOnly</prop>
                         <prop key="get*">PROPAGATION_REQUIRED,readOnly</prop>
                         <prop key="load*">PROPAGATION_REQUIRED,readOnly</prop>
                         <prop key="*">PROPAGATION_REQUIRED</prop>
                 </props>
               </property>
              </bean>
          </beans>


          配置Action將Service注入到Action

          <?xml version="1.0" encoding="UTF-8"?>
          <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "
          <beans>
           <!-- 配置Action,singleton屬性設(shè)置為false表示不使用單例,每次都重新創(chuàng)建實(shí)例,避免并發(fā)問(wèn)題,注入事務(wù)管理的Service -->
            <bean name="/login" class="com.action.LoginAction" singleton="false">
             <property name="loginService">
                   <ref bean="loginService"/>
                  </property>
            </bean>
          </beans>

          配置Service將Dao注入到Service

          <?xml version="1.0" encoding="UTF-8"?>
          <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "
          <beans>
           <!-- 配置事務(wù)代理Service,先將Dao注入到Service,再將Service注入給事務(wù)代理 -->
              <bean id="loginService" parent="transactionProxy">
               <property name="target">
                <ref bean="loginTempService"/>
               </property>
             </bean>
             <bean id="loginTempService" class="com.service.LoginService">
                  <property name="loginDao">
                   <ref bean="loginDao"/>
                  </property>
              </bean>
          </beans>

          配置Dao 注入Session工廠

          <?xml version="1.0" encoding="UTF-8"?>
          <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "
          <beans>
           <!-- 配置Dao,注入Session工廠 -->
              <bean id="loginDao" class="com.dao.LoginDao">
                  <property name="sessionFactory">
                   <ref bean="sessionFactory"/>
                  </property>
              </bean>
          </beans>

          數(shù)據(jù)源屬性文件(注意不是Hibernate的配置文件,是為了讓Spring讀入的)

          hibernate.dialect=org.hibernate.dialect.SQLServerDialect
          hibernate.driverClassName=com.mysql.jdbc.Driver
          hibernate.url=jdbc:mysql://127.0.0.1:3306/ssh
          hibernate.username=root
          hibernate.password=5719
          hibernate.showSQL=true
          hibernate.maxActive=50
          hibernate.maxIdle=30
          hibernate.maxWait=1000

          log4j配置文件(簡(jiǎn)單)

          log4j.rootLogger=ERROR,console,file

          log4j.appender.console=org.apache.log4j.ConsoleAppender
          log4j.appender.console.layout=org.apache.log4j.PatternLayout
          log4j.appender.console.layout.ConversionPattern=%-5p %d [%F,%L] - %m%n

          log4j.appender.file=org.apache.log4j.RollingFileAppender
          log4j.appender.file.File=F:\\SSH.log
          #log4j.appender.file.MaxFileSize=100000KB
          #log4j.appender.file.MaxBackupIndex=1
          log4j.appender.file.layout=org.apache.log4j.PatternLayout
          log4j.appender.file.layout.ConversionPattern=%-5p %d [%F,%L] - %m%n

          下面是類(lèi)文件

          package com.action;

          import javax.servlet.http.HttpServletRequest;
          import javax.servlet.http.HttpServletResponse;

          import org.apache.commons.logging.Log;
          import org.apache.commons.logging.LogFactory;
          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 com.service.LoginService;
          import com.vo.LoginVO;

          public class LoginAction extends DispatchAction {

           private Log logger = LogFactory.getLog(LoginAction.class);
           private LoginService loginService;

           public void setLoginService(LoginService loginService) {
            this.loginService = loginService;
           }

           public ActionForward login(ActionMapping mapping,ActionForm form,HttpServletRequest request,HttpServletResponse response) {
            try {
             LoginVO loginVO = (LoginVO) form;
             String username = loginVO.getUsername();
             String password = loginVO.getPassword();
             System.out.println(username+password);
             if(loginService.validate(username, password)) {
              return mapping.findForward("index");
             }
             return mapping.findForward("error");
            } catch (Exception e) {
             logger.error(e);
             return mapping.findForward("error");
            }
           }

           public ActionForward save(ActionMapping mapping,ActionForm form,HttpServletRequest request,HttpServletResponse response) {
            try {
             LoginVO loginVO = (LoginVO) form;
             loginService.saveUser(loginVO);
             return mapping.findForward("index");
            } catch (Exception e) {
             logger.error(e);
             return mapping.findForward("error");
            }
           }
          }



          package com.dao;

          import java.util.List;

          import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

          import com.po.LoginPO;

          public class LoginDao extends HibernateDaoSupport {

           @SuppressWarnings("unchecked")
           public String getPassword(String username) {
            String hql = "from LoginPO l where l.username=?";
            List list = getSession().createQuery(hql).setString(0,username).list();
            if(list!=null && list.size()>0) {
             LoginPO loginPO = (LoginPO) list.get(0);
             return loginPO.getPassword();
            }
            return null;
           }

           public void save(LoginPO loginPO) {
            getSession().save(loginPO);
           }
          }



          package com.po;

          import java.io.Serializable;

          public class LoginPO implements Serializable {

           private static final long serialVersionUID = 1L;

           private Integer id = null;
           private String username = null;
           private String password = null;

           public Integer getId() {
            return id;
           }
           public String getPassword() {
            return password;
           }
           public String getUsername() {
            return username;
           }
           public void setId(Integer id) {
            this.id = id;
           }
           public void setPassword(String password) {
            this.password = password;
           }
           public void setUsername(String username) {
            this.username = username;
           }
          }



          package com.service;

          import com.dao.LoginDao;
          import com.po.LoginPO;
          import com.vo.LoginVO;

          public class LoginService {

           private LoginDao loginDao;

           public void setLoginDao(LoginDao loginDao) {
            this.loginDao = loginDao;
           }

           public boolean validate(String username,String password) {
            String pass = loginDao.getPassword(username);
            if(pass!=null) {
             if(pass.equals(password)) {
              return true;
             }
            }
            return false;
           }

           public void saveUser(LoginVO loginVO) {
            LoginPO loginPO = new LoginPO();
            loginPO.setUsername(loginVO.getUsername());
            loginPO.setPassword(loginVO.getPassword());
            System.out.println(loginVO.getUsername()+"-"+loginVO.getPassword()+":save succeed...");
            loginDao.save(loginPO);
            //故意制造異常,測(cè)試事務(wù)。
            //loginDao.save(null);
           }
          }



          package com.vo;

          import org.apache.struts.action.ActionForm;

          public class LoginVO extends ActionForm {

           private static final long serialVersionUID = 1L;
           
           private String username = null;
           
           private String password = null;
           
           public String getPassword() {
            return password;
           }
           public String getUsername() {
            return username;
           }
           public void setPassword(String password) {
            this.password = password;
           }
           public void setUsername(String username) {
            this.username = username;
           }
          }

          Hibernate映射文件

          <?xml version="1.0"?>
          <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
          "

          <hibernate-mapping>
              <class name="com.po.LoginPO" table="login">
                  <comment></comment>
                 
                  <id name="id" type="int">
             <column name="id" />
             <generator class="native" />
            </id>
                 
                  <property name="username" type="string">
                      <column name="username" not-null="true">
                          <comment></comment>
                      </column>
                  </property>
                 
                  <property name="password" type="string">
                      <column name="password" not-null="true">
                          <comment></comment>
                      </column>
                  </property>
              </class>
          </hibernate-mapping>

          jsp頁(yè)面文件

          <%@ page contentType="text/html;charset=UTF-8" language="java" %>
          <html>
          <body>
            <form name="form" action="login.do?action=login" method="post">
             <h1>Login</h1>
             <h4>username:</h4><input name="username" type="text">&nbsp;&nbsp;&nbsp;<span id="message"></span>
             <h4>password:</h4><input name="password" type="password">
             <br>
             <br>
             <input value="提交" type="button" onclick="form.submit();">
            </form>
            <br>
            <br>
            <br>
            <form name="form" action="login.do?action=save" method="post">
             <h1>Save</h1>
             <h4>username:</h4><input name="username" type="text">&nbsp;&nbsp;&nbsp;<span id="message"></span>
             <h4>password:</h4><input name="password" type="password">
             <br>
             <br>
             <input value="提交" type="button" onclick="form.submit();">
            </form>
          </body>
          </html>

          jar包太多太大了這里就不發(fā)了.具體什么意思都有注視,完全實(shí)例,理論知識(shí)以后在說(shuō),歡迎拍磚嘎嘎...


          FeedBack:
          # re: 原創(chuàng) Struts Spring Hibernate (SSH) 整合實(shí)例
          2007-08-29 14:11 | zp
          請(qǐng)問(wèn)你是不是姓張?  回復(fù)  更多評(píng)論
            
          # re: 原創(chuàng) Struts Spring Hibernate (SSH) 整合實(shí)例
          2007-08-30 12:49 | JAVA面試題
          文章好長(zhǎng),看的好累  回復(fù)  更多評(píng)論
            
          # re: 原創(chuàng) Struts Spring Hibernate (SSH) 整合實(shí)例
          2007-09-01 12:54 | sea7
          沒(méi)什么值得看的東西啊,失望…………  回復(fù)  更多評(píng)論
            
          # re: 原創(chuàng) Struts Spring Hibernate (SSH) 整合實(shí)例
          2007-10-14 17:05 | 路人甲
          然后創(chuàng)建struts.xml(String配置文件)
          這一句中那個(gè)string應(yīng)該是struts,樓主寫(xiě)錯(cuò)了哦,容易引起誤解的。  回復(fù)  更多評(píng)論
            
          # re: 原創(chuàng) Struts Spring Hibernate (SSH) 整合實(shí)例
          2008-03-13 09:29 | 路人甲
          改造struts控制器,不需要對(duì)action進(jìn)行service的注入  回復(fù)  更多評(píng)論
            
          # re: 原創(chuàng) Struts Spring Hibernate (SSH) 整合實(shí)例
          2008-04-14 14:14 | as
          就是一堆代碼 缺少?zèng)]有描述性語(yǔ)句  回復(fù)  更多評(píng)論
            
          # re: 原創(chuàng) Struts Spring Hibernate (SSH) 整合實(shí)例
          2008-05-28 19:59 | 魂?duì)繅?mèng)縈
          ry搞點(diǎn)注釋撒,剛開(kāi)始學(xué),那么多東西不注釋怎么披露了  回復(fù)  更多評(píng)論
            
          # re: 原創(chuàng) Struts Spring Hibernate (SSH) 整合實(shí)例
          2008-06-04 10:48 | ziptheworld
          ssh居然能這么理解  回復(fù)  更多評(píng)論
            
          # re: 原創(chuàng) Struts Spring Hibernate (SSH) 整合實(shí)例[未登錄](méi)
          2008-06-25 00:03 | Jacken
          我以前也寫(xiě)過(guò)一個(gè)pdf講解ssh整合的!
          呵呵 覺(jué)得挺不錯(cuò)
          大家可以在這里看看:
          http://www.jacken.com.cn/struts-spring-hibernate-_-integration.yy/  回復(fù)  更多評(píng)論
            
          # re: 原創(chuàng) Struts Spring Hibernate (SSH) 整合實(shí)例[未登錄](méi)
          2008-07-15 11:43 | egg
          確實(shí),一點(diǎn)注釋都沒(méi)有  回復(fù)  更多評(píng)論
            
          # re: 原創(chuàng) Struts Spring Hibernate (SSH) 整合實(shí)例
          2008-07-18 09:05 | pengpeng
          挺喜歡。簡(jiǎn)單  回復(fù)  更多評(píng)論
            
          # re: 原創(chuàng) Struts Spring Hibernate (SSH) 整合實(shí)例
          2008-09-03 22:10 | 往往
          亂啊,你要把幾個(gè)文件完整貼出來(lái),比你這樣一段一段加進(jìn)去,明了多了。你這樣注釋不加,貼上來(lái)干么?會(huì)的人不用看你的,不會(huì)的能看明么


            回復(fù)  更多評(píng)論
            
          # re: 原創(chuàng) Struts Spring Hibernate (SSH) 整合實(shí)例
          2008-10-24 19:16 | 舞命小丟
          不錯(cuò)  回復(fù)  更多評(píng)論
            
          # re: 原創(chuàng) Struts Spring Hibernate (SSH) 整合實(shí)例[未登錄](méi)
          2009-03-09 20:47 | DD
          支持一個(gè)  回復(fù)  更多評(píng)論
            
          # re: 原創(chuàng) Struts Spring Hibernate (SSH) 整合實(shí)例[未登錄](méi)
          2009-03-27 10:48 | ssh
          是呀,看你這個(gè)太累了。
          樓主還是修正一下!  回復(fù)  更多評(píng)論
            
          # re: 原創(chuàng) Struts Spring Hibernate (SSH) 整合實(shí)例
          2009-04-09 16:32 | 發(fā)達(dá)
          @Jacken
          都打不開(kāi)你的網(wǎng)頁(yè)  回復(fù)  更多評(píng)論
            
          # re: 原創(chuàng) Struts Spring Hibernate (SSH) 整合實(shí)例[未登錄](méi)
          2009-05-09 19:08 | SSH
          # re: 原創(chuàng) Struts Spring Hibernate (SSH) 整合實(shí)例[未登錄](méi)
          2009-10-20 11:24 | 某某某
          如果部署SSH用這樣的代碼方式,是不是效果上有點(diǎn)說(shuō)不去啊,建議樓主用開(kāi)發(fā)平臺(tái)來(lái)說(shuō)明實(shí)現(xiàn)過(guò)程會(huì)比較受歡迎!  回復(fù)  更多評(píng)論
            
          # re: 原創(chuàng) Struts Spring Hibernate (SSH) 整合實(shí)例
          2009-11-01 22:09 | yinlei
          謝謝 感謝!!!  回復(fù)  更多評(píng)論
            
          # re: 原創(chuàng) Struts Spring Hibernate (SSH) 整合實(shí)例
          2010-11-12 16:57 | 11
          拿來(lái)復(fù)習(xí)還是不錯(cuò)的
          感謝分享  回復(fù)  更多評(píng)論
            
          # re: 原創(chuàng) Struts Spring Hibernate (SSH) 整合實(shí)例
          2011-05-30 11:37 | 流星
          缺少注視,費(fèi)解  回復(fù)  更多評(píng)論
            

          只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 商洛市| 德安县| 桐城市| 大新县| 宜君县| 兴国县| 台北市| 柞水县| 常德市| 山阳县| 晋州市| 分宜县| 老河口市| 吐鲁番市| 丘北县| 义乌市| 荣昌县| 基隆市| 阿克苏市| 高阳县| 北票市| 汾西县| 锡林浩特市| 东乌珠穆沁旗| 黄梅县| 平邑县| 静乐县| 刚察县| 比如县| 茶陵县| 海伦市| 小金县| 安溪县| 房山区| 中牟县| 五河县| 那曲县| 白山市| 闽清县| 中牟县| 筠连县|