SSH框架是JavaWeb轻量级整合开发框架,分别由Spring、Struts、Hibernate三部分内容融合在一起,从而让javaweb开发变得快捷、简单、高效。其他不多说,进入配置框架的正题吧:
我们在eclipse中新建一个动态工程:
点击“下一步”到如下图:
一定要勾选上图中的复选框,然后点击“完成”,得到如下的工程目录:
一个空的动态javaweb工程项目就建立好了。
然后,因为SSH框架需要相应的jar文件支持,我把资料文件放这里了:https://download.csdn.net/my/uploads/1/1
以上是jar文件截图(只是部分的),全部复制-粘贴到刚刚新建项目的/WebContent/WEB-INF/lib路径下:如图
我们先配置Hibernate的相关内容:
在我给的资料文件中找到下图中左侧的文件,复制粘贴到下图中右侧项目中相应位置:
在src目录下新建一个包(org.songle.pojo),包下新建一个类(Student),类中给出相应的成员属性,并生成属性的set()、get()方法:
把下图左侧选中的文件,复制-粘贴到下图右侧的src目录下:
Student.hbm.xml文件是对上面建立的Student类进行“类对象与关系数据表映射”配置
双击Student.hbm.xml文件打开进行编辑配置:
进行了以上配置之后,还需要把上图中的配置文件“注册”到项目中的hibernate.cfg.xml文件中:
注意上图中的注释部分,这里也要把“连接数据库相关的配置代码”注释掉。到此为止,Hibernate部分主要的已配置完。
下面接着上面的内容情况继续进行Spring的配置:
新建一个包名(org.songle.dao),在包下新建一个接口StudentDao,该接口中给出如下抽象方法:
再新建一个包(org.songle.dao.impl),包下新建一个类(StudentDaoImpl),该类需要继承HibernateDaoSupport类,且还有实现StudentDao接口:
点击“完成”,进行如下编辑:
把下图中左侧选中的文件复制-粘贴到右侧项目中的src目录下,
然后在项目中双击applicationContext.xml文件打开它进行配置编辑:
此处因配置文件篇幅太长不好截图,我直接贴代码:
开始《《《
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd " >
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/website?characterEncoding=utf-8"/>
<property name="username" value="root"/>
<property name="password" value=""/>
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="configLocation" value="classpath:hibernate.cfg.xml" />
</bean>
<bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="save*" propagation="REQUIRED"/>
</tx:attributes>
</tx:advice>
<aop:config>
<aop:pointcut expression="execution(* org.songle.dao.impl.*.*(..))" id="allMethod"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="allMethod"/>
</aop:config>
<bean id="hibernateTemplate" class="org.springframework.orm.hibernate5.HibernateTemplate">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<bean id="studentDao" class="org.songle.dao.impl.StudentDaoImpl">
<property name="hibernateTemplate" ref="hibernateTemplate"/>
</bean>
</beans>
》》》结束
接下来:准备两个jsp页面文件,为页面跳转实验做准备:如下图,放入/WebContent/WEB-INF/jsp路径下:
student-add.jsp与student-msg.jsp文件的代码,为了你们方便实验,这里直接给出代码:
student-add.jsp的代码:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>添加学生</title>
<link rel="stylesheet" type="text/css" href="css/bootstrap.min.css">
</head>
<body>
<div class="container">
<div class="row">
<div class="col-lg-3"></div>
<div class="col-lg-6">
<form action="" method="post">
<div class="card shandow">
<div class="card-header">
<h4>添加学生</h4>
</div>
</div>
<div class="card-body">
<div class="form-group">
<label>学号</label>
<input type="text" class="form-control" name="student.sid">
</div>
<div class="form-group">
<label>姓名</label>
<input type="text" class="form-control" name="student.name">
</div>
<div class="form-group">
<label>性别</label>
<input type="text" class="form-control" name="student.gender">
</div>
<div class="form-group">
<label>班级</label>
<input type="text" class="form-control" name="student.clazz">
</div>
</div>
<div class="card-footer text-right">
<input type="reset" value="重置" class="btn btn-secondary">
<input type="submit" value="添加" class="btn btn-primary">
</div>
</form>
</div>
<div class="col-lg-3"></div>
</div>
</div>
</body>
</html>
student-msg.jsp的代码:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<link rel="stylesheet" type="text/css" href="css/bootstrap.min.css">
</head>
<body>
<br/>
<div class="container">
<div class="row">
<div class="col-lg-3"></div>
<div class="col-lg-6">
<div class="card shadow">
<div class="card-header">
<h4>系统提示</h4>
</div>
<div class="card-body">
添加成功
</div>
<div class="card-footer text-right">
<a href="" class="btn btn-primary">继续添加</a>
</div>
</div>
</div>
<div class="col-lg-3"></div>
</div>
</div>
</body>
</html>
然后,把下图中左侧选中的文件复制-粘贴到右侧项目的src目录下:
然后新建一个包(org.songle.web),包下新建一个类BaseAction,该类要继承ActionSupport类,并且实现ServletRequestAware接口:
点击“完成”,在BaseAction类中做如下编辑:
再新建一个包(org.songle.web.action),包下新建一个类(StudentAction),该类需要继承之前建立的BaseAction类:
点击“finish”,作如下编辑:
在applicationContext.xml文件里再添加一个<bean>节点:
再在Struts.xml文件中做如下配置:
在student-add.jsp文件的表单处的action后写上student-save
在student-msg.jsp文件的超链接href后写上student-add
在如上目录的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>spring-hibernate</display-name>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<filter>
<filter-name>openSessionInViewFilter</filter-name>
<filter-class>org.springframework.orm.hibernate5.support.OpenSessionInViewFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>openSessionInViewFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</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>
知到如此SSH的框架已经搭建好,这里提醒,别忘了在自己的MySQL数据库中建立一个叫“website”的数据库。、、
把这次建立的项目MySSH部署到服务器中(我用的是Tomcat),然后启动Tomcat。在浏览器中运行:http://localhost:8080/MySSH/student-add.action 得到如下:
输入如下数据:
点击“添加” ,显示:
点击“继续添加”,又会显示:
再去打开MySQL数据库,会发现此时多生成了一个数据表myssh_student_info:
双击打开数据表,查看,此时一个学生数据已经添加进去了:
(此篇完毕,可供练习SSH框架搭建!编写不易,转载请注明出处,谢谢!)