SSH环境搭建

本文详细介绍SSH(Struts、Spring、Hibernate)框架的环境搭建过程,包括所需JAR包、配置文件等关键技术点,并分享作者在搭建过程中遇到的问题及解决方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

水水的我开始写水水的java web 项目,写项目前首先是环境搭建,环境搭建主要是jar包导入。jar如果导入正确,配置文件没有问题,项目很快就能做的差不多。
第一步
jar包
SSH,顾名思义包括Struts、Spring和hibernate。关键就是三者单独的jia包和整合起来的jar.


Sping jar包
1首先包括4+1:即核心(core),上下文(context),工厂(beans),表达式(expression)。
2事务管理:Spring-tx,spring-aspects,spring-aop,spring-aopalliance(aop联盟),aspectj.weaver
3连接数据库:Spring-jdbc,mysql-connector,c3p0,spring.orm
4日志管理:commons-loggging
5web端:spring-web


struts包
asm,asm-commons,asm-tree
common-fileupload(commons组件,文件上传)
commons-io(java.io增强包)
commons-lang3(Java-lang)
commons-log(日志)
freemark(模板技术)
log4(日志,详细记录)
ognl(表达式,值栈底层)
javaassist
xwork-core(从哪里来)


hibernate包
这里写图片描述
全部包截图
这里写图片描述
这里写图片描述
Spring配置文件
分为两个
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:aop="http://www.springframework.org/schema/aop" 
    xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
                        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
                        http://www.springframework.org/schema/context
                        http://www.springframework.org/schema/context/spring-context-3.0.xsd
                        http://www.springframework.org/schema/aop 
                        http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
                        http://www.springframework.org/schema/tx
                        http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">

    <!-- 1.1加载properties文件 -->
   <context:property-placeholder location="classpath:jdbcInfo.properties"/>
    <!-- 1.2配置数据源,使用c3p0 -->
   <bean id = "dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" value="${jdbc.driver}"></property>
        <property name="jdbcUrl" value="${jdbc.url}"></property>
        <property name="user" value="${jdbc.username}"></property>
        <property name="password" value="${jdbc.password}"></property>
   </bean>

   <!-- 2配置sessionFactory -->
   <!-- 配置sessionFactory -->
   <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
        <property name="dataSource" ref="dataSource"></property>
        <property name="hibernateProperties">
            <props>
                <prop key="show_sql">true</prop>
                <prop key="format_sql">true</prop>
                <prop key="hbm2ddl.auto">update</prop>
                <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
                <prop key="hibernate.current_session_context_class">thread</prop>
            </props>
        </property>
        <!--mappingLocations支持通配符  -->
        <!-- <property name="mappingLocations" value="classpath:entity/*.hbm.xml"></property> -->
   </bean>

    <!-- 3事务管理,只作用在service -->
    <!-- 3.1事务管理器 -->
    <bean id = "txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory"></property>
    </bean >
    <!-- 3.2事务详情,增删改读写,查询只读 -->
    <tx:advice id="txAdvice" transaction-manager="txManager">
        <tx:attributes>
            <tx:method name="add*" isolation="DEFAULT" propagation="REQUIRED"/>
            <tx:method name="update*" isolation="DEFAULT" propagation="REQUIRED"/>
            <tx:method name="delete*" isolation="DEFAULT" propagation="REQUIRED"/>
            <tx:method name="find*" read-only="true"/>
        </tx:attributes>
    </tx:advice>
    <!-- 3.3aop编程确定切入点 -->
    <aop:config>
        <aop:advisor advice-ref="txAdvice" pointcut="execution(* com.zym.crm.*.service..*.*(..))"/>
    </aop:config>

    <!-- 4 导入其他配置文件 -->
    <!-- 4.1 员工 -->
    <import resource="其他配置文件路径"/>
</beans>

其他配置文件–主要是其模块,例如下面的

<?xml version="1.0" encoding="UTF-8"?>
<!-- $Id: VMHelper.java 16 2012-05-31 05:56:57Z wangxindong $ -->
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:aop="http://www.springframework.org/schema/aop" 
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
                        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
                        http://www.springframework.org/schema/aop 
                        http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
                        http://www.springframework.org/schema/context
                        http://www.springframework.org/schema/context/spring-context-3.0.xsd
                        http://www.springframework.org/schema/tx
                        http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">

 <bean id="staffDao" class="com.zym.crm.staff.dao.impl.StaffDaoImpl">
    <property name="sessionFactory" ref="sessionFactory"></property>
 </bean>

 <bean id="staffService" class="com.zym.crm.staff.service.impl.StaffServiceImpl">
    <property name="staffDao" ref="staffDao"></property>
 </bean>

</beans>

他们在同一个目录下,因此模块中的可以直接引用sessionFactory。
hibernate配置文件
因为Spring整合了hibernate,所以项目中没有没有hibernate单独配置文件,关于hibernate配置在Spring配置文件中

<!-- 2配置sessionFactory -->
   <!-- 配置sessionFactory -->
   <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
        <property name="dataSource" ref="dataSource"></property>
        <property name="hibernateProperties">
            <props>
                <prop key="show_sql">true</prop>
                <prop key="format_sql">true</prop>
                <prop key="hbm2ddl.auto">update</prop>
                <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
                <prop key="hibernate.current_session_context_class">thread</prop>
            </props>
        </property>
        <!--mappingLocations支持通配符  -->
        <!-- <property name="mappingLocations" value="classpath:entity/*.hbm.xml"></property> -->
   </bean>

Struts配置文件
注意struts.xml文件必须在classpath下,所以必须是这样的目录结构(如图所示)
这里写图片描述
其中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>
<!-- 1.常量 -->
    <!-- 开发者模式 -->
    <constant name="struts.devMode" value="true" />
    <!-- 标签主题,没有风格 -->
    <constant name="struts.ui.theme" value="simple"/>
<!--2.公共项  -->
    <package name="common" namespace="/"></package> <!-- extends="struts-default" -->

<!-- 3.加载其他配置文件 -->
    <include file="struts/struts-staff.xml"/>

</struts>

其中加载用的是include,而Spring用的是import,不一样哦。package名字是common,方便其他模块的struts配置文件引用

其他模块的struts配置文件(举例子说明)

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN"
    "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
<!-- 员工配置 -->

    <!-- 继承父亲,common -->
    <package name="sta"  namespace="/" extends="common" >

    <action name="staffAction" class="com.zym.crm.staff.web.action.StaffAction" method="{1}">
        <!-- 1.登陆成功 -->
        <!-- <result name="success" type="redirectAction"></result> -->
        <!-- 2 登录失败,请求转发首页 -->
        <!-- <result name="login">/WEB-INF/pages/login.jsp</result> -->
        <!-- 3 首页 -->
        <!-- <result name="home">/WEB-INF/pages/frame.jsp</result> -->
    </action>

    </package>

</struts>

注:package里面引用的是公共的。
web.xml文件配置
主要是Spring 监听器配置和stutter拦截器的配置,具体如下

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" 
    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_3_0.xsd">
  <display-name>crm</display-name>

  <!-- 1.1 Spring配置文件位置
  方式1:<param-value>classpsth:spring/applicationContext.xml</param-value>
  需要在 applicationContext.xml中import
  方式2:<param-value>classpsth:spring/applicationContext*.xml</param-value>
  建议方式1-->
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:spring/applicationContext.xml</param-value>
  </context-param>
  <!-- 1.2加载Spring配置文件的监听器 -->
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>

 <!-- 2struts的前端控制器-->
  <filter>
    <filter-name>struts2</filter-name>
   <!-- <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareFilter org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter </filter-class> -->
  <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>

错误说明
SSH环境我搭了将近一天。首先所示用zhangboy给的struts jar包,但是项目启动总是报错,总是说dispacher找不到,可是我明明有struts-core的包。那个气呀,后来我百度说是版本不对,然后我又重新下载,结果报lang3找不到,错误,但是我真的有!
就这么上午过去了~~,呜呜呜
下午,我想了一下,决定自己用官方下载的,我自己从官方网站上下载了struts包,结果里面包括sturts和spring的包,以及两个整合的。我就把struts的包全部替换掉,终于struts没有错了!
然而,Spring开始报错了,开始说Spring配置文件错了,后来我又重新改了配置文件,终于是没有错了。。说“通配符很全面,但是找不到tx的声明”,我看了一下果然是配置文件的xsi和jar 不对应,我改了后,就好了。
这里写图片描述

这一天的虽然折腾,但是让我对jar包有了更近一步的了解。。。注意,不要心急,有错误一点一点来。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值