Struts+HIbernate+Spring
1.Struts
取代JSP中的控制功能,为系统添加独立的控制,采用Struts引入标签,实现JSP与后台JAVA代码的分离,JSP只负责显示,与struts.xml配合实现页面跳转
实现:接收请求,调用业务逻辑组件,返回HTTP响应
Struts中控制器由底层的一个FilterDispatcher(较老)负责实现,FilterDispatcher类负责接收用户的请求并最终转发用户的请求。
在web.xml中配置:
<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee" XML(标准通用标记语言的子集)命名空间,赋予命名空间一个惟一的名称。不过,很多公司常常会作为指针来使用命名空间指向实际存在的网页,这个网页包含关于命名空间的信息,不同元素可在不同命名控件
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" 多个 XML 架构文档提供位置信息
version="3.0"
metadata-complete="true"> 由部署描述符为Web应用程序提供所有的配置信息
<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>
在struts.xml中配置:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" 文档声明,在struts-core-2.0.11.2.jar中的struts2.0dtd文件中复制
"http://struts.apache.org/dtds/struts-2.0.dtd"> 源码库
<struts>
<constant name="struts.objectFactory" value="spring"/>
<constant name="struts.devmode" value="true"/> 是否使用开发模式
<constant name="struts.i18n.encoding" value="GBK"/>
<package name="default" extends="struts-default" namespace="/"> 包名,要继承的包,命名控件,namespace=“dogs”,则dogs/garfield优先在dogs下找,后去默认命名控件下找
<action name="information_*" method = "{1}" class="com.web.action.InformationAction"> method = "add"则调用add方法
<result name="main" type="chain">main</result>
<result name="list">/informationList.jsp</result>
<result type="chain">informationList</result>
<result name="success">/informationList.jsp</result>
<result name="detail">/informationDetail.jsp</result>
<result name="input">/addInformation.jsp</result>
<result name="edit">/editInformation.jsp</result>
</action>
</package>
</struts>
有了Action类(com.web.action.InformationAction),取代在客户端的所有表单提交对象(JSP),Struts.xml根据Action类返回值选择跳转至目的页面,Action类继承ActionSupport类,必须重载 public String execute() throws Exception{} 和 可重载 public void validate(){},当没有指名method默认先执行validate(),execute(),method=“{1}”时,根据name="information_*",调用Action类中的方法*。
是在struts2调用完拦截器栈之后,调用响应Action的execute方法前
详细的说:struts2在接受一个请求之后,会建立相应请求的Action对象,同时生成管理其上下文的ActionContext对象,并将javabean(即Action对象)
的属性放到Valuestack栈顶,将这些属性初始化,之后struts2会调用拦截器栈中的拦截器(注意这些拦截器会改变valuestack中属性的值,如params拦
截器会完成将表单字段映射到valuestack属性上。。。),在调用结束后,struts2会将valuestack中的属性映射赋值给Action对象属性,
最后调用action方法。
Action类中的
public String execute() throws Exception{
this.Message="Hello, World";
ActionContext context = ActionContext.getContext();
Map session = context.getsession();
session.put("message", message);
return success;
}
JSP中,可以使用<%=request.getssion.getAttribute("message").toString%>,也可以使用<s:property value="??.message"/>
<%=request.getssion().getAttribute("message").toString%>使用Struts2.0方式给session存取数据,却用request对象直接访问session对象读取数据
<s:property value="??.message"/>通过Struts2.0的数据绑定机制,传递??.message等同与Action.get??().getMessage();
package机制用来统一组织和管理Action,你的浏览器地址是根据namespace/action名称 访问的 这个namespace是虚拟路径,可以在项目中完全不存在。随便取但是我在项目中为了方面查找jsp页面,会将jsp文件的路径与namespace想匹配。
知识点梳理:
1.使用Action接收用户句表单提交上来的参数
2.在Action中访问request/session/application对象
3.package的配置和名称空间的配置,采用累死与JAVA中package的机制统一组织管理Action
4.Action的配置,包括method属性,动态方法的调用及默认Action配置
5.Result的配置
建议:
1.配置class的默认路径为Project/WebRoot/WEB-INF/classes
2.web.xml放置在Project/WebRoot/WEB-INF/下
与JSP区别:
1.页面跳转使用Action
2.数据的读取存储使用领域对象(Action对象)
3.与selevet API 可耦合和解耦合
解耦合:
ActionContext context = ActionContext.getContext();
Map request = (Map) context.get("request");
Map response = (Map) context.get("response");
Map application = (Map) context.getApplication();
耦合:
HttpServletRequest request = ServletActionContext.getRequest();
HttpServletResponse response = ServletActionContext.getResponse();
ServletContext application = ServletActionContext.getServletContext();
2.HIbernate 数据库操作
1.书写类的映射文件
<?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"> XML文件格式的验证文件位置,用于检验XML文件格式是否正确
<hibernate-mapping>
<class name="com.web.domain.Comment" table="comment"> 需要存储的类名,对应的表名
<id name="id" length="32" column="id" type="java.lang.String"> 属性id由hibernate自动生成
<generator class="uuid.hex">
</generator>
</id>
<property 属性
name="title"
type="java.lang.String"
column="title"
length="64"
not-null="false" 可空
/>。。。
Hibernate的配置文件:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.url"> 数据库地址及数据库名称
jdbc:mysql://localhost:3306/community
</property>
<property name="hibernate.connection.driver_class"> 数据库驱动类
com.mysql.jdbc.Driver
</property>
<property name="hibernate.connection.username">root</property> 访问数据库的用户名及密码
<property name="hibernate.connection.password">********</property>
<!-- 指定映射文件 -->
<mapping resource="Chapter04/Apple.hbm.xml"/>
</session-factory>
</hibernate-configuration>
使用Hibernate:
public static void main(String[] args) {
Configuration configuration = new Configuration(); 读取hibernate.properties或者hibernate.cfg.xml
configuration = configuration.configure();
SessionFactory factory = configuration.buildSessionFactory(); 打开与数据库会话,并开始事务
Session session = factory.openSession();
Transaction transaction = session.beginTransaction();
。。。。
session.save(apple2);
transaction.commit();
session.close();
}
查询:session.get(Apple.Class, id);也可以使用HSQL语句:hsql="FROM User WHERE user = 'admin'"; HIbernate.getSessionFactory.getCurrentSession().createQuery(hsql).list();
当将三个框架一起使用是,无需Hibernate配置文件hibernate.cfg.xml,在applicationContext中配置JAVA BEAN
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost/web"/>
<property name="username" value="root"/>
<property name="password" value="chenhaibin"/>
</bean>
使用时:this.getHibernateTemplate().save(comment);实现保存,也可以使用HSQL实现查询等:String hsql = "from comment where title = '" + title + "'";
return this.getHibernateTemplate().find(hsql);
Spring:
通过XML<bean/>元素能够声明受管POJO,通过设值方法(Setter)和构造器能够完成其依赖对象
IOC(控制反转):Spring从程序员手中接管实例化对象和注入协作对象(协作bean),控制由程序员手中转移给Spring IOC容器,控制发生了根本性的反转。
DI(依赖注入):两个或多个对象(UserService,UserDAO)协同工作,其中一个(UserService)需要另一个(UserDAO)协作来完成任务,这种协作通过设置属性的方式来实现(UserService中设置UserDAO属性)。给UserService中的UserDAO属性赋值的过程,成为依赖注入。被注入对象(UserDAO)称为协作bean,接收注入的对象(UserService)被称为依赖bean。
设置ApplicationContext:
<?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: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-2.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.0.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/web"/>
<property name="username" value="root"/>
<property name="password" value="chenhaibin"/>
</bean>
<bean id="userService" class="com.web.service.imp.UserService">
<property name="userDAO">
<ref local="userDAOProxy"/>
</property>
<!-- <property name="userDAO">
<ref local="userDAO"/>
</property> -->
</bean>
实例化:
String[] files = new String[] {"applicationContext.xml"};
ApplicationContext context = new ClassPathXmlApplicationContext(files);
UserService userService = (UserService) context.getBean("userService");
String ID = userService.getUserIdByName(userName);
Struts+HIbernate+Spring的更多相关文章
- 三大框架:Struts+Hibernate+Spring
三大框架:Struts+Hibernate+Spring Java三大框架主要用来做WEN应用. Struts主要负责表示层的显示 Spring利用它的IOC和AOP来处理控制业务(负责对数据库的操作 ...
- Maven+Struts+Hibernate+Spring简单项目搭建
这段时间学习如何使用Maven+Struts+Hibernate+Spring注解方式建立项目,在这里感谢孙宇老师. 第一次写博客,主要是方便自己查看,同时分享给大家,希望对大家有所帮助,我也是 ...
- 详谈Struts+Hibernate+Spring三大框架
前言:对于JAVA WEB端的程序员来说,对JAVA三大框架:Struts+Hibernate+Spring的了解必不可缺,下面详细谈谈 Java三大框架主要用来做WEN应用. 三大框架:Struts ...
- Struts+Hibernate+Spring面试题合集及答案
Struts+Hibernate+Spring面试题合集及答案 Struts+Hibernate+Spring面试题合集 1 1. Hibernate部分 2 1.1. Hibernate工作原理 2 ...
- Struts+Hibernate+Spring面试题合集及答案(转)
Struts+Hibernate+Spring面试题合集及答案 Struts+Hibernate+Spring 面试题合集 1 1. Hibernate部分 2 1.1. Hibernate工作原理 ...
- Struts+Hibernate+Spring实现用户登录功能
通过登录案例实现三大框架之间的整合,登录功能是任何系统和软件必不可少的一个模块,然而通过这个模块来认识这些复杂的框架技术,理解数据流向和整个设计思路是相当容易的.只有在掌握了这些小模块的应用后,才能轻 ...
- ssh repo ----> struts+hibernate+spring( jar包和源码)各版本下载链接
struts http://archive.apache.org/dist/struts/ hibernate http://hibernate.org/orm/releases/5.0/ sprin ...
- Struts/Hibernate/Spring源码下载
Struts: https://olex.openlogic.com/packages/struts Hibernate: https://olex.openlogic.com/packages/hi ...
- Struts hibernate Spring 框架原理
转自:http://www.cnblogs.com/javaNewegg/archive/2011/08/28/2156521.html 原理:1.通过Configuration().configur ...
随机推荐
- Linux CGI编程基础【整理】
Linux CGI编程基础 1.为什么使用CGI? 如前面所见,任何的HTML均是静态网页,它无法实现一些复杂的功能,而CGI可以为我们实现.如:a.列出服务器上某个目录中的文件,对目录中的文件进行操 ...
- 137.Single Number II---位运算---《剑指offer》40
题目链接:https://leetcode.com/problems/single-number-ii/description/ 题目大意:给出一串数,每个数都出现三次,只有一个数只出现一次,把这个出 ...
- vuex实例详解
vuex是一个专门为vue.js设计的集中式状态管理架构.状态?把它理解为在data中的属性需要共享给其他vue组件使用的部分. 简单的说就是data需要共用的属性 一.小demo 已经用Vue脚手架 ...
- chkconfig命令主要用来更新(启动或停止)和查询系统服务的运行级信息
chkconfig命令主要用来更新(启动或停止)和查询系统服务的运行级信息.谨记chkconfig不是立即自动禁止或激活一个服务,它只是简单的改变了符号连接. 使用语法:chkconfig [--ad ...
- 自家人不认识自家人——考你一道有趣的Javascript小题目
今天的内容很简单,给大家分享一个有趣的Javascript小题目. 题目很简单,就是填空: var a = ______; var b = a; alert(a==b); // alert " ...
- CSU 1102 多连块拼图
多连块拼图 时间限制:1000 ms | 内存限制:65535 KB 难度:4 描述 多连块是指由多个等大正方形边与边连接而成的平面连通图形. ———— 维基百科 ...
- 解决类似 /usr/lib64/libstdc++.so.6: version `GLIBCXX_3.4.21' not found 的问题
https://itbilu.com/linux/management/NymXRUieg.html
- vuejs学习——vue+vuex+vue-router项目搭建(二)
前言 最近比较忙,所有第二章发布晚了,不好意思各位. vuejs学习——vue+vuex+vue-router项目搭建(一) 中我们搭建好了vue项目,我相信大家已经体验了vue其中的奥妙了,接下来我 ...
- linux命令---查找文件中的内容
linux命令---查找文件中的内容 [yang@localhost ~]$ cat 1.txt |egrep '123456789|second'-------匹配123456789或者seco ...
- karma+seajs
下面的介绍以karma能正常运行为前提,看karma系列文章:http://www.cnblogs.com/laixiangran/tag/Karma/ 目录结构 步骤 安装 npm install ...