ssh整合 小例子
实现了用户的查插删改操作。
原理:struts负责接收、分发请求。spring采用面向接口编程的思想整合整体框架,负责连接数据库。hibernate负责操作数据库语言。
思路:
1.配置struts的配置信息
2.创建struts和hibernate的关联(主要是加载jar包)
3.创建struts和spring的关联(主要是加载jar包)
4.编写页面信息。action
5.纵向开发模式(但是平时按照横向开发)。纵向开发好处是思路清晰,先写完dao层
service层action层编写模式
6.开始编写spring的配置文件。添加bean。采用依赖注入完成面向接口编程(面向切面编
程更准确些)。
7.编写struts的配置文件。将action映射到spring的配置文件中的bean中
防止表单重复提交(重定向)
好了,不多说了,用例子说话才是最权威的。
1.配置struts
a.wen.xml文件添加struts的过滤器、映射,spring的监听器
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
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_2_5.xsd"> <!-- 解决中文乱码问题 -->
<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- 解决中文乱码问题 --> <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> <listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener> </web-app>
b.struts的jar包支持
c.struts的配置文件 struts.xml(放在src目录下)
<?xml version="1.0" encoding="UTF-8" ?>
<!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.i18n.encoding" value="UTF-8"></constant>
<package name="ssh2" extends="struts-default">
<action name="saveUser" class="saveUserAction">
<result name="success" type="redirect">listUser.action</result>
</action> <action name="listUser" class="listUserAction">
<result name="success">/list.jsp</result>
</action> <action name="deleteUser" class="removeUserAction">
<result name="success" type="redirect">/listUser.action</result>
</action> <!--请求转发 -->
<action name="updatePUser" class="updatePUserAction">
<result name="success">/update.jsp</result>
</action> <!-- 重定向 -->
<action name="updateUser" class="updateUserAction">
<result name="success" type="redirect">/listUser.action</result>
</action>
</package> </struts>
2.导入hibernate的jar包支持,采用myeclipse自带的jar包
a.右击项目名称选择 myeclipse-->Add Hibernate Capabilities 如下图(为了将hibernate配置文件放置在src目录下,其实哪里都一样。。。)
b.点击next,不用向导配置完成后,打开配置文件
c.只是导入一些jar包,不需要连接数据库
d.不用创建session工厂
e.点击Finish完成
3.添加spring的jar包支持
同上面一样,导入jar包
4.编写页面信息、bean以及此bean的映射文件User.hbm.xml(放在com.test.bean目录下)
a.index.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="/struts-tags" prefix="s" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>"> <title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head> <body> <h1><font color="red">Operation List</font></h1> <s:a href="save.jsp">Sav e User</s:a><br/>
<s:a href="listUser.action">List Users</s:a>
</body>
</html>
b.创建bean
package com.test.bean; public class User { private int id;
private String firstname;
private String lastname;
private int age;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getFirstname() {
return firstname;
}
public void setFirstname(String firstname) {
this.firstname = firstname;
}
public String getLastname() {
return lastname;
}
public void setLastname(String lastname) {
this.lastname = lastname;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
3.bean的映射文件User.hbm.xml
<?xml version="1.0"?>
<!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.test.bean.User" table="users">
<id name="id" column="id" type="int">
<generator class="increment"></generator>
</id> <property name="firstname" column="firstname" type="string"></property>
<property name="lastname" column="lastname" type="string"></property>
<property name="age" column="age" type="int"></property>
</class>
</hibernate-mapping>
6.因为要良好封装,所以从dao层开始编写
a.dao层接口
package com.test.dao.impl; import java.util.List; import org.springframework.orm.hibernate3.support.HibernateDaoSupport; import com.test.bean.User;
import com.test.dao.UserDAO; public class UserDAOImpl extends HibernateDaoSupport implements UserDAO
{ public void saveUser(User user) {
this.getHibernateTemplate().save(user);
} @SuppressWarnings("unchecked")
@Override
public List<User> findAllUsers() {
String hql = "from User user order by user.id desc";
return (List<User>)this.getHibernateTemplate().find(hql);
} @Override
public void removeUser(User user) {
this.getHibernateTemplate().delete(user);
} @Override
public void updateUser(User user) {
this.getHibernateTemplate().update(user);
} @Override
public User findUserById(Integer id) { User user = (User)this.getHibernateTemplate().get(User.class, id);
return user;
} }
b.dao层实现类
package com.test.dao.impl; import java.util.List; import org.springframework.orm.hibernate3.support.HibernateDaoSupport; import com.test.bean.User;
import com.test.dao.UserDAO; public class UserDAOImpl extends HibernateDaoSupport implements UserDAO
{ public void saveUser(User user) {
this.getHibernateTemplate().save(user);
} @SuppressWarnings("unchecked")
@Override
public List<User> findAllUsers() {
String hql = "from User user order by user.id desc";
return (List<User>)this.getHibernateTemplate().find(hql);
} @Override
public void removeUser(User user) {
this.getHibernateTemplate().delete(user);
} @Override
public void updateUser(User user) {
this.getHibernateTemplate().update(user);
} @Override
public User findUserById(Integer id) { User user = (User)this.getHibernateTemplate().get(User.class, id);
return user;
} }
7.编写service层
a.service的接口
package com.test.service; import java.util.List; import com.test.bean.User; public interface UserService { public void save(User user); public List<User> findAll(); public void delete(User user); public User findById(Integer id); public void update(User user); }
b.service的实现类
package com.test.service.impl; import java.util.List; import com.test.bean.User;
import com.test.dao.UserDAO;
import com.test.service.UserService; public class UserServiceImpl implements UserService
{
private UserDAO userDAO; public UserDAO getUserDAO() {
return userDAO;
} public void setUserDAO(UserDAO userDAO) {
this.userDAO = userDAO;
} @Override
public void save(User user) { this.userDAO.saveUser(user);//完成真正的业务逻辑
} @Override
public List<User> findAll() {
return this.userDAO.findAllUsers();
} @Override
public void delete(User user) {
this.userDAO.removeUser(user);
} @Override
public User findById(Integer id) {
User user = this.userDAO.findUserById(id);
System.out.println(user);
return user;
} @Override
public void update(User user) { this.userDAO.updateUser(user);
}
}
8.action的实现
a.完成的是显示全部用户信息功能的action
package com.test.action.user; import java.util.List;
import java.util.Map; import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.test.bean.User;
import com.test.service.UserService; public class ListUserAction extends ActionSupport { private UserService service; public UserService getService() {
return service;
} public void setService(UserService service) {
this.service = service;
} @SuppressWarnings("unchecked")
@Override
public String execute() throws Exception { Map request = (Map)ActionContext.getContext().get("request");
request.put("list", this.service.findAll());
return SUCCESS;
}
}
b.根据id删除用户信息
package com.test.action.user; import com.opensymphony.xwork2.ActionSupport;
import com.test.bean.User;
import com.test.service.UserService; public class RemoveUserAction extends ActionSupport { private User user;
private UserService service;
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
public UserService getService() {
return service;
}
public void setService(UserService service) {
this.service = service;
} @Override
public String execute() throws Exception {
this.service.delete(user);
return SUCCESS;
}
}
c.添加用户信息
package com.test.action.user; import com.opensymphony.xwork2.ActionSupport;
import com.test.bean.User;
import com.test.service.UserService; public class SaveUserAction extends ActionSupport { private User user;
private UserService service; public UserService getService() {
return service;
} public void setService(UserService service) {
this.service = service;
} public User getUser() {
return user;
} public void setUser(User user) {
this.user = user;
} @Override
public String execute() throws Exception { //调用service相关的方法,完成实际的业务逻辑
//action只负责收集数据,验证校验
this.service.save(this.user);
return SUCCESS;
}
}
d.通过ID查找用户信息
package com.test.action.user; import com.opensymphony.xwork2.ActionSupport;
import com.test.bean.User;
import com.test.service.UserService; public class UpdatePUserAction extends ActionSupport { private User user;
private UserService service;
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
public UserService getService() {
return service;
}
public void setService(UserService service) {
this.service = service;
} @Override
public String execute() throws Exception { this.user = this.service.findById(this.user.getId());
return SUCCESS;
}
}
e.通过ID修改用户信息
package com.test.action.user; import com.opensymphony.xwork2.ActionSupport;
import com.test.bean.User;
import com.test.service.UserService; public class UpdateUserAction extends ActionSupport { private User user;
private UserService service;
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
public UserService getService() {
return service;
}
public void setService(UserService service) {
this.service = service;
} @Override
public String execute() throws Exception {
this.service.update(user);
return SUCCESS;
}
}
9.添加spring的配置信息
bean的实现(放在WEB-INF目录下)
<?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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close"> <property name="driverClassName">
<value>com.mysql.jdbc.Driver</value>
</property> <property name="url">
<value>jdbc:mysql://localhost:3306/hibernate</value>
</property> <property name="username">
<value>root</value>
</property> <property name="password">
<value>123456</value>
</property>
</bean> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <property name="dataSource">
<ref local="dataSource"/>
</property> <property name="mappingResources">
<list>
<value>com/test/bean/User.hbm.xml</value>
</list>
</property> <property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
org.hibernate.dialect.MySQLDialect
</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
</bean> <bean id="userDao" class="com.test.dao.impl.UserDAOImpl" scope="singleton">
<property name="sessionFactory">
<ref bean="sessionFactory"/>
</property>
</bean> <bean id="userService" class="com.test.service.impl.UserServiceImpl" scope="singleton">
<property name="userDAO" ref="userDao" />
</bean> <bean id="saveUserAction" class="com.test.action.user.SaveUserAction" scope="prototype">
<property name="service" ref="userService"></property>
</bean> <bean name="listUserAction" class="com.test.action.user.ListUserAction" scope="prototype">
<property name="service" ref="userService"></property>
</bean> <bean name="removeUserAction" class="com.test.action.user.RemoveUserAction" scope="prototype">
<property name="service" ref="userService"></property>
</bean> <bean name="updateUserAction" class="com.test.action.user.UpdateUserAction" scope="prototype">
<property name="service" ref="userService"></property>
</bean> <bean name="updatePUserAction" class="com.test.action.user.UpdatePUserAction">
<property name="service" ref="userService"></property>
</bean>
</beans>
10.struts.xml(放在src目录下)
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd"> <struts> <package name="ssh2" extends="struts-default">
<action name="saveUser" class="saveUserAction">
<result name="success" type="redirect">listUser.action</result>
</action> <action name="listUser" class="listUserAction">
<result name="success">/list.jsp</result>
</action> <action name="deleteUser" class="removeUserAction">
<result name="success" type="redirect">/listUser.action</result>
</action> <!--请求转发 -->
<action name="updatePUser" class="updatePUserAction">
<result name="success">/update.jsp</result>
</action> <!-- 重定向 -->
<action name="updateUser" class="updateUserAction">
<result name="success" type="redirect">/listUser.action</result>
</action>
</package> </struts>
11.页面展示
a.list.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<%@ taglib uri="/struts-tags" prefix="s"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>"> <title>My JSP 'list.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
<SCRIPT type="text/javascript">
function del()
{
if(comfirm("are you sure?"))
{
return true;
}
return false;
}
</SCRIPT>
</head> <body>
<h1><font color="red"><center>User List</center></font></h1> <table border="1" width=80% align="center">
<tr>
<td>序号:</td>
<td>姓:</td>
<td>名:</td>
<td>年龄:</td>
<td>删除:</td>
<td>更新:</td>
</tr> <s:iterator value="#request.list" id="us">
<tr>
<td>
<s:property value="#us.id"/>
</td>
<td>
<s:property value="#us.firstname"/>
</td>
<td>
<s:property value="#us.lastname"/>
</td>
<td>
<s:property value="#us.age"/>
</td>
<td>
<s:a href="deleteUser.action?user.id=%{#us.id}" onclick="return del();">delete</s:a>
</td>
<td>
<s:a href="updatePUser.action?user.id=%{#us.id}">update</s:a>
</td>
</tr>
</s:iterator> </table>
</body>
</html>
b.save.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="/struts-tags" prefix="s"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>"> <title>My JSP 'save.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
--> </head> <body> <h1><font color="red">Operation User</font></h1> <s:form action="saveUser">
<s:textfield name="user.firstname" label="firstname"></s:textfield>
<s:textfield name="user.lastname" label="lastname"></s:textfield>
<s:textfield name="user.age" label="age"></s:textfield> <s:submit value="submit"></s:submit> </s:form> </body>
</html>
c.update.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>"> <title>My JSP 'update.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
--> </head> <body> <h1><font color="red"><center> Update User</center></font></h1> <s:form action="updateUser"> <table>
<tr>
<td>
<s:hidden name="user.id" value="%{user.id}"></s:hidden>
</td>
<td>
<s:textfield name="user.firstname" value="%{user.firstname}"></s:textfield>
</td>
<td>
<s:textfield name="user.lastname" value="%{user.lastname}"></s:textfield>
</td>
<td>
<s:textfield name="user.age" value="%{user.age}"></s:textfield>
</td>
<td>
<s:submit value="submit"></s:submit>
</td>
</tr>
</table>
</s:form>
</body>
</html>
总体的框架
lib目录下jar包信息
好了完工。。。。坐等评论。
ssh整合 小例子的更多相关文章
- SSH整合简单例子
说明:简单SSH整合,struts版本2.3.32,spring版本3.2.9,hibernate版本3.6.10 一.开发步骤 1 引jar包,创建用户library.使用的包和之前博文相同,可以参 ...
- web学习:Spring2.5+Hibernate3.3+Struts1.3整合小例子
写android有段时间了,感觉思维越写越狭窄,考虑问题越来越局限了,看不到一个项目整体的设计和构架,觉得很有必要多多写一些大型的框架性的东西来提高自己的视野. 从接触java到现在一年多了,在我的印 ...
- spring小例子-springMVC+mybits整合的小例子
这段时间没更博,找房去了... 吐槽一下,自如太坑了...承诺的三年不涨房租,结果今年一看北京房租都在涨也跟着涨了... 而且自如太贵了,租不起了.. 突然有点理解女生找对象要房了.. 搬家太 ...
- 三大框架SSH整合
三大框架SSH整合 -------------------------------Spring整合Hibernate------------------------------- 一.为什么要整合Hi ...
- SSH整合之全注解
SSH整合之全注解 使用注解配置,需要我们额外引入以下jar包
- SSH整合方案2
[案例3]SSH整合_方案2 ** 案例描述 两个知识点的演示 其一,SSH整合的第二个方案 其二,Spring+JDBC+Struts2 参考代码 31) 使用工程spring4 32 ...
- Spring自学教程-ssh整合(六)
以下是本人原创,如若转载和使用请注明转载地址.本博客信息切勿用于商业,可以个人使用,若喜欢我的博客,请关注我,谢谢!博客地址 感谢您支持我的博客,我的动力是您的支持和关注!如若转载和使用请注明转载地址 ...
- Spring.Net在ASP.NET Mvc里使用的一个小例子
就贴个小例子,就不注意格式了. 1.下载dll NuGet的下载地址:http://docs.nuget.org/docs/start-here/installing-nuget 在vs的NuGet里 ...
- 【Java EE 学习 67 下】【OA项目练习】【SSH整合JBPM工作流】【JBPM项目实战】
一.SSH整合JBPM JBPM基础见http://www.cnblogs.com/kuangdaoyizhimei/p/4981551.html 现在将要实现SSH和JBPM的整合. 1.添加jar ...
随机推荐
- Nessus虚拟机的几个问题解决办法
1.使用ppp的校园网或者家庭宽带无法通过桥接上网. 这时要把这俩网卡变成NAT模式就行. 2.国外下载插件包(或者过慢). 我这里贡献个高速链接.base64,懂得自然懂. c3NyOi8vTkRj ...
- 深度排序与alpha混合 【转】
翻译:李现民 最后修改:2012-07-03 原文:Depth sorting alpha blended objects 先说个题外话,本来我想回答在 Creators Club论坛上的一个常见 ...
- 2.SOAP 语法
SOAP 消息的基本结构 <?xml version="1.0"?> <soap:Envelope xmlns="http://www.w3.org/2 ...
- run kubernetes
use kubeadm in lab only,,, do not use it in production install docker 1.12.6 -.. please noted ...
- 完美删除vector的内容与释放内存
问题:stl中的vector容器常常造成删除假象,这对于c++程序员来说是极其讨厌的,<effective stl>大师已经将之列为第17条,使用交换技巧来修整过剩容量.内存空洞这个名词是 ...
- 串匹配算法之BM算法
参考资料: http://blog.csdn.net/eric491179912/article/details/6210009 http://blog.163.com/pengfeicui@ye ...
- Oracle oledb 打包并集成到程序安装包(VC ADO訪问Oracle)
近期有一个项目和oracle交互,我用的是ADO的方式进行试问操作. 首先把数据库连接的东东记录一下,老了记性不好啊! 操作例如以下: //连接串这么写的话就能够不用配置 tnsnames.ora配置 ...
- nyoj84 阶乘的0
阶乘的0 时间限制:3000 ms | 内存限制:65535 KB 难度:3 描写叙述 计算n!的十进制表示最后有多少个0 输入 第一行输入一个整数N表示測试数据的组数(1<=N<=1 ...
- ssh 保持连接
ssh 保持连接 使用 ssh 登陆到云主机上,一段时间没有操作终端,会发现 ssh 连接断了,终端无响应. 配置 ssh 客户端,使其以一定间隔时间向 sshd 服务端发送心跳包,可解决此问题. / ...
- Dash 使用
花了 160 买了这个软件,至少看一遍它的 user guide,钱不能白花. https://kapeli.com/guide/guide.html 设置全局快捷键 Preference -> ...