`
z95001188
  • 浏览: 40704 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

SSH整合备忘

阅读更多

SSH整合.

          如果使用Myelipse直接导入Hibernate . Spring2.0 的话 .一般会有jar包冲突!

如 asm.jar 这个 和一个高版本的asm-2.2.3.jar 这两个就有问题!删掉后者即可

          准备工作就是先把所有jar导入,并且没有冲突

          SSH2 (Struts2.Hiebrnate3.2 .Spring 2.5)lib,请看图片!

          由于是项目中使用到的lib 所以有些多余的...

          然后开始忽悠忽悠写:

          具体过程就不需要详细讲解了.晚上文章一堆一堆的!

          核型要点就是:

            首先创建 Bean id=dataSource


            然后就是创建 sessionFactory  引用到

           <property name="dataSource" ref="dataSource">

      

            然后就可以根据DAO  Service Action 一层一层的相互配置就好了 !

 

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
    <property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
    <property name="url" value="jdbc:mysql://localhost:3306/link"></property>
    <property name="username" value="root"></property>
    <property name="password" value="admin"></property>
    <property name="maxActive" value="100"></property>
    <property name="maxIdle" value="30"></property>
    <property name="maxWait" value="500"></property>
    <property name="defaultAutoCommit" value="true"></property>
</bean>

<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
    <property name="dataSource" ref="dataSource"></property>
    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
            <prop key="hibernate.show_sql">true</prop>
        </props>
    </property>
    <property name="mappingResources">
        <list>
            <value>com/test/bean/User.hbm.xml</value>
        </list>
    </property>
</bean>

<bean id="userDao" class="com.test.dao.impl.UserDAOImpl" scope="singleton">
    <property name="sessionFactory">
        <ref bean="sessionFactory"/>
    </property>
</bean>

<bean id="userService" class="com.test.service.impl.UserServiceImpl">
    <property name="userDao" ref="userDao"></property>
</bean>

<bean id="userAction" class="com.test.action.user.SaveUserAction" scope="prototype">
    <property name="service" ref="userService"></property>
</bean>

 

          就贴点xml配置上来参考即可!

 

跟着 对应的 dao service action里面 都有相应的get set方法相互注入即可

 

 

开始配置Struts2

    首先导入 struts2-spring-plugin-2.0.11.jar 包

 

  打开这个jar包 可以看到有一个struts-plugin.xml 插件

 

BODY{font:x-small 'Verdana';margin-right:1.5em} .c{cursor:hand} .b{color:red;font-family:'Courier New';font-weight:bold;text-decoration:none} .e{margin-left:1em;text-indent:-1em;margin-right:1em} .k{margin-left:1em;text-indent:-1em;margin-right:1em} .t{color:#990000} .xt{color:#990099} .ns{color:red} .dt{color:green} .m{color:blue} .tx{font-weight:bold} .db{text-indent:0px;margin-left:1em;margin-top:0px;margin-bottom:0px;padding-left:.3em;border-left:1px solid #CCCCCC;font:small Courier} .di{font:small Courier} .d{color:blue} .pi{color:blue} .cb{text-indent:0px;margin-left:1em;margin-top:0px;margin-bottom:0px;padding-left:.3em;font:small Courier;color:#888888} .ci{font:small Courier;color:#888888} PRE{margin:0px;display:inline}

- < struts >
  < bean type =" com.opensymphony.xwork2.ObjectFactory " name =" spring " class =" org.apache.struts2.spring.StrutsSpringObjectFactory " />
- <!--
  Make the Spring object factory the automatic default 
  -->
  < constant name =" struts.objectFactory " value =" spring " />
- < package name =" spring-default " >
- < interceptors >
  < interceptor name =" autowiring " class =" com.opensymphony.xwork2.spring.interceptor.ActionAutowiringInterceptor " />
  < interceptor name =" sessionAutowiring " class =" org.apache.struts2.spring.interceptor.SessionContextAutowiringInterceptor " />
  </ interceptors >
  </ package >
  </ struts >

 

 

 Struts2 本来就是支持插件的. 导入该jar包 就为我们省去了很多自己编写的麻烦

 如果同时还需要导入 json插件... Struts2 默认 导入xml 顺序是 首先

struts-default.xml   然后就是插件 xml  但是如果有多个插件 ..导入顺序是随即的.. 最后才是 我们自己编写的struts.xml

 

我们在struts.xml中就只需要找到Spring定义的 action 声明就可以了

<action name="saveUser" class="userAction">
            <result name="success" type="redirect">listUser.action</result>
            <result name="input">/save.jsp</result>
</action>

 

 

同时web.xml文件除了配置Struts2 过滤器外 还需要加入Spring 监听

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

 

如果还需要用到事务..

   可以使用Spring2.5 的标注

 

  <!-- 以AspectJ方式 定义 AOP -->
    <aop:config proxy-target-class="true">
        <aop:advisor pointcut="execution(* XXXManager.*(..))" advice-ref="txAdvice"/>
    </aop:config>

 

<tx:advice id="txAdvice">
        <tx:attributes>
            <tx:method name="find*" read-only="true"/>
            <tx:method name="get*" read-only="true"/>
            <tx:method name="*" read-only="false" rollback-for="Exception"/>
        </tx:attributes>
    </tx:advice>

 

 同时 对于service层 也可以使用注释技术进来

   <!--自动搜索@Component , @Controller , @Service , @Repository标注的 -->
    <context:component-scan base-package="XXX.XXX.XXX" />

 

 

 

具体一些应用可以查看相关文档.!

 

 

  • 大小: 30 KB
  • 大小: 94.3 KB
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics