快速搭建SSH框架(含资源文件)

        大家好!近期在网上看到很多人比较关注SSH框架的快速搭建。我个人便整理了一个快速搭建的SSH框架的Demo,由于时间限制的原因,今天我先帮助大家快速搭建,至于SSH框架技术原理以及一些业务流程下篇文章中我再根据这个Demo再给大家讲解。还请大家后期继续关注!

        好了,废话不多说了,下面开始快速搭建SSH框架。


第一步:SSH框架所需要的jar包




我这里一共用到上图那些jar包,关于其中有些jar在后续文章中我会一一介绍到,暂时先不用太关心这些jar的作用,因为我们今天的目的就是带领大家快速搭建一个SSH框架。

为了方便大家整合,我统一将jar包上传到网上,方便大家下载,免得网上到处搜集!

链接: https://pan.baidu.com/s/1slapmah 密码: 23eq


第二步:导入jar包


自己建一个动态web项目,这个我就不在这里介绍了。我的web项目结构如下图:




通过buildpath或者直接将下载好的jar包复制到WEB-INF下面的lib目录下面。


第三步:web.xml配置文件


web项目启动最开始的时候都是根据web.xml中配置的参数来启动的,因此该配置文件也是比较重要的:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>SSHDemo</display-name>
  <!-- 核心监听器,监听tomcat服务器 -->
  <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>
  
  <!-- struts 核心过滤器 -->
  <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>*.action</url-pattern>
  </filter-mapping>
  
  <welcome-file-list>
      <welcome-file>/WEB-INF/jsp/index.jsp</welcome-file>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>


    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
</web-app>



第四步:applicationContext.xml配置文件


applicationContext.xml是spring框架的核心配置文件,其中有关于数据源、Hibernate、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"    xmlns:p="http://www.springframework.org/schema/p"
     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-4.1.xsd     
   http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.1.xsd  
   http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.1.xsd  
   http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd">   
 
<!-- 导入外部的properties配置文件 -->

    <context:property-placeholder location="classpath*:db.properties"/>

<!-- 配置 c3p0数据源-->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"  destroy-method="close" >
<property name="jdbcUrl" value="${jdbcUrl}"></property>
<property name="driverClass" value="${driverClass}"></property>
<property name="user" value="${user}"></property>
<property name="password" value="${password}"></property>
<!-- 初始化时获取三个连接,取值应在minPoolSize与maxPoolSize之间 -->
<property name="initialPoolSize" value="${initialPoolSize}"></property>
<!-- 连接池中保留的最小连接数。 -->
<property name="minPoolSize" value="3"></property>
<!-- 连接池中保留的最大连接数 -->
<property name="maxPoolSize" value="${maxPoolSize}"></property>
<!-- 当前连接池中的连接耗尽的时候c3p0一次同时获取的连接数 -->
<property name="acquireIncrement" value="3"></property>
<!-- 最大空闲时间,1800秒内未使用则连接被丢弃,若为0则永不丢弃 -->
<property name="maxIdleTime" value="1800"></property>
</bean>

<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="hibernateProperties">
<props>
<!--  
<prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
-->

<prop key="hibernate.dialect">org.hibernate.dialect.SQLServer2008Dialect </prop>

<prop key="hibernate.show_sql" >true</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
<prop key="javax.persistence.validation.mode">none</prop>
</props>
</property>

<property name="mappingLocations">
<list>
<value>classpath:com/test/entity/*.hbm.xml</value>
</list>
</property>
</bean>

<!-- 事物管理 -->
<bean id="txManager" 
class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>

<!-- 事物通知 -->
<tx:advice id="txAdvice" transaction-manager="txManager">
<tx:attributes>
<tx:method name="get*" read-only="true"/>
<tx:method name="find*" read-only="true"/>
<tx:method name="list*"  read-only="true"/>
<!-- 其他方法采用事物回滚 -->
<tx:method name="*"  rollback-for="Throwable"/>
</tx:attributes>
</tx:advice>

<!-- aop配置哪些类需要切入事物 -->
<!-- 
<aop:config>
<aop:pointcut expression="execution(*.com.test.service.impl.*.*(..))"  id="serviceOperation"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="serviceOperation"/>
 </aop:config>
 -->
<!-- 引入其它的spring配置文件 -->
<import resource="classpath:com/test/conf/*-spring.xml"/>
</beans>  

第五步:db.properties配置文件


数据源配置文件,这个我就不作过多的解释了,因为这个配置文件比较基础而且里面的参数也比较简单,仅仅从几个参数的字面意思 也能理解是什么意思。我这里是用的SQLServer2008数据库,因此我就只配了SQLServer的驱动和URL,至于有些童鞋是使用MySQL或者Oracle或者其他数据库还请自己查阅一下相关资料。

#mysql
#jdbcUrl=jdbc:mysql://127.0.0.1:3306/testDemo?usUnicode=true&characterEncoding=utf8
#driverClass=com.mysql.jdbc.Driver

#sqlserver
jdbcUrl=jdbc:sqlserver://localhost:1433;DatabaseName=NETTEST
driverClass=com.microsoft.sqlserver.jdbc.SQLServerDriver
user=sa
password=bxl878878
initialPoolSize=10
maxPoolSize=20


 

第五步:struts.xml配置文件

struts.xml配置为struts.xml框架的核心配置文件。在这个配置文件中我们同样可以配置模块化的struts配置文件。

<!DOCTYPE struts PUBLIC
        "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
        "http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
    <!-- 禁止动态方法访问 -->
    <constant name="struts.enable.DynamicMethodInvocation" value="false"></constant>
    <!-- 设置成开发者模式 -->
    <constant name="struts.devMode" value="true"></constant>
<!-- 配置扩展名为action -->
<constant name="struts.action.extention" value="action"></constant> 
<!-- 把主体配置成simple-->
<constant name="struts.ui.theme" value="simple"></constant>   
  
    <include file="com/test/conf/test-struts.xml"></include>
 
</struts>

第六步:模块化的两个配置文件

test-struts.xml和test-spring.xml配置文件:

在上述项目目录结构图中conf包下面含有这两个配置文件,test-struts.xml配置文件如下:

<!DOCTYPE struts PUBLIC
        "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
        "http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
 
 <package name="Test"  namespace="/" extends="struts-default">
 
 <action name="list" class="com.test.action.TestAction"  >
 <result name="success">/WEB-INF/jsp/list.jsp</result>
 </action>

 </package>
     
</struts>


test-spring.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"
     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-4.1.xsd     
   http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.1.xsd  
   http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.1.xsd  
   http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd">  
   
   
   
   <bean id="testDao" class="com.test.dao.impl.TestDaoImpl">
   <property name="sessionFactory" ref="sessionFactory"></property>
   </bean>
   
   
   <!-- 扫描业务层的注解 -->
   <context:component-scan base-package="com.test.service.impl.TestServiceImpl">
   </context:component-scan>
   
   </beans>



第七步:启动项目

准备工作都完成后,下面我们需要进行启动测试,如果在启动的过程中没有出现任何错误,便说明框架搭建成功!至此就像我们盖房子一样,框架起来了,至于装修和门窗之类的填充我们后续再补上!  还请大家持续关注!如果有任何建议和问题还请留言建议!











评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值