如何部署一个简单的SSH框架?只需要这几个步骤,请戳开...

前言:

本文适合部署SSH项目失败的,主要解决部署时的异常!

我为什么要写这篇文章?

  学了一个周的Spring了,部署个SSH项目都到处报错,为一个sessionFactoty can‘t be build异常困扰了一个周,根本冷静不下来,难道跟 Spring 这么无缘?NO NO NO,不完全是这样的!

  最近冷静下来,重新部署了一次项目,成功了!下面与同样被sessionFactory 或者说applicationContext.xml 报出的异常困扰的朋友们分享一下“成功” 的经验!

正文:

  整合SSH总体的步骤:

    加入架包-->编写web.xml配置文件-->编写hibernate.cfg.xml-->编写applicationContext.xml

-->编写struts.xml-->完善代码

    

  1、架包

    

  下载地址:SSH整合所需架包 / SSH整合所需架包(二) 总大小:14MB

  2、web.xml

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <web-app version="3.0"
 3     xmlns="http://java.sun.com/xml/ns/javaee"
 4     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 5     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
 6     http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
 7   <!--告诉默认读取哪个配置-->
 8   <context-param>
 9       <param-name>contextConfigLocation</param-name>
10       <param-value>classpath:applicationContext.xml</param-value>
11   </context-param>
12   <!--开启监听:不开启将无法读取context-paramm-->
13   <listener>
14       <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
15
16   </listener>
17   <!--开启事务过滤器-->
18   <filter>
19       <filter-name>OpenSessionInViewFilter</filter-name>
20       <filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
21   </filter>
22   <filter-mapping>
23       <filter-name>OpenSessionInViewFilter</filter-name>
24       <url-pattern>*.action</url-pattern>
25   </filter-mapping>
26   <!--struts2配置-->
27   <filter>
28       <filter-name>struts2</filter-name>
29       <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
30   </filter>
31
32   <filter-mapping>
33       <filter-name>struts2</filter-name>
34       <url-pattern>/*</url-pattern>
35   </filter-mapping>
36 </web-app>

web.xml

  3、hibernate.cfg.xml

 1 <!DOCTYPE hibernate-configuration PUBLIC
 2     "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
 3     "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
 4
 5 <hibernate-configuration >
 6     <session-factory >
 7
 8     <!-- hibernate.dialect(方言)使用的是哪一种数据库
 9     org.hibernate.dialect.MySQLDialect
10     org.hibernate.dialect.SQLServerDialect
11     -->
12     <property name="hibernate.dialect">
13     org.hibernate.dialect.MySQLDialect
14     </property>
15
16     <!-- 数据库驱动类
17     com.mysql.jdbc.Driver
18     com.microsoft.sqlserver.jdbc.SQLServerDriver
19     -->
20     <property name="hibernate.connection.driver_class">
21     com.mysql.jdbc.Driver
22     </property>
23
24     <!-- 用户名 -->
25     <property name="connection.username">root</property>
26
27     <!-- 用户密码 -->
28     <property name="connection.password">abc123</property>
29
30     <!-- 自动建表 -->
31     <property name="hibernate.hbm2ddl.auto">update</property>
32
33     <!-- url
34     jdbc:sqlserver://localhost:1344;DataBaseName=testDB
35     -->
36     <property name="connection.url">jdbc:mysql://localhost:3306/use4class</property>
37
38     <!-- 控制台查看sql语句 -->
39     <property name="show_sql">true</property>
40
41     <!-- 控制台查看格式化sql语句 -->
42     <property name="hibernate.format_sql">true</property>
43
44     <!-- 告诉hibernate映射文件路径 -->
45
46
47     </session-factory>
48
49 </hibernate-configuration>

hibernate.cfg.xml

  我使用的mysql数据库,如果你使用别的数据库,请替换家暴、修改驱动、登录信息、数据库语言

  4、struts.xml

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <!DOCTYPE struts PUBLIC
 3     "-//Apache Software Foundation//DTD Struts Configuration 2.1.7//EN"
 4     "http://struts.apache.org/dtds/struts-2.1.dtd">
 5
 6 <struts>
 7     <!-- 允许使用动态方法 -->
 8     <constant name="struts.enable.DynamicMethodInvocation" value="true"></constant>
 9     <!-- 字符编码 -->
10     <constant name="struts.i18n.encoding" value="utf-8"></constant>
11     <!-- 主题 -->
12     <constant name="struts.ui.theme" value="simple"></constant>
13     <!-- 开发者模式 -->
14     <constant name="struts.devMode" value="true"></constant>
15     <!-- 国际化 -->
16     <constant name="struts.custom.i18n.resources" value="message"/>
17     <package name="default" namespace="/" extends="struts-default">
18         <!-- 获取文章 -->
19         <!--<action name="*Sys" class="com.jboaoffice.action.SysAction" method="{1}">
20             <result name="success" >/index.jsp</result>
21             <result name="input">/login.html</result>
22             <result name="suc">index.jsp</result>
23             <result name="err">login.html</result>
24         </action>-->
25
26     </package>
27 </struts>

struts.xml

  5、applicationContext.xml

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4        xmlns:p="http://www.springframework.org/schema/p"
 5        xmlns:context="http://www.springframework.org/schema/context"
 6        xmlns:tx="http://www.springframework.org/schema/tx"
 7        xmlns:aop="http://www.springframework.org/schema/aop"
 8        xsi:schemaLocation="
 9            http://www.springframework.org/schema/aop
10            http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
11          http://www.springframework.org/schema/beans
12          http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
13          http://www.springframework.org/schema/context
14          http://www.springframework.org/schema/context/spring-context-3.0.xsd
15            http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd ">
16
17
18
19  <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
20      <property name="configLocation">
21          <value>classpath:hibernate.cfg.xml</value>
22      </property>
23      <property name="mappingDirectoryLocations">
24          <list>
25              <value>classpath:com/jboa/entity/</value>
26          </list>
27      </property>
28  </bean>
29
30
31  <!-- 配置hibernate -->
32  <bean id="hibernateTeamp" class="org.springframework.orm.hibernate3.HibernateTemplate">
33      <constructor-arg name="sessionFactory" ref="sessionFactory"></constructor-arg>
34  </bean>
35
36
37  <!-- Dao配置 -->
38  <bean id="empDao" class="com.jboaoffice.impl.SysEmployeeImpl">
39      <property name="hibernateTemplate" ref="hibernateTeamp"></property>
40  </bean>
41
42  <!-- biz配置 -->
43  <bean id="empBiz" class="com.jboaoffice.biz.impl.SysEmployeeBizImpl">
44      <property name="empdao" ref="empDao"></property>
45  </bean>
46
47  <!-- action 配置 -->
48  <bean id="empAction" class="com.jboaoffice.action.SysAction">
49      <property name="empBiz" ref="empBiz"></property>
50  </bean>
51
52
53  <!-- 配置事务管理器 -->
54  <!-- <bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
55      <property name="sessionFactory" ref="sessionFactory"></property>
56  </bean>
57  <tx:advice id="txadvice" transaction-manager="txManager">
58      <tx:attributes>
59          <tx:method name="get*" read-only="true"/>
60          <tx:method name="find*" read-only="true"/>
61          <tx:method name="search*" read-only="true"/>
62          <tx:method name="list*" read-only="true"/>
63          <tx:method name="update*" propagation="REQUIRED"/>
64          <tx:method name="del*" propagation="REQUIRED"/>
65          <tx:method name="save*" propagation="REQUIRED"/>
66      </tx:attributes>
67
68  </tx:advice>
69  aop config
70 <aop:config>
71     <aop:pointcut expression="execution(* com.jboaoffice.bi.impl.*.*(..))" id="tx"/>
72     <aop:advisor advice-ref="txadvice" pointcut-ref="tx" />
73 </aop:config>
74   -->
75  </beans>

applicationContext.xml

  请注释掉sessionFactory后面</bean>前面的代码【那是配置dao、hibernateTemplate、biz、action自动注入的】,做完这几个工作你就可以运行服务!

  applicationContext里面的配置有很多种形式,先把基础的做了

  6、完善代码

    1、下面你可以去完善entityBean & entityBean.hbm.xml

    2、dao、 biz(service)、action 代码 并且去配置struts   

    3、请取消注释sessionFactory后面</bean>前面的代码

    再运行应该不会报框架的问题了!

 小总结:

  根据以往在web中使用框架的经验:

    1、架包正确(缺架包一般都会报出 classnotfound 的异常,后面就是一长串命名空间 :很好找)

    2、配置正确 (spring 框架我遇到最多的是sessionFactory can‘t be build )

    3、配置中提到的 java bean 存在 (java bean 不存在 提示很明显的 直接把你配置里面的bean id 打印出来,对症下药方可解决)

    即可正常启动服务!

  我经常犯得错误:

    1、【配置】少配置内容、命名错误、bean 关系不完整

    2、【Java代码】反向工程建映射文件【Spring可能会不买账】,我的是出问题了,报sessionFactory can‘t be build 的异常

    sessionFactory can‘t be build :

    这是个困扰我很久的异常,这个异常详细内容有这么一段话

    

    

google 过后得到的答案是使用了低版本hibernate 、高版本的springFramework  ,

我崩溃了,于是我就重新建项目,按照这个流程,成功了。上面那个问题我会再检查,看到的朋友,如果你遇到过并且解决了,请告诉我;或者你知道这是什么原因,告诉我,告诉大家!

  

?出自新手之笔,如有错误,请提出,大家一起纠正?

    

  

时间: 2024-08-08 05:01:48

如何部署一个简单的SSH框架?只需要这几个步骤,请戳开...的相关文章

自己实现的一个简单的EF框架(反射实现)

我实现了一个简单的EF框架,主要用于操纵数据库.实现了对数据库的基本操纵--CRUD 这是项目结构 这是一个 core 下的 DLL 写了一个数据库工厂,用于执行sql语句.调用sql语句工厂 写了一个sql语句工厂,用于生成sql语句.调用类型工厂 写了一个类型工厂,用于获取所需的类型,识别特性等. appsettings.json是配置文件 最后一个是使用说明 我实现过程的首先从底层开始. 首先写的是类型工厂 结构 BaseTypeHelper.cs 是基础的类型帮助类 TypeHelper

一个简单的web框架实现

一个简单的web框架实现 #!/usr/bin/env python # -- coding: utf-8 -- __author__ = 'EchoRep' from wsgiref.simple_server import make_server def index(): # data = open('html/index.html').read() return data def echo(): # data = open('html/echo.html').read() return d

使用idea搭建一个简单的SSM框架:(3)配置spring+mybatis

在此之前请先查看: 使用idea搭建一个简单的SSM框架:(1)使用idea创建maven项目 使用idea搭建一个简单的SSM框架:(2)配置springMVC 1 配置spring和mybatis整合文件 spring和mybatis整合分为三个步骤:(1)配置数据库,(2)配置SqlSessionFactoryBean (来自mybatis-spring),(3)配置MapperScannerConfigurer,该配置就我们通过接口定义的方法,就是mybatis的xml对应的namesp

java创建一个简单的小框架frame

import java.awt.*; import javax.swing.*; public class SimpleFrameTest { public static void main(String[] args) { EventQueue.invokeLater(new Runnable(){ // 开一个线程 public void run() { SimpleFrame frame = new SimpleFrame(); frame.setTitle("记事本"); //

eclipse中SSH框架搭建和项目开发基本步骤

1.下载SSH框架代码和eclipse插件,地址:http://yunpan.cn/QTCrdHF4xkEVp (提取码:0e8d) 注意,一定要分清楚,SSH框架是要导入到自己的工程项目中的包,这些包是要在项目中调用的预先开发好的java文件:而eclipse插件是在eclipse环境下开发SSH相关项目是方便用户建立项目管理项目的工具,跟项目本身的文件和功能无关.一定要分清楚这两个概念. 2,下载完成之后,解压,会发现有5个文件夹,第一步要用到的是spring plugins for ecl

AsMVC:一个简单的MVC框架的Java实现

当初看了<从零开始写一个Java Web框架>,也跟着写了一遍,但当时学艺不精,真正进脑子里的并不是很多,作者将依赖注入框架和MVC框架写在一起也给我造成了不小的困扰.最近刚好看了一遍springMVC的官方文档,对过去一段时间的使用做了一下总结,总结了一些MVC的使用需求,打算自己开坑写一个MVC框架,虽然是重复造轮子的过程,但也是学习提高的过程. 1.我们可能需要一个什么样的MVC框架 (1)用户一:我讨厌配置文件,最好能用注解的全用注解注解,能扫描直接扫描 (2)用户二:最好我导入一个j

js实现一个简单的MVVM框架

以前都是默默地看园子里的文章,猥琐的点赞,今天也分享一下自己用js实现的一个简单mvvm框架. 最初只做了自动绑定事件,后面又参考学习了vue,knouckout以及argular实现方式,以及结合自己做WPF的一些经验,增加了属性绑定,今天又稍微整理了下,完善了部分功能,把代码提交到了码云:https://gitee.com/zlj_fy/Simple-MVVM 先简单介绍下用法: 1 <form class="form-horizontal" role="form&

Python学习 - 编写一个简单的web框架(二)

在上一篇日志中已经讨论和实现了根据url执行相应应用,在我阅读了bottle.py官方文档后,按照bottle的设计重写一遍,主要借鉴大牛们的设计思想. 一个bottle.py的简单实例 来看看bottle是如何使用的,代码来自http://www.bottlepy.org/docs/0.12/index.html: from bottle import route, run, template @route('/hello/<name>') def index(name): return t

撸一个简单的MVVM框架

我个人以为mvvm框架里面最重要的一点就是VM这部分,它要与Model层建立联系,将Model层转换成可以被View层识别的数据结构:其次也要同View建立联系,将数据及时更新到View层上,并且响应View对数据的更改,同步到Model层. MVVM的具体例子,可以看一下阮一峰老师的这篇博客. 我们提取其中比较关键的点: Model层存储数据 需要一个View-Model来对数据做中转,响应数据变化,同步到两端 View层来负责展示数据,接受用户事件 Model层,我们用一个对象来代表.例如: