第一步:使用eclipse创建一个Dynamic Web Project ,在最后勾选生成 web.xml文件。
第二步:导入SSH框架所需jar包,struts2所需jar包在struts2\app\struts2-showcase\WEB_INF\lib下;spring所需jar包在 spring\libs下(注意javadoc和source后缀的可以不用导入);hibernate所需jar包在hibernate\lib\required 下所有包。
struts2所需jar包下载,请点击这里(不用积分)
spring所需jar包下载,请点击这里(不用积分)
hibernate所需jar包下载,请点击这里(不用积分)
本项目所用到的包: 请点击这里(不用积分)
MySQL数据库驱动包: 请点击这里(不用积分)
第三步:在web.xml中配置struts2的拦截器和映射,代码如下:
<!--struts2的 过滤器 -->
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<!-- struts2的映射 -->
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
第四步:在src目录下创建一个struts的核心配置文件struts.xml,注意该配置文件的头可以直接从 showcase.war\WEB-INF\src\java\struts.xml中拷贝过来,结果 如图:
第五步:在src目录下创建一个spring的核心配置文件applicationContext.xml,文件头可以在结果如图:
其中文件头内容如下:
<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:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.2.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd">
</beans>
第六步:在web.xml中配置监听器,另外,为applicationContext配置全局触发参数,结果如图所示:
注意:如果不配置<context-param>,applicationContext.xml文件会默认放置WEB_INF下
第七步:整合spring和Hibernate.在applicationContext.xml中进行配置,相关配置步骤如下:
1.在src目录下创建一个conf包,在这个包里面新建一个 jdbc.properties 文件,jdbc.properties文件主要包括数据库驱动名,URL,用户名和密码,这里使用的MYSQL数据库。当然,也可以不用创建这个文件而直接在applicationContext.xml中进行配置,但是那样不便于修改,出错的可能性较大。通过单独建一个文件,将数据库一些相关的配置值写在其中使得框架结构更加有层次感,也便于修改。这里将jdbc.properties的内容贴出来,具体设置根据个人情况适当修改。
jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/jwxt?autoReconnect=true&characterEncoding=UTF-8
jdbc.username=root
jdbc.password=password
jdbc.maxActive=100
jdbc.maxIdle=20
jdbc.logAbandoned=true
jdbc.removeAbandoned=false
jdbc.removeAbandonedTimeout=60
hibernate.dialect=org.hibernate.dialect.MySQLDialect
hibernate.show_sql=true
hibernate.hbm2ddl.auto=update
hibernate.format_sql=true
hibernate.connection.autocommit=true
其中一些参数的含义如下:
maxActive:最大连接数据库连接数,设置为0为无限制
maxIdle: 最大空闲数,数据库连接的最大空闲时间,超过空闲时间,将被标记为不可用,然后被释放,设置为0表示无限制
logAbandoned: 连接被泄露使是否打印,true为打印
removeAbandoned: 超过removeAbandonedTimeout时间后,是否进行没用连接的回收,true为回收超时连接,默认为false
removeAbandonedTimeout: 超过时间限制,回收没有用的链接,默认为300秒
hibernate.dialect: Hibernate数据库方言,就是用来确定Hibernate连接的是哪种类型的数据库
hibernate.show_sql: 输出sql语句,为了调试用,,可以看到执行了哪些sql语句
hibernate.hbm2ddl.auto: 常用的参数是update,意思是没有表的时候创建表,有表的时候更新表,还有一个create,意思是每次都会重新创建表,
不适合插入内容.另外,还有几个参数这里不做过多介绍
hibernate.format_sql: 格式化sql语句,可以把一条sql语句分成几行,看的更清晰
hibernate.connection.autocommit: true表示自动提交
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath*:conf/*.properties</value>
</list>
</property>
</bean>
2.配置数据源和连接池
<!--配置数据源和连接池 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="${jdbc.driverClassName}"></property>
<property name="url" value="${jdbc.url}"></property>
<property name="username" value="${jdbc.username}"></property>
<property name="password" value="${jdbc.password}"></property>
<property name="maxActive" value="${jdbc.maxActive}"></property>
<property name="maxIdle" value="${jdbc.maxIdle}"></property>
<property name="logAbandoned" value="${jdbc.logAbandoned}"></property>
<property name="removeAbandoned" value="${jdbc.removeAbandoned}"></property>
<property name="removeAbandonedTimeout" value="${jdbc.removeAbandonedTimeout}"></property>
</bean>
3.配置sessionFaction和配置映射文件
<!-- 配置SessionFactor -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource">
<ref bean="dataSource"></ref>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">${hibernate.dialect}</prop>
<prop key="hibernate.connection.autocommit">${hibernate.connection.autocommit}</prop>
<prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop>
<prop key="hibernate.format_sql">${hibernate.format_sql}</prop>
<prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
</props>
</property>
<!-- 指定Hibernate的映射文件 -->
<property name="mappingResources">
<list>
<value>com/zys/model/SysLogin.hbm.xml</value>
</list>
</property>
</bean>
在这步之前,需要在实体类里面创建映射文件并且给出实体类相关的getter,setter方法,实体类如下:
package com.zys.model;
public class SysLogin {
private int stunumber;
private String pwd;
private String id;
public int getStunumber() {
return stunumber;
}
public void setStunumber(int stunumber) {
this.stunumber = stunumber;
}
public String getPwd() {
return pwd;
}
public void setPwd(String pwd) {
this.pwd = pwd;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
}
映射文件内容如下:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.zys.model.SysLogin" table="SYS_LOGIN">
<id name="id">
<column name="ID" />
<generator class="assigned"/>
</id>
<property name="stunumber" type="java.lang.Integer">
<column name="stunumber"></column>
</property>
<property name="pwd" type="java.lang.String">
<column name="pwd"></column>
</property>
</class>
</hibernate-mapping>
第八步:在eclipse中配置tomcat7服务器
第九步:在web.xml下创建一个欢迎界面,用来测试SSH框架是否搭建成功,然后在webContent目录下创建一个index.jsp文件,结果如图:
代码如下:
<!-- 配置欢迎界面 -->
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
index.jsp页面如图:
代码如下:
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<h1>GOOD</h1>
</body>
</html>
欢迎界面如图:
另外,数据库中也应该自动创建一个名为sys_login的表,如图所示:
至此,SSH的框架搭建完成,至于如何利用SSH框架进行web后端开发将在接下来的博客中介绍。