s2sh项目搭建及使用详解

来源:互联网 发布:怎么理解网络协议 编辑:程序博客网 时间:2024/05/16 13:51

  • 把整合中遇到的问题放在最前面:再见
1、jar包冲突:因为Spring2.5 AOP Liberaries里的asm2.2.3.jarHiberate中的生成代理用的asm.jar冲突,我们需要删除asm2.2.3.jar,不然就会发生异常:java.lang.NoClassDefFoundError: org/objectweb/asm/CodeVisitor 。具体的删除方法(避免在Tomcat中的lib下删除了,然后又重新发布项目时此jar又重新发不到lib下):在MyEclipse中【WindowàPreferencesàMyEclipse Enterprise WorkbenchàProject CapabilitiesàSpring】在spring2.5 AOP Libraries中删除asm2.2.3.jar

2、hibernate3.2生成映射文件报错:hibernate3.2生成oracle10.2映射文件时报如下错, An internal error occurred during: "Generating Artifacts". 问题原因,是因为Oracle的驱动程序不匹配,找最新版本的驱动程序,或者在本地安装的Oracle中找到对应的驱动包即可,比如我的是:F:\oracle\product\10.2.0\db_1\jdbc\lib\classes12.jar,换这个试下,binggo! 另外,注意看一下eclipse\workspace\.metadata.metadata\.log文件,可以根据错误提示解决问题。
  • 整合步骤:/haha
1、先引入spring:
(1) 整合spring步骤
  a) 要引入的spring的library :
 b)测试spring的ioc是否配置成功:
① 配置applicationContext.xml测试数据
<span style="white-space:pre"></span><span style="white-space:pre"></span><!-- 测试spring的IOC是否配置成功 --><bean id="testIOC" class="com.spring.test.TestService"><property name="name" value="gwm"></property></bean>
② 编写Java测试代码
<span style="white-space:pre"></span>获取ApplicationContext类
<span style="white-space:pre"></span>public class LoadBeansXML {<span style="white-space:pre"></span>private static ApplicationContext context;<span style="white-space:pre"></span>public static ApplicationContext getContext(){<span style="white-space:pre"></span>if(context == null){<span style="white-space:pre"></span>context = new ClassPathXmlApplicationContext("config/applicationContext.xml");<span style="white-space:pre"></span>return context;<span style="white-space:pre"></span>}<span style="white-space:pre"></span>return context;<span style="white-space:pre"></span>}<span style="white-space:pre"></span>}
<span style="white-space:pre"></span>测试类中的测试方法<span style="white-space:pre"></span>
<span style="white-space:pre"></span><span style="white-space:pre"></span>@Testpublic void test(){ApplicationContext cxt = LoadBeansXML.getContext();TestService ts = (TestService) cxt.getBean("testIOC");//测试spring的ioc是否配置成功System.out.println(ts.getName());//打印出gwm则成功}
2、spring与hibernate整合
(1)配置文件
a) 在添加Hibernate支持之前,我们先在【MyEclipse DataBase Explorer】中设置数据库连接,使用Oracle数据库,驱动程序为classes12.jar。
一路默认即可。
b)添加 Hibernate Capbilities
c)使用jdbc开发模式
d)不创建sessionFactory,使用applicationContext.xml方式配置sessionFactory和datasource,然后【finish】即可。
e)在applicationContext.xml为当前页面时,打开OutLine视图,右键new  Hibernate sessionFactory
可添加sessionFactory配置及相关属性
 (2)开发,根据数据表建立映射文件及相应的pojo
a)切换到MyEclipse DataBase Explorer】视图,找到前面新建的数据库连接,打开,在table中建立业务所需要的表,表上右键选择【Hibernate reverse Engineering

注:主键的生成方式可依据选择的数据库种类做出相应选择:Id Generator选择native,如果是Oracle可以选择increment
Java package要事先在项目中建立相应的包,使生成的entity实体和entity.hbm.xml存放在相应的包中。

--------------------------------------------------------------------------------

------------------------------------------------------------------------------------

注:此处有可能由于数据库驱动程序的问题,会出现问题,导致创建失败,请在前面找解决方案
b)上一步成功之后,就可以看到在com.spring.zd.pojo包中生成的实体类和相应的映射文件,以及在applicationContext.xml的sessionFactory中生成映射文件的映射属性路径
<span style="white-space:pre"></span><span style="white-space:pre"></span>
<span style="white-space:pre"></span><property name="mappingResources"><list><value>com/spring/zd/pojo/TbUserinfo.hbm.xml</value></list></property>
c)编写hibernate的dao层代码,操作数据库
接口Dao
public interface Dao {//保存单个Serializable saveEntity(Object entity);//更新单个void updateEntity(Object entity);//删除单个void deleteEntity(Object entity);//获取单个Object getEntity(Class entityClass, Serializable id);//获取单个 延迟加载Object loadEntity(Class entityClass, Serializable id);//根据条件查询List findEntityByCondition(String hql, Object[] paramArr);//查询所有List findAllEntity(String hql);
实现Dao接口的实现类,<span style="line-height: 15px; font-size: 13px; text-indent: 28px; font-family: 宋体;">同时要继承</span><span style="font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19px; text-indent: 28px;">HibernateDaoSupport</span><span style="line-height: 15px; font-size: 13px; text-indent: 28px; font-family: 宋体;">类</span>
public class BaseDao extends HibernateDaoSupport implements Dao{//private Session session;private DataSource dataSource;public DataSource getDataSource() {return dataSource;}public void setDataSource(DataSource dataSource) {this.dataSource = dataSource;}public void deleteEntity(Object entity) {getHibernateTemplate().delete(entity);}public List findAllEntity(String hql) {List<Object> listCondition = new ArrayList<Object>();listCondition = getHibernateTemplate().find(hql);return listCondition;}public List findEntityByCondition(String hql,Object[] paramArr) {List<Object> list = new ArrayList<Object>();list = getHibernateTemplate().find(hql, paramArr);return list;}public Object getEntity(Class entityClass, Serializable id) {Object entity = getHibernateTemplate().get(entityClass, id);return entity;}public Object loadEntity(Class entityClass, Serializable id) {Object entity = null;try {entity = getHibernateTemplate().load(entityClass, id);//load方法对象不存在时抛出异常} catch (Exception e) {}return entity;}public Serializable saveEntity(Object entity) {Serializable id = getHibernateTemplate().save(entity);return id;}public void updateEntity(Object entity) {getHibernateTemplate().update(entity);}
d)编写service层代码,以便spring对复杂的业务逻辑实现事务管理
service层接口
public interface  IUserService {//拥有多个操作,但这些操作必须 在同一个事物中(即复杂的业务逻辑时需要事务管理)void saveAndDelete(UserInfo userInfo);}
实现service层接口的实现类
public class UserServiceImpl implements IUserService{private Dao dao;public void saveAndDelete(UserInfo userInfo){Serializable id = dao.saveEntity(userInfo);System.out.println(id.toString());//测试spring管理的事务是否配置成功//int i = 1/0;UserInfo entity = (UserInfo) dao.loadEntity(UserInfo.class, id);dao.deleteEntity(entity);}public void setDao(Dao dao) {this.dao = dao;}}
e)操作数据库代码编写完成后,我们就需要在spring中注册类了。打开applicationContext.xml,然后注册<bean>
在applicationContext.xml中注册bean
<!-- ioc加载bean 开始 --><bean id="dao" class="com.spring.zd.dao.impl.BaseDao"><property name="sessionFactory" ref="sessionFactory"></property><property name="dataSource" ref="dataSource"></property></bean><bean id="userService" class="com.spring.zd.service.impl.UserServiceImpl"><property name="dao" ref="dao"></property></bean><!-- ioc加载bean 结束 -->
使用spring声明式事务配置hibernate的事务管理(主要管理service--具有复杂的业务逻辑)
<!-- 配置事务管理器 指定其作用的sessionFactory把事务交给Spring管理 --><bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory"></property></bean><!-- 在applicationContext.xml中默认是不能使用<tx:advice>和<aop:config>标签,需要在applicationContext.xml中,加入spring2.5的文档说明:xmlns:aop="http://www.springframework.org/schema/aop"xmlns:tx="http://www.springframework.org/schema/tx"http://www.springframework.org/schema/tx        http://www.springframework.org/schema/tx/spring-tx.xsd       http://www.springframework.org/schema/aop        http://www.springframework.org/schema/aop/spring-aop.xsd 后才能使用。--><!-- 配置事务的传播属性 --><tx:advice id="txAdvice" transaction-manager="transactionManager"><tx:attributes><tx:method name="save*" propagation="REQUIRED"/><tx:method name="update*" propagation="REQUIRED"/><tx:method name="delete*" propagation="REQUIRED"/><tx:method name="bulk*" propagation="REQUIRED"/><tx:method name="get*" read-only="true"/><tx:method name="load*" read-only="true"/><tx:method name="find*" read-only="true"/></tx:attributes></tx:advice><!-- 事务通知和切点的装配器,织入环境中,将事务交给Spring管理 --><aop:config><aop:pointcut expression="execution(* com.spring.zd.service.*.*(..))" id="pointCut"/><aop:advisor advice-ref="txAdvice" pointcut-ref="pointCut"/></aop:config>
<span style="text-indent: 20px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">f)测试类</span>
//用session处理public void test1(){Session session = LoadBeansXML.getSession();Transaction transaction = session.beginTransaction();UserInfo userInfo = new UserInfo("gwm", "123456", nowDate, 123456.23);Serializable id = session.save(userInfo);System.out.println(id);transaction.commit();}//hibernateTemplete 单事务处理public void test2(){UserInfo userInfo = new UserInfo("gwm2", "123456", nowDate, 123456.23);dao.saveEntity(userInfo);} //hibernateTemplete 多事务处理//测试spring管理的事务是否配置成功@Testpublic void test3(){IUserService userService = (IUserService) cxt.getBean("userService");UserInfo userInfo = new UserInfo("gwm3", "123456", nowDate, 123456.23);userService.saveAndDelete(userInfo);}
g)至此,spring2.5与hibernate3.2整合结束,测试成功!

3、spring与struts2整合
 (1)添加struts2的library
a)添加 Struts Capbilities,勾选中其中两项即可,点击finish.

b)上一步完成后,就可以看到项目的src目录下多了struts的配置文件struts.xml
c)在struts.xml配置文件中,可以通过<constant>节点设置:项目的开发模式,项目的字符编码集,项目是否使用远程方法调用等(这些属性可以在struts2-core-2.1.1.jar包下的default.properties文件中找到相关属性的和属性对应的值)。
<?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><!-- 设置项目的开发模式 --><constant name="struts.devMode" value="true"></constant><!-- 设置项目的编码格式 --><constant name="struts.i18n.encoding"  value="GBK"></constant><!-- 是否允许远程方法调用 --><constant name="struts.enable.DynamicMethodInvocation" value="true"></constant><package name="default" namespace="/" extends="struts-default"><action name="login" class="com.spring.zd.action.Login" method="regester"><result>/user/list.jsp</result></action></package></struts>  
d)编写action层代码
action层代码的实现方式分为两种方式:
一种是属性驱动:属性驱动需要继承子ActionSupport类,在类中写上需要获取的属性名称(必须是jsp页面form表单中存在的属性名称才能成功获取),对应的getter和setter方法。或者是在jsp页面有与form表单中属性名称相对应的javabean,可在jsp页面中设置 对象.属性 ,在action中new 出javabean对应的对象。
属性驱动  .java
public class Login <span style="background-color: rgb(51, 204, 0);">extends ActionSupport</span>{private UserInfo <span style="background-color: rgb(51, 204, 0);">userInfo</span>;private IUserService userService;//private Log log = LogFactory.getLog(this.getClass());<span style="color:#33cc00;">public void setUserInfo(UserInfo userInfo) {this.userInfo = userInfo;}public UserInfo getUserInfo() {return userInfo;}</span>public void setUserService(IUserService userService) {this.userService = userService;}public String regester(){//System.out.println(userInfo.getUsername());System.out.println(userInfo.getPassword());System.out.println(userInfo.getHiredate());userService.saveUserInfo(userInfo);//userService.saveAndDelete(userInfo);return this.SUCCESS;}}
<span style="white-space:pre"></span>.jsp 
<body>    This is my JSP page. <br>    <form method="post" action="<span style="background-color: rgb(255, 153, 0);">login!regester.action</span>" name="fm"><!-- 远程方法调用:<span style="background-color: rgb(255, 153, 0); font-family: Arial, Helvetica, sans-serif;">login!regester.action <span style="color:#ffffcc;">      </span></span>action的name!方法名.action -->username:<input name="<span style="color:#33cc00;">userInfo.username</span>" /><br>password:<input type="password" name="userInfo.password"/><br> hiredate:<input name="userInfo.hiredate" /><br><input type="submit" value="Login"/>    </form>  </body>
一种是模型驱动:模型驱动需要实现ModelDrivern<T>接口,同时实现接口的getModel方法(作用:获取对象)
模型驱动  .java
public class Login <span style="color:#33cc00;">implements ModelDriven<UserInfo></span>{private UserInfo userInfo<span style="color:#33cc00;"> = new UserInfo()</span>;private IUserService userService;//private Log log = LogFactory.getLog(this.getClass());<span style="color:#33cc00;">public void setUserInfo(UserInfo userInfo) {this.userInfo = userInfo;}public UserInfo getUserInfo() {return userInfo;}</span>public void setUserService(IUserService userService) {this.userService = userService;}public String regester(){//System.out.println(userInfo.getUsername());System.out.println(userInfo.getPassword());System.out.println(userInfo.getHiredate());userService.saveUserInfo(userInfo);//userService.saveAndDelete(userInfo);return null;}@Override<span style="color:#33cc00;">public UserInfo getModel() {// TODO Auto-generated method stubreturn userInfo;}</span>}
<span></span>.jsp 
<body>    This is my JSP page. <br>    <form method="post" action="<span style="background-color: rgb(255, 153, 0);">login!regester.action</span>" name="fm"><!-- 远程方法调用:<span style="background-color: rgb(255, 153, 0); font-family: Arial, Helvetica, sans-serif;">login!regester.action <span style="color:#ffffcc;">      </span></span>action的name!方法名.action -->username:<input name="<span style="color:#33cc00;">username</span>" /><br>password:<input type="password" name="password"/><br> hiredate:<input name="hiredate" /><br><input type="submit" value="Login"/>    </form>  </body>


  • 整合过程中的关键点:

1、在struts.xml中配置struts.ObjectFactory属性,可通过spring管理struts

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

2、在applicationContext.xml的文件声明中,配置  default-autowire="byName"属性 ,这样可以自动装配,不必显示配置依赖的bean

3、struts2使用属性驱动或者模型驱动时,使用对象封装来获取对象属性时,该对象的getter和setter方法,必须全部写上,否则拦截器不能正确的获取到对象的属性。(如果只写对象的setter方法,只能获取对象的第一个属性值,其它值为null)











0 0
原创粉丝点击