ssh初步整合

来源:互联网 发布:淘宝卖家代销怎么发货 编辑:程序博客网 时间:2024/05/18 18:55

想必大家在学ssh的时候遇到过各种问题和错误吧 ,下面我就来介绍一下我的整合经过,希望大家能从中学点什么

在myeclipse 新建一个web项目取名为 blog 目录如下

如下如下


然后再webroot中找到web-info中的lib然后把需要的jar包导入进去:如图

这里用到的是spring4.0 hibernate4.3 struts2.3.8


然后web.sml代码如下:

<?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></display-name>    
  <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <!-- <param-value>/WEB-INF/daoContext.xml /WEB-INF/applicationContext.xml</param-value> -->
        <param-value>classpath:beans.xml</param-value>
    </context-param>
    
    <filter>
        <filter-name>openSessionInView</filter-name>
        <filter-class>org.springframework.orm.hibernate4.support.OpenSessionInViewFilter</filter-class>
    </filter>

    <filter-mapping>
        <filter-name>openSessionInView</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
    <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.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.3//EN"
    "http://struts.apache.org/dtds/struts-2.3.dtd">

<struts>

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

    <package name="struts2" namespace="/" extends="struts-default">
        <!--
        <default-action-ref name="index" />
        <global-results>
            <result name="error">/error.jsp</result>
        </global-results>

        <global-exception-mappings>
            <exception-mapping exception="java.lang.Exception" result="error"/>
        </global-exception-mappings>
         -->
         <!--  
         <action name="user" class="com.qbd.obms.action.UserAction">  
               <result name="success">/executesuccess.jsp</result>
               <result name="register">/register.jsp</result>
               <result name="findsuccess">/userfindsuccess.jsp</result>
               <result name="input">/index.jsp</result>
               <result name="findallsucess">/findallsucess.jsp</result>
               <result name="updateusersuccess">/index.jsp</result>
               <result name="update1success">/setbook.jsp</result>
               <result name="listsuccess">/userlist.jsp</result>
               <result type="chain" name="lendsuccess">
                    <param name="actionName">book</param>  
                      
                    <param name="method">find</param>
               </result>  
               <result type="chain" name="updatesuccess">
                    <param name="actionName">book</param>   
                    <param name="method">find</param>
               </result>
         </action>-->
        <action name="article" class="com.qbd.blog.action.ArticleAction">  
               <result name="list1">/list1.jsp</result>
        </action>   
       
    </package>

    <!-- Add packages here -->

</struts>

跟着是spring.xml这里在hibernate的配置文件直接写在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: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/beans
           http://www.springframework.org/schema/beans/spring-beans.xsd
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context.xsd
           http://www.springframework.org/schema/tx
           http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
           http://www.springframework.org/schema/aop
           http://www.springframework.org/schema/aop/spring-aop-4.0.xsd">
      <context:annotation-config></context:annotation-config>
      <context:component-scan base-package="com.qbd"></context:component-scan>
     
      <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
        <!-- results in a setDriverClassName(String) call -->
        <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
        <property name="url" value="jdbc:mysql://localhost:3306/blog"/>
            <property name="username" value="root"/>
        <property name="password" value="root"/>
    </bean>
    <bean id="sessionFactory"
    class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <property name="annotatedClasses">
            <list>
                <value>com.qbd.blog.model.Article</value>
                <value>com.qbd.blog.model.User</value>
                <value>com.qbd.blog.model.Log</value>
            </list>
        </property>
        <property name="hibernateProperties">
             <value>
                <!-- 设置数据库方言 -->
                hibernate.dialect=org.hibernate.dialect.MySQLDialect
                <!-- 设置自动创建|更新|验证数据库表结构 -->
                hibernate.hbm2ddl.auto=update
                <!-- 是否在控制台显示sql -->
                hibernate.show_sql=true
                <!-- 是否格式化sql,优化显示 -->
                hibernate.format_sql=true
                <!-- 是否开启二级缓存 -->
                hibernate.cache.use_second_level_cache=false
                <!-- 是否开启查询缓存 -->
                hibernate.cache.use_query_cache=false
                <!-- 数据库批量查询最大数 -->
                hibernate.jdbc.fetch_size=50
                <!-- 数据库批量更新、添加、删除操作最大数 -->
                hibernate.jdbc.batch_size=50
                <!-- 是否自动提交事务 -->
                hibernate.connection.autocommit=true
                <!-- 指定hibernate在何时释放JDBC连接 -->
                hibernate.connection.release_mode=auto
                <!-- 创建session方式 hibernate4.x 的方式 -->
                hibernate.current_session_context_class=org.springframework.orm.hibernate4.SpringSessionContext
            </value>
        </property>
    </bean>
    <bean id="txManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory"/>
    </bean>
    <aop:config>
        <aop:pointcut id="businessService"
               expression="execution(public * com.qbd.blog.serviceimpl..*.*(..))"/>
        <aop:advisor pointcut-ref="businessService" advice-ref="tx-advice"/>
    </aop:config>

    <tx:advice id="tx-advice" transaction-manager="txManager">
        <tx:attributes>
            <tx:method name="add*" propagation="REQUIRED"/>
            <tx:method name="delete*" propagation="REQUIRED"/>
            <tx:method name="update*" propagation="REQUIRED"/>
           </tx:attributes>
    </tx:advice>

    <bean id="hibernateTemplate" class="org.springframework.orm.hibernate4.HibernateTemplate">
        <property name="sessionFactory" ref="sessionFactory"></property>
    </bean>
</beans>

这里面有些东西需要替换,数据库要自己建立,初学者可以尝试自己改下,只有这样才能记得清楚,明白道理。

把那些该替换的改为自己的包路径或者名字即可

把struts的配置文件和spring的都放在src目录下,编译后会自动放到classes目录下

然后把项目添加到tomcat中启动运行


0 0
原创粉丝点击