ssh为 struts+spring+hibernate的一个集成框架,是目前较流行的一种Web应用程序开源框架html
struts :是一个基于MVC设计模式的Web应用框架,struts相似.net mvc中的控制器 (Controller)java
spring :管理对象的生命周期,建立,装配和配置等;它的特性就是 ,DI (依赖注入)和 AOP(面向切面设计),上一章已经讲过,它也是ssh中最难的,说白了就是bean的注入;web
hibernate :数据库映射框架,对JDBC进行了很是轻量级的对象封装spring
从职责上分为 表示层 ,业务逻辑层,数据持久层 和 实体层sql
准备工做:下载环境(下载 struts ,spring ,hibernate),eclipse默认不包含这些东西;数据库
我整合了一个ssh的jar包,解压后解决可用:整合的ssh架包.rar apache
连接:https://pan.baidu.com/s/1bahlwgoM3w8ehXzMR78_yg 提取码:hyat设计模式
(mvc
固然你也能够分别去官网下载(不推荐):app
1:下载struts struts-2.3.35-apps.zip
官网地址: http://struts.apache.org/download.cgi#struts252
2:下载spring spring-framework-4.2.2.RELEASE-dist.zip
官网地址: https://repo.spring.io/webapp/#/artifacts/browse/tree/General/libs-release-local/org/springframework/spring/4.2.2.RELEASE/spring-framework-4.2.2.RELEASE-dist.zip
下面讲怎么把下载的包引用到项目中:
第一步:直接在eclipse中建立一个web应用程序 ,我给他取名叫ssh
file----new ----other ---web---Dynamic web project ----next----取名ssh,next ----勾上“Generate web.xml deployment descriptor” ---finis
第二步:
最后我解压的路径是:E:\Program Files\Java\整合的ssh架包
把里面的jar包直接copy 到项目中 ssh---webContent----Web-InF----lib下面
第三步:若是要连j数据库,就须要配置dbc,就是把 sqljdbc42.jar 也copy到项目中
个人路径是: E:\Program Files\Java\jdbc\sqljdbc_6.0\chs\jre8\sqljdbc42.jar 也copy到项目中就行了
配置XML文件
1:新建4个包:new---package---输入名称就能够创建;
(我在创建的过程当中发现包不在指定位置上,按F5刷新就能够解决了)
2:新建2个xml文件 ,new----Other----XML----XML File--Next---输入名称----Finish
applicationContext.xml (spring配置用)
struts.xml (Struts配置用)
这2个xml的内容先不用管他,后面会告诉你应该加些什么:
3:修改 ssh---WebContent----Web-INF-- web.xml, 修改内容以下:
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1"> <display-name>ssh</display-name> <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> <!-- struts2的过滤器 --> <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>/*</url-pattern> </filter-mapping> <!-- spring的监听器配置开始 --> <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> </web-app>
仔细看你会发现,主要加了几个标签 :
<filter> <filter-mapping> 主要是struts2 的过滤器设置
<context-param> <listener> Spring的监听器
看不懂不要紧,直接复制粘贴就行了,你只须要知道这些配置文件就是告诉了eclipse,须要使用 Struts 和 Spring,并把这些包的位置或配置文件的位置告诉了eclipse;
开始写代码
在以前新建的4个包下面,分别新建4个java文件,new----Class---输入名称---Finish
咱们以产品Product为例子,因此新建4个java文件为:
ProductAction.java (界面层)
ProductService.java(业务逻辑服务层)
ProductDao.java(数据层)
Product.java(model实体层)
Product.java的代码以下:
package ssh.domain; public class Product { private Integer pid; private String pname; private Double price; public Integer getPid() { return pid; } public void setPid(Integer pid) { this.pid = pid; } public String getPname() { return pname; } public void setPname(String pname) { this.pname = pname; } public Double getPrice() { return price; } public void setPrice(Double price) { this.price = price; } }
productDao.java
package ssh.dao; import ssh.domain.Product; public class ProductDao { public void save(Product product) { System.out.println("DAO中的Save方法执行了"); } }
productService.java
package ssh.service; import ssh.dao.ProductDao; import ssh.domain.Product; public class ProductService { private ProductDao productDao; public void setProductDao(ProductDao productDao) { this.productDao = productDao; } public void save(Product product) { System.out.println("Service 中的 Save 方法执行了..."); productDao.save(product); } }
productAction.java的代码以下:
package ssh.action; import com.opensymphony.xwork2.ActionSupport; import com.opensymphony.xwork2.ModelDriven; import ssh.service.ProductService; import ssh.domain.Product; public class ProductAction extends ActionSupport implements ModelDriven<Product> { private Product product = new Product(); @Override public Product getModel() { return product; } private ProductService productService; public void setProductService(ProductService productService) { this.productService = productService; } public String Save() { System.out.println("Action 中的 Save 方法执行了..."); productService.save(product); return NONE; } }
总之呢,你应该知道 product 就是一个model实体层,这很容易理解把。
而后是dao,他就相似数据库操做层,这里咱们没连数据库,因此用 System.out.println 来测试,后面会把链接数据库的代码补齐;一口气吃太多,怕会晕掉。
service 业务逻辑层,他调用了数据层,
action是界面层,他调用了服务层,action直接跟jsp打交道,相似.net的mvc~~ Save方法就比如一个action.
至于各个方法的set赋值是什么意思呢?为何没看到调用? ssh把它封装在配置文件中了,这也是spring的依赖注入的一个特色;
下面咱们接着来配置
applicationContext.xml (spring配置用)
struts.xml (Struts配置用)
applicationContext.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: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"> <bean id="productAction" class="ssh.action.ProductAction" scope="prototype"> <!-- 手动注入Service --> <property name="productService" ref="productService" ></property> </bean> <bean id="productService" class="ssh.service.ProductService"> <property name="productDao" ref="productDao"></property> </bean> <bean id="productDao" class="ssh.dao.ProductDao"> </bean> </beans>
这段xml,前面的你不用管,他是spring的一些装配信息,直接copy就行了;你要注意的是下面的三个<bean>标签:
关于bean标签的 id,请你随便取,class 就是咱们新建的那3个类 的路径,这个很是好理解吧?
至于property:
<property name="productService" ref="productService" ></property>
property就是属性成员的意思
name就是这个属性的名称,让咱们回到ProductAction.java,看到下面的图了吗?这个就是name
ref是什么?让咱们回到applicationContext.xml ,看下面的图就知道了吧?是其它bean的id,说白了就是把下面的那个bean做为参数传给了set方法,set方法在上图能够看到;
在eclipse中 set和get是能够自动生成的,请不要和.net同样本身写,本身写ssh是不认识的哦;
怎样自动生成属性的get和set? 右键属性名----Source----Generate Getters and Setters....
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> <package name="ssh" extends="struts-default" namespace="/"> <action name="product_*" class="productAction" method="{1}"></action> </package> </struts>
package标签,里面配置了信息, 直接copy就行了
由于咱们的项目是product,因此name用 product_* 就行了;至于为何要用_ ?这是在jsp的from表单中的action属性中写死的规定!死记硬背就好!一会到jsp页面你就知道了!
class的值是 productAction ,对应的是什么?它对应的是 applicationContext.xml 中的action类的id;以下图,让咱们回顾下applicationContext.xml
method="{1}"写死就好,初学者不作过多的计较;
你只要这样理解就行了,struts.xml 实际上是对Action动做作了规定,而且它关联了spring的配置文件applicationContext.xm,
( ref 直接指向了spring配置文件的bean的id值, 主要是根据这个id获取到它的class,也就是ssh.action.ProductAction,固然ref直接等于ssh.action.ProductAction也是能够的,可是这种写法不支持spring的aop面向切面设计 )
运行测试
好了,上面搞了那么多,咱们新建了2个xml,和4个java文件,这些都是为jsp页面服务的,下面咱们新建jsp页面:
ssh---WebContent----右键----new---JSP File---命名:AddProduct.jsp----Finish
addProduct.jsp 代码以下:
<?xml version="1.0" encoding="utf-8" ?>
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<%@ taglib uri="/struts-tags" prefix="s" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<title>Insert title here</title>
</head>
<body>
<h1>保存商品的页面</h1>
<s:form action="product_Save" method="post" namespace="/" theme="simple">
<table border="1" width="400">
<tr>
<td>商品名称</td>
<td><s:textfield name="pname"></s:textfield></td>
</tr>
<tr>
<td>商品名称</td>
<td><s:textfield name="price"></s:textfield></td>
</tr>
<tr>
<td colspan="2"><input type ="submit" value="添加"></input></td>
</tr>
</table>
</s:form>
</body>
</html>
<%@ taglib uri="/struts-tags" prefix="s" %> 是什么意思呢?
s是你随便取的,其它的你写死就好,你只需知道这句代码是引用了struts的一个模板,这是模板引用的写法;
<s:form action="product_Save" method="post" namespace="/" theme="simple"> 是什么意思呢?
s:form就是from标签,只不过用了模板的写法,s是你以前本身随便取的名字;
action是动做的意思,也就是告诉你提交的时候会提交到哪里去? product_Save 怎么理解呢? 你还记得Struts的配置文件吗?<action name="product_*" class="productAction" method="{1}"></action>
由于你是product_Save,因此你符合了product_*,因此你会走class="productAction",也就是ssh.action.ProductAction这个文件里面了,_Save会执行他的Save方法,为何?这是写死的!反正_后面的就是方法;
method="post" post提交
namespace="/" theme="simple" 不须要理解,死记硬背就好
而后终于接完了,咱们打开jsp页面,run as ----run on server ---finish 看效果
点击添加按钮
终于完成了;
下一章咱们将继续学习:
使用eclipse 搭建 ssh框架二( struts spring hibernate )