ssh项目搭建(有相同错误)

原文摘自:ruoya502  http://blog.sina.com.cn/s/blog_6870d1e00100jvwl.html

第一个做SSH集成的开发,很多都还不熟悉,这里就先写下第一个自己完成的SSH框架搭建的过程吧。
第一步:新建一个web project。Myeclipse中加入Spring capability。这个过程会加入Spring的配置文件,并自动加载Myeclipse自带的Spring jar包。如果不想用自带的,可以在build path中删除,再把Spring的jar加入web lib下面(注意:一定要加入web lib,否则容易出现ContextLoaderListener类没发现错误,详见另一篇博文SSH错误之ContextLoaderListener类没找到错误)。
第二步:Myeclipse中加入Hibernate capability,并选择不创建Factory Session,以及配置文件与Spring集成成一个,并选择已经加入的Spring配置文件,配置数据源。由Spring来创建Factory Session。同样,可以不适用自动加载的jar包,而导入自己下载的Hibernatejar包,
然后使用Myeclipse Hibernate reverse Engineering对要转换的数据库表进行反向映射,生成PoJo类,并生成Spring Dao文件。实现Spring对hibernate 的session的管理。
这里生成的Spring配置文件如下:其中增加了数据源,并配置了SessionFactory类。
<?xml version="1.0" encoding="UTF-8"?>
<beans
    xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">

    <bean id="dataSource"
        class="org.apache.commons.dbcp.BasicDataSource">
        <property name="driverClassName"
            value="com.mysql.jdbc.Driver">
        </property>
        <property name="url" value="jdbc:mysql://localhost:3306"></property>
        <property name="username" value="root"></property>
        <property name="password" value="412385524"></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>
            </props>
        </property>
        <property name="mappingResources">
            <list>
                <value>com/ruoya/entity/Student.hbm.xml</value></list>
        </property>
    </bean>
   
    <bean id="userDao" class="com.ruoya.daoImpl.UserDao">
        <property name="sessionFactory">
            <ref bean="sessionFactory" />
        </property>
    </bean>
    </beans>
第三步:加入Structs2,这个只要自己导入structs2包,并创建structs2.xml与struts.properties,放于src下面即可。
第四步:由于使用了多个配置文件,所以在web.xml中增加structs与spring的配置,如下:
<?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">
    <filter>
        <filter-name>struts2</filter-name>
        <filter-class>
            org.apache.struts2.dispatcher.FilterDispatcher
        </filter-class>
    </filter>
    <filter-mapping>
        <filter-name>struts2</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
    <listener>
        <listener-class>
            org.springframework.web.context.ContextLoaderListener
        </listener-class>
    </listener>
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
            /WEB-INF/classes/applicationContext.xml
        </param-value>
    </context-param>
    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>
</web-app>
默认Spring的配置文件要在/WEB-INF下面查找,可是我们放在了src下面,myeclipse会给自动加载的/WEB-INF/classes/下面,所以这里要进行路径配置,否则出现找不到Spring配置文件错误。
第五步:编写structs的action类,以及jsp文件。并编写service类。并把相应的action类使用Spring进行管理,即加入Spring的配置文件applicationContext.xml中。
第六步:部署项目到tomcat中,运行。

整个过程容易出现的错误有:
1、Hibernate,Spring中jar包冲突,asm等jar包冲突,要把一个里面的删除。具体见SSH错误asm包冲突。
2、ContextLoaderListener类找不到:Spring的jar包要放在web lib下面。
3、Spring listener没有配置,要配置Spring listener在web.xml中。
4、找不到Spring配置文件applicationContext.xml,要配置applicationContext.xml文件的位置:/WEB-INF/classes/applicationContext.xml。

posted @ 2017-07-24 01:01  小伙儿帅  阅读(152)  评论(0编辑  收藏  举报