一、新建项目

  eclipse->file->new->other->Dynamic Web Project,project name为sshDemo

二、下载jar包

1、struts:http://struts.apache.org。一般下载最新版2.5即可,下载完成后解压,将lib下的struts2-spring-plugin.jar导入lib,将apps/struts2-blank.war中的所有jar导入项目中的lib。

2、spring:http://repo.spring.io/release/org/springframework/spring。下载4.xx版本即可,记得下载带“RELEASE-dist”字样的而非“Resource”字样的。下载完成后解压,将libs下所有未带有source和javadoc的jar包导入lib

3、hibernate:http://www.mirrorservice.org/sites/download.sourceforge.net/pub/sourceforge/h/project/hi/hibernate/

或https://sourceforge.net/projects/hibernate/files/hibernate-orm/。一般下载3.6.10版本(hibernate3最高版本),解压后将lib/required、lib/bytecode/cglib、lib/jpa、lib/optional/c3p0和根目录的hibernate3.jar分别导入lib

4、数据库driver包:这里只举例oracle的,cmd中输入sqlplus即可显示数据库版本号,然后根据数据库版本查找对应的driver包。http://www.oracle.com/technetwork/database/features/jdbc/index-091264.html

三、文件配置

1、web.xml,配置Struts2的Filter和spring的监听器

  <filter>
       <filter-name>strutsFilter</filter-name>
       <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
   </filter>
   <filter-mapping>
       <filter-name>strutsFilter</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>classpath:applicationContext.xml</param-value>
  </context-param>

2、创建jdbc.properties文件,保存基本的数据连接信息,放在/WEB-INF根下

jdbc.driverClassName=oracle.jdbc.OracleDriver
jdbc.url=jdbc:oracle:thin:@localhost:1521:orcl
jdbc.username=scott
jdbc.password=scott123

  

3、applicationContext.xml-配置spring配置文件,将其放在src根下

<!-- 扫描properties文件 -->
    <context:property-placeholder location="WEB-INF/jdbc.properties"/>
    <context:annotation-config />
    <!-- 扫描@component注解文件 -->
    <context:component-scan base-package="cn.test.*"></context:component-scan>

    <!-- 配置dataSource,来获取connection,此处用的是c3p0连接池 -->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
        <property name="driverClass" value="${jdbc.driverClassName}"/>
        <property name="jdbcUrl" value="${jdbc.url}"/>
        <property name="user" value="${jdbc.username}"/>
        <property name="password" value="${jdbc.password}"/>

        <property name="initialPoolSize" value="3"></property>
        <property name="minPoolSize" value="3"></property>
        <property name="maxPoolSize" value="5"></property>
        <property name="acquireIncrement" value="3"></property>
        <property name="maxIdleTime" value="1800"></property>
    </bean>

    <!-- 配置SessionFactory,用来获取session-->
    <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
        <property name="dataSource" ref="dataSource"></property>
        <property name="configLocation" value="classpath:hibernate.cfg.xml"></property>
    </bean>

    <!-- 配置以下内容实现注解事务管理 -->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"/>
    </bean>
    <tx:annotation-driven transaction-manager="transactionManager"/>

4、hibernate.xml,将其放在src根下

<hibernate-configuration>
    <session-factory>
    <!--applicationContext.xml中已经配置了数据库连接的用户名密码等信息,此处配置数据库连接方言即可-->
        <property name="hibernate.dialect">org.hibernate.dialect.OracleDialect</property>
         <mapping resource="cn/test/model/Student.hbm.xml"/>
    </session-factory>
</hibernate-configuration>

   tip:关于方言可以使用ctrl+shift+t打开类型搜索界面,例如mysql数据库,输入*mysqldialect,然后选择对应版本的方言,记下起地址,填入即可。

5、struts.xml,将其放在src根下

    <!-- 设置开发模式,重新加载国际化资源文件和配置文件 -->
    <constant name="struts.devMode" value="true" />
    <package name="studentPkg" namespace="/helloworld" extends="struts-default">
        <!--如果请求地址是sshdemo_delete,则调用studentAction中的delete方法,这叫动态调用;class中写的studentAction是spring管理的beanName-->
        <action name="sshdemo_*" class="studentAction" method="{1}">
            <result name="stulist">/student/stulist.jsp</result>
            <result name="updatepage">/student/updatepage.jsp</result>
            <result name="insertpage">/student/insertpage.jsp</result>
            <result name="fail">/fail.jsp</result>
            <result name="success">/success.jsp</result>
        </action>
    </package>

四、代码上的使用

  1、创建model/bean类,需要创建该model/bean与数据库映射的.hbm.xml文件。如下,创建了Student类

package cn.test.model;

public class Student {
    private int id;
    private String name;
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
}

    其映射文件和该类同名,放在同级路径,Student.hbm.xml如下:

<hibernate-mapping>
        <class name="cn.test.model.Student" table="stutbl">
            <!--id列是主键列-->
            <id name="id">
                <generator class="identity" /><!-- native or identity-->
            </id>
            <property name="name" not-null="true"/>
        </class>
 </hibernate-mapping>

    最后,打开hibernate.xml文件,将映射配置入hibernate.xml文件中

 <mapping resource="cn/test/model/Student.hbm.xml"/>

  2、将类交由spring容器管理,既然我前面配置了注解方式,那么我就使用注解方式来管理相关的类

    1)在StudentDaoImpl类(需要是implDao,接口dao不用)上配置bean注解@Repository("stuDao")以及事务注解@Transactional,并在类中动态获取sessionFactory对象,通过sessionFactory.getCurrentSession()来获取session

@Transactional@Repository("stuDao")
public class StudentDaoImpl implements StudentDao {

    @Resource
      private SessionFactory sessionFactory;

    @Override
    public void delete(int stuid) {
        Session session = sessionFactory.getCurrentSession();
        session.delete(stuid);
    } 

    2)在StudentService类上配置注解@Service("stuService")并在类中动态获取dao对象

@Service("stuService")
public class StudentService {

    @Resource
    private StudentDao stuDao;
        ...

    3)将所有的Action都加上注解@Component交由spring容器管理,并动态获取service对象,@Scope("prototype")表示该action是多例形态的。

@Component("studentAction")
@Scope("prototype")
public class StudentAction  {
    private int id;
    private String name;

    @Resource
    private StudentService stuService;

    public int getId() {
        return id;
    }

五、常见问题

1)出现Could not open ServletContext resource[/WEB-INF/applicationContext.xml]
解决:监听器配置问题,考虑是否拼写有错误

2)出现java.lang.NoClassDefFoundError:Lorg/Hibernate/cache/CacheProvider
解决:hibernate4中移除了CacheProvider这个类,使用低于4.0版本的Hibernate-jar包,或使用更高版本的spring-jar包

3)弹窗detail:timeout waiting for tomcat
解决:window->preference->server->server timeout delay设置为Longer

4)出现:Could not determine type for:
解决:hibernate映射文件.hbm.xml中字段type填写错误,类似java.lang.Integer为正确的写法

5)出现java.lang.ClassNotFoundException: org.xxxx.xxx.xxx
解决:这个最easy,缺少相应的jar包,直接百度org.xxx.xxx下载加上就行

6)Listener refused the connection with the following error:ORA-12505, TNS:listener does not currently know of SID given in connect descriptor
解决:运行services.msc(或控制面板打开服务)->检查ORACLE相关服务是否开启->检查数据库连接字符串是否正确。参考:      http://blog.csdn.net/redarmy_chen/article/details/7025033

8、No mapping found for dependency [type=java.lang.String
解决:一般是拼写错误或者是jar包重复

9、Caused by: Action class [studentAction] not found
解决:struts-sping-plugin.jar未导入或拼写错误

十、附上资源文件

 内有已经配置好的配置文件以及示例demo

 百度云: http://pan.baidu.com/s/1i4qOUlJ

 防和谐链接:htt去p://pa掉n.b中aidu.co文m/s/1i4qOUlJ

SSH三大框架的基本整合以及常见错误的解决方法的更多相关文章

  1. centos linux 系统日常管理4 scp,rsync,md5sum,sha1sum,strace ,find Rsync 常见错误及解决方法 第十七节课

    centos linux 系统日常管理4  scp,rsync,md5sum,sha1sum,strace ,find Rsync 常见错误及解决方法  第十七节课 rsync可以增量同步,scp不行 ...

  2. Servlet常见错误及解决方法

    常见错误及解决方法 1. 404产生的原因为Web服务器(容器)根据请求地址找不到对应资源,以下情况都会出现404的错误提示: 输入的地址有误(应用名大小写不正确,名称拼写不正确) 在web.xml文 ...

  3. IIS7常见错误及解决方法

    IIS7常见错误及解决方法   问题一:HTTP 错误 500.19 - Internal Server Error 无法访问请求的页面,因为该页的相关配置数据无效.  详细错误信息模块 IIS We ...

  4. 微信jssdk常见错误及解决方法

    调用config 接口的时候传入参数 debug: true 可以开启debug模式,页面会alert出错误信息.以下为常见错误及解决方法: invalid url domain当前页面所在域名与使用 ...

  5. Windows10 下 github ssh 访问出现 Permission denied(publickey)错误的解决方法

    Windows10 下 github ssh 访问出现 Permission denied(publickey)错误的解决方法. 错误信息: git clone git@github.com:ediw ...

  6. WCF项目中出现常见错误的解决方法:基础连接已经关闭: 连接被意外关闭

    在我们开发WCF项目的时候,常常会碰到一些莫名其妙的错误,有时候如果根据它的错误提示信息,一般很难定位到具体的问题所在,而由于WCF服务的特殊性,调试起来也不是那么方便,因此往往会花费不少时间来进行跟 ...

  7. cmd常见错误及解决方法

    [英文] Bad command or file name [译文] 错误的命令或文件名 错误原因和解决: 这大概是大家最常见到的错误提示了,它的意思是输入的命令无效.当输入的命令既不是DOS内部命令 ...

  8. jmeter常见错误及解决方法

    jmeter常见错误:   错误一: Response code: Non HTTP response code: java.net.SocketTimeoutException Response m ...

  9. SSH三大框架的搭建整合(struts2+spring+hibernate)(转)

    原文地址:http://blog.csdn.net/kyle0349/article/details/51751913  尊重原创,请访问原文地址 SSH说的上是javaweb经典框架,不能说100% ...

随机推荐

  1. linux 线程回顾

    额,时隔两年重新写博客了. 这次看一下thread_cond_wait(pthread_cond_t * cond, pthread_mutex_t *mutex)和thread_cond_signa ...

  2. SQL点滴8—the account is currently locked out. The system administrator can unlock it.

    原文:SQL点滴8-the account is currently locked out. The system administrator can unlock it. 今天遇到的问题比较有意思. ...

  3. openwrt路由器更换了Flash之后需要修改的源码

    假如我使用的是WR703N,改为8M内存: 1 修改openwrt/target/linux/ar71xx/image/Makefile文件 $(eval $(call SingleProfile,T ...

  4. solr与.net主从复制

    solr主从复制 solr与.net系列课程(七)solr主从复制    既然solr是解决大量数据全文索引的方案,由于高并发的问题,我们就要考虑solr的负载均衡了,solr提供非常简单的主从复制的 ...

  5. 安装Windows2008操作系统 - 初学者系列 - 学习者系列文章

    Windows2008这款服务器操作系统不知道有多少服务器在使用,毕竟前面有经典的2003系统,后续有2012操作系统.具体就不讨论这些了.下面就对Windows2008服务器操作系统的安装进行介绍. ...

  6. AjaxPro实现无刷新更新数据

    使用AjaxPro实现无刷新更新数据 需求 在一个页面动态无刷新的更新后台得到的数据.要想无刷新的更新数据,需要使用Javascript能够获取后台返回的数据,然后通过第三方Javascript库(J ...

  7. Weblogic Server 的下载,安装配置与部署

    下载 下载页面: http://www.oracle.com/technetwork/middleware/weblogic/downloads/wls-for-dev-1703574.html 目前 ...

  8. Linux 宿主机安装 MiniGUI

    去MiniGUI官方网站看的时候,很兴奋,安装竟然这么容易. 上帝总是在给你一个苹果之后,赏你一巴掌.我的确是高兴太早了. 首先看一下官网文档的说明步骤: (截取于官方文档) Installing r ...

  9. 动态生成WebService的客户端

    给定了WebService地址和调用接口后动态的生成DLL,第二次再请求时会先判断DLL是否存在,以提高执行效率 核心代码下: /// <summary> /// 动态生成WebServi ...

  10. UNIX基础知识--<<UNIX 环境编程>>读书笔记

    1 shell程序就是位于应用软件与系统调用之间的程序   每个用户登录系统,系统就会为用户分配shell (用户的登录的口令文件 在  /etc/passwd 2 ls filename  运行原理 ...