SSH整合

        这几天一直整合ssh,过程是艰辛的,报了很多的bug而且无从处理,但是一直坚持下来了,最终也整合完成,总结起来,当我们遇到困难的时候如果解决不了,请一定要冷静而且可以暂且把问题放一放,可以玩玩游戏,看看电影什么的,但是请不要放弃解决困难的勇气,时间会见证一切。好了,话不多说,直接进入ssh的整合。

        

第一步:先整合Spring 、Struts2

                   拷贝必要的jar包

                   Struts2必要的jar包有13个

                    

                    接下来添加log4j的配置文件,配置web.xml文件和创建struts.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">

   <display-name>Struts Blank</display-name>

   <!-- web集成spring -->

   <context-param>

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

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

   </context-param>

   <!-- 配置监听器 -->

   <listener>

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

   </listener>

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

   <welcome-file-list>

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

   </welcome-file-list>

</web-app>

                    

                       配置Struts2

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

  <!DOCTYPE struts PUBLIC

   "-//Apache Software Foundation//DTD StrutsConfiguration 2.3//EN"

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

   <struts>

   <constant name="struts.devMode" value="true"></constant>

   <package name="p" extends="struts-default">

   <action name="hello" class="actionSupport">

   <result>/index.html</result>

   </action>

   </package>

   </struts>

                       

                        导入springjar和整合jar

                       

                       

                       SS的整合就成功了

                       ss基础之上整合hibernate

                       导入jar

                         

                        还有sh的整合包Spring-orm.jar

                       编写映射类 Account \ Account.hbm.xml \ hibernate.cfg.xml文件并且建立相应的数据库

       Account类:

  package it.cast.domain;

  publicclass Account {

   privateintid;

   private String name;

   privateintmoney;

   publicint getId() {

      returnid;

   }

   publicvoid setId(intid) {

      this.id = id;

   }

   public String getName() {

      returnname;

   }

   publicvoid setName(String name) {

      this.name = name;

   }

   publicint getMoney() {

      returnmoney;

   }

   publicvoid setMoney(intmoney) {

      this.money = money;

   }

   @Override

   public String toString() {

      return"Account[id=" + id + ",name=" + name + ",money=" + money + "]";

   }

}

                        Account.cfg.xml 映射文件:


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

  <!DOCTYPE hibernate-mapping PUBLIC

    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"

    "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">

 <hibernate-mapping>

 <class name="it.cast.domain.Account"table="ACCOUNT">

 <id name="id"column="ID">

 <generator class="native"></generator>

 </id>

 <property name="name"column="NAME"></property>

 <property name="money"column="MONEY"></property>

 </class>

 </hibernate-mapping>

                        接下来配置application.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"

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

      xsi:schemaLocation="

        http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsd

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

        http://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop.xsd

        http://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsd">

      <!-- 配置hibernate链接 -->

      <bean id="sessionFactory"class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">

      <!-- 加载配置文件 -->

      <property name="configLocation"value="classpath:hibernate.cfg.xml"></property>

      </bean>

      <!-- 配置dao -->

      <bean id="accountDao"class="it.cast.daoimpl.AccountDaoImpl">

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

      </bean>

      <!-- 配置service -->

      <bean id="accountService"class="it.cast.serviceimpl.AccountServiceImpl">

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

      </bean>

      <!--  配置动作类 ,springStruts集成必须设置为多例的模式-->

      <bean id="action"class="it.cast.action.ActionDemo" scope="prototype">

      <property name="as"ref="accountService"></property>

      </bean>

</beans>

                        Dao实现类建立数据库的对象最主要的是导hibernateDaoSupport

packageit.cast.daoimpl;

 importjava.util.List;

 importorg.springframework.orm.hibernate3.support.HibernateDaoSupport;

 importit.cast.dao.AccountDao;

importit.cast.domain.Account;

 publicclass AccountDaoImpl extends HibernateDaoSupport implements AccountDao {

 

   @Override

   public void save(Account account) {

      super.getHibernateTemplate().save(account);

   }

 

   @Override

   public void update(Account account) {

      super.getHibernateTemplate().update(account);

   }

 

   @Override

   public void delete(Integer id) {

      Account account=super.getHibernateTemplate().get(Account.class, id);

      super.getHibernateTemplate().delete(account);

   }

 

   @Override

   public Account getOne(Integer id) {

      return super.getHibernateTemplate().get(Account.class,id);

   }

 

   @Override

   public List<Account> findAll() {

      return super.getHibernateTemplate().find("from Account");

   }

 }

                        

                        Action的配置:

packageit.cast.action;

 importjava.util.List;

 import javax.ejb.SessionContext;

 importcom.opensymphony.xwork2.ActionContext;

 importcom.opensymphony.xwork2.ActionSupport;

 importit.cast.domain.Account;

 importit.cast.service.AccountServiceDao;

 publicclass ActionDemo extends ActionSupport {

   private AccountServiceDao as;

 

   public AccountServiceDao getAs() {

      return as;

   }

 

   public void setAs(AccountServiceDao as) {

      this.as = as;

   }

   public String getAll(){

      List<Account> list= as.findAll();

      ActionContext.getContext().put("list",list);

      return "success";

   }

}

        以上并不是所有的代码list.jsp ,index.html 等等都是可以自己配置的,当我们访问时,先进入index.html文件,index.html转发访问ssh/hello.action 根据struts.xml的配置进入action,由于action有Service控制,acclicationContext.xml DI注入AOP控制反转,Service根据Dao的实现类进行控制事务,然后追溯到DAO层,获取sessionFactory,注入dataSource 获取数据库对象,然后返回到action并且把list集合加入ContextMap,在list.jsp中用传统的core标签遍历,完成ssh整合


 




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值