【原创】如何建立 SSH 项目 (Struts2 + Spring + Hibernate)

  1.  首先建立一个java web项目 名称随意 

  2.  右键项目-> Hibernate -> sprint capaili... 

  

  

  完成后,复制3分更改一下名称 

  applicationContext-struts.xml           applicationContext-dao.xml           applicationContext-service.xml 

  以上3个内容都一样, 然后再添加applicationContext-transaction.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:p="http://www.springframework.org/schema/p"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
    <bean id="transactionManager"
        class="org.springframework.orm.hibernate3.HibernateTransactionManager">
        <property name="sessionFactory">
            <ref bean="sessionFactory" />
        </property>
    </bean>
    <bean id="transactionInterceptor"
        class="org.springframework.transaction.interceptor.TransactionInterceptor">
        <property name="transactionManager">
            <ref bean="transactionManager" />
        </property>
        <property name="transactionAttributes">
            <props>
                <!--
                    表示对SErvice类中的所有方法都进行连接关闭的处理(同时还进行事务的提交和回滚操作。)
                    PROPAGATION_REQUIRE0表示使用的事务处理规则。
                    PROPAGATION_REQUIRED:如果之前有事务,将当前操作合并到事务中,如果之前没有事务,则开始一个新的事务。
                    PROPAGATION_REQUIRED_NEW:无论之前是否有事务,都开始一个新的事务。
                    PROPAGATION_REQUIRED_NEVER:不使用事务,自动提交
                -->
                <prop key="*">PROPAGATION_REQUIRED</prop>
            </props>
        </property>
    </bean>
    <bean
        class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
        <property name="beanNames">
            <list>
                <value>*ServiceImpl</value>
            </list>
        </property>
        <property name="interceptorNames">
            <list>
                <value>transactionInterceptor</value>
            </list>
        </property>
    </bean>
</beans>

  3.  开始加入hibernate支持,注意在加入之前先查看一下DB Browser 是否已经创建了连接

  

  如果没有的选择window-> show View -> other...-> MyEclipse Database ->DB Browser

  

  在DB BROWSER 界面里右键 新建 New...建数据库  我已mysql 为例

  

  开始加入Hibernate 支持

  

  

  

  

  

  完成已经步骤后,src目录下有一个applicationContext-hibernate.xml的文件

  修改application-hibernate.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:p="http://www.springframework.org/schema/p"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
        <property name="driverClassName" value="org.gjt.mm.mysql.Driver">
        </property>
        <property name="url" value="jdbc:mysql://localhost:3306/mydb"></property>
        <property name="username" value="root"></property>
        <property name="password" value="admin"></property>
    </bean>
    <bean id="sessionFactory"
        class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
        <property name="dataSource">
            <ref bean="dataSource" />
        </property>
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">
                    org.hibernate.dialect.MySQLDialect
                </prop>
                <prop key="hibernate.formar_sql">
                    true
                </prop>
                <prop key="hibernate.show_sql">
                    true
                </prop>
            </props>
        </property>
    </bean>
    <bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
        <property name="sessionFactory">
            <ref bean="sessionFactory" />
        </property>
    </bean>
</beans>

  4. 加入Struts2 

  项目->右键 加入Struts2支持

  

  

  添加完成后,src目录下会增加struts.xml文件,这是struts2的核心配置文件。在新建一个叫struts.properties的文件,也是用来配置的。

  在里面添加2个配置信息: name :  struts.i18n.encoding    value : gbk

              name :  struts.objectFactory   value: spring

  

    还要拷贝一个log4j.properties 放到 src目录下 (网上有下载)

   log4j的信息。

  修改一下/WEB-INFO/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">

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/classes/applicationContext-*.xml</param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>
    <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>
</web-app>

  最终的目录结构是这样的

  

  部署一下服务器,试一下,看是否有报错,如果没有报错就成功了

   

  补充:

    当我们完成部署后,新建一些测试文件后发现会报错,错误信息报在浏览器页面上,例如:

HTTP Status 500 -

type Exception report

message

description The server encountered an internal error () that prevented it from fulfilling this request.

exception

java.lang.reflect.InvocationTargetException
    sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    java.lang.reflect.Method.invoke(Method.java:597)
      .......    .......
root cause

java.lang.NoSuchMethodError: antlr.collections.AST.getLine()I
    org.hibernate.hql.ast.HqlSqlWalker.generatePositionalParameter(HqlSqlWalker.java:896)
    org.hibernate.hql.antlr.HqlSqlBaseWalker.parameter(HqlSqlBaseWalker.java:4819)
    org.hibernate.hql.antlr.HqlSqlBaseWalker.expr(HqlSqlBaseWalker.java:1373)
    org.hibernate.hql.antlr.HqlSqlBaseWalker.comparisonExpr(HqlSqlBaseWalker.java:3869)
    org.hibernate.hql.antlr.HqlSqlBaseWalker.logicalExpr(HqlSqlBaseWalker.java:1864)
    org.hibernate.hql.antlr.HqlSqlBaseWalker.whereClause(HqlSqlBaseWalker.java:818)
    org.hibernate.hql.antlr.HqlSqlBaseWalker.query(HqlSqlBaseWalker.java:604)
    org.hibernate.hql.antlr.HqlSqlBaseWalker.selectStatement(HqlSqlBaseWalker.java:288)        

  实际上是因为jar包冲突所致造成的,让我们来看一下jar包。

  

  antlr-2.7.2这个jar包是hibernate的,从hibernate3版本后,就开始使用antlr-2.7.5H3.jar这个jar包,所以要删除这个包

  spring-beans-2.5.6 等4个包也同样。所以都要删除掉

  操作如下 : window -> preferences - >  选择MyEclipse - Project Capalities -Struts2

  

  

  删除后,项目要重新部署(一定要Remove一次,因为要把之前的加入的jar包删除掉)。再启动测试吧

  祝大家成功

原文地址:https://www.cnblogs.com/arkia123/p/2748932.html