- 浏览: 7516 次
- 性别:
- 来自: 北京
文章分类
最新评论
-
winter13292:
谢谢
ssh 登录例子(lib里的包已经清除) -
zgyxzgyx123:
病毒名
账户添加成功
钓鱼品牌 Top N
网站危险级别
恶意 ...
国际化 -
zgyxzgyx123:
木马分布
华为赛门铁克 IP信誉系统
钓鱼---最近6个周内趋 ...
国际化 -
zgyxzgyx123:
请选择区域
国内威胁
WSS指web页面扫描引擎
感染数量
验 ...
国际化 -
zgyxzgyx123:
恶意URL统计
日期格式不正确,请重新输入
钓鱼趋势
最新数据 ...
国际化
ssh框架
第一步:添加spring 添加hibernate 注:Spirng applicationContext.xml 文件 放在目mytest\WebRoot\WEB- INF 下
----------------------------------------------------------------------------------------------------------------------------------
第二步:配置 web.xml 如下(固定)
----------------------------------------------------------------------------------------------------------------------------------
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.5" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<filter>
<filter-name>struts2</filter-name>
<filter-class>
org.apache.struts2.dispatcher.FilterDispatcher
</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
</web-app>
----------------------------------------------------------------------------------------------------------------------------------
第三步:配置 mytest\src\struts.xml 文件
----------------------------------------------------------------------------------------------------------------------------------
<?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="user" extends="struts-default">
<action name="saveUser" class="saveUserAction">
<result name="success" type="redirect">listUser.action</result>
<result name="input">/save.jsp</result>
</action>
<action name="listUser" class="listUserAction">
<result>/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>
<result name="input">/update.jsp</result>
</action>
<action name="generateExcel" class="generateExcelAction">
<result name="success" type="stream">
<param name="contentType">application/vnd.ms-excel</param>
<param name="contentDisposition">filename="AllUsers.xls"</param>
<param name="inputName">downloadFile</param>
</result>
</action>
</package>
</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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
<property name="url" value="jdbc:mysql://localhost:3306/mytest"></property>
<property name="username" value="root"></property>
<property name="password" value="root"></property>
<property name="maxActive" value="100"></property>
<property name="maxIdle" value="30"></property>
<property name="maxWait" value="500"></property>
<property name="defaultAutoCommit" value="true"></property>
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
<property name="mappingResources">
<list>
<value>com/test/bean/User.hbm.xml</value>
</list>
</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">
<property name="userDao" ref="userDao"></property>
</bean>
<bean id="saveUserAction" class="com.test.action.user.SaveUserAction" scope="prototype">
<property name="service" ref="userService"></property>
</bean>
<bean id="listUserAction" class="com.test.action.user.ListUserAction" scope="prototype">
<property name="service" ref="userService"></property>
</bean>
<bean id="removeUserAction" class="com.test.action.user.RemoveUserAction" scope="prototype">
<property name="service" ref="userService"></property>
</bean>
<bean id="updatePUserAction" class="com.test.action.user.UpdatePUserAction" scope="prototype">
<property name="service" ref="userService"></property>
</bean>
<bean id="updateUserAction" class="com.test.action.user.UpdateUserAction" scope="prototype">
<property name="service" ref="userService"></property>
</bean>
<bean id="generateExcelAction" class="com.test.action.user.GenerateExcelAction" scope="singleton">
<property name="service" ref="userService"></property>
</bean>
</beans
----------------------------------------------------------------------------------------------------------------------------------
第五步:Dao接口
----------------------------------------------------------------------------------------------------------------------------------
package com.test.dao;
import java.util.List;
import com.test.bean.User;
public interface UserDAO
{
public boolean saveUser(User user);
public void removeUser(User user);
public User findUserById(Integer id);
public List<User> findAllUsers();
public void updateUser(User user);
}
----------------------------------------------------------------------------------------------------------------------------------
第六步:实现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
{
@SuppressWarnings("unchecked")
public List<User> findAllUsers()
{
String hql = "from User user order by user.id desc";
return (List<User>)this.getHibernateTemplate().find(hql);
}
public User findUserById(Integer id)
{
User user = (User) this.getHibernateTemplate().get(User.class, id);
return user;
}
public void removeUser(User user)
{
this.getHibernateTemplate().delete(user);
}
public boolean saveUser(User user)
{
this.getHibernateTemplate().save(user);
return true;
}
public void updateUser(User user)
{
this.getHibernateTemplate().update(user);
}
}
----------------------------------------------------------------------------------------------------------------------------------
第七步:servise接口
----------------------------------------------------------------------------------------------------------------------------------
package com.test.service;
import java.io.InputStream;
import java.util.List;
import com.test.bean.User;
public interface UserService
{
public List<User> findAll();
public void save(User user);
public void delete(User user);
public User findById(Integer id);
public void update(User user);
public InputStream getInputStream();
}
----------------------------------------------------------------------------------------------------------------------------------
第七步:实现servise接口
----------------------------------------------------------------------------------------------------------------------------------
package com.test.service.impl;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
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;
}
public void delete(User user)
{
this.userDao.removeUser(user);
}
public List<User> findAll()
{
return this.userDao.findAllUsers();
}
public User findById(Integer id)
{
return this.userDao.findUserById(id);
}
public void save(User user)
{
boolean brt = this.userDao.saveUser(user);
if (brt) {
//zhibiao
}
}
public void update(User user)
{
this.userDao.updateUser(user);
}
public InputStream getInputStream()
{
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet = wb.createSheet("sheet1");
HSSFRow row = sheet.createRow(0);
HSSFCell cell = row.createCell((short) 0);
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellValue("序号");
cell = row.createCell((short) 1);
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellValue("姓");
cell = row.createCell((short) 2);
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellValue("名");
cell = row.createCell((short) 3);
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellValue("年龄");
List<User> list = this.findAll();
for (int i = 0; i < list.size(); ++i)
{
User user = list.get(i);
row = sheet.createRow(i + 1);
cell = row.createCell((short) 0);
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellValue(i + 1);
cell = row.createCell((short) 1);
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellValue(user.getFirstname());
cell = row.createCell((short) 2);
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellValue(user.getLastname());
cell = row.createCell((short) 3);
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellValue(user.getAge());
}
File file = new File("test.xls");
try
{
OutputStream os = new FileOutputStream(file);
wb.write(os);
os.close();
}
catch (Exception e)
{
e.printStackTrace();
}
InputStream is = null;
try
{
is = new FileInputStream(file);
}
catch (FileNotFoundException e)
{
e.printStackTrace();
}
return is;
}
}
----------------------------------------------------------------------------------------------------------------------------------
第九步:添
----------------------------------------------------------------------------------------------------------------------------------
jsp文件
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Save User</title>
</head>
<body>
<h1><font color="red">Save User</font></h1>
<s:form action="saveUser">
<s:textfield name="user.firstname" label="%{getText('firstname')}"></s:textfield>
<s:textfield name="user.lastname" label="%{getText('lastname')}"></s:textfield>
<s:textfield name="user.age" label="%{getText('age')}"></s:textfield>
<s:submit></s:submit>
</s:form>
</body>
</html>
action 文件
package com.test.action.user;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
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 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.save(this.user);
return SUCCESS;
}
@Override
@SuppressWarnings("unchecked")
public void validate()
{
Map map = this.getFieldErrors();
Set set = map.keySet();
for (Iterator iter = set.iterator(); iter.hasNext();)
{
System.out.println(map.get(iter.next()));
}
}
}
----------------------------------------------------------------------------------------------------------------------------------
第十步: 查 改 删
----------------------------------------------------------------------------------------------------------------------------------
显示jsp文件
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<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(confirm("你真的想删除该记录么?"))
{
return true;
}
return false;
}
</script>
</head>
<body>
<h1><font color="red"><center>Users 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>
<s:a href="index.jsp">Homepage</s:a><br><br>
<s:a href="generateExcel.action">生成excel</s:a>
</body>
</html>
修改之前必须利用Id先把相关数据查出来 jsp文件
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<%
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 '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">Update User</font></h1>
<s:form action="updateUser">
<table>
<tr>
<td>
<s:hidden name="user.id" value="%{user.id}"></s:hidden>
</td>
</tr>
<tr>
<td>
<s:textfield name="user.firstname" value="%{user.firstname}" label="%{getText('firstname')}"></s:textfield>
</td>
</tr>
<tr>
<td>
<s:textfield name="user.lastname" value="%{user.lastname}" label="%{getText('lastname')}"></s:textfield>
</td>
</tr>
<tr>
<td>
<s:textfield name="user.age" value="%{user.age}" label="%{getText('age')}"></s:textfield>
</td>
</tr>
<tr>
<td>
<s:submit></s:submit>
</td>
</tr>
</table>
</s:form>
</body>
</html>
----------------------------------------------------------------------------------------------------------------------------------
第十一步: 查 删 改 Action
----------------------------------------------------------------------------------------------------------------------------------
查
package com.test.action.user;
import java.util.Map;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
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", service.findAll());
return SUCCESS;
}
}
删
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;
}
}
改
先查
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
{
user = this.service.findById(user.getId());
return SUCCESS;
}
}
再改
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;
}
}
----------------------------------------------------------------------------------------------------------------------------------
第十二步:excle 文件下载
----------------------------------------------------------------------------------------------------------------------------------
package com.test.action.user;
import java.io.InputStream;
import com.opensymphony.xwork2.ActionSupport;
import com.test.service.UserService;
public class GenerateExcelAction extends ActionSupport
{
private UserService service;
public UserService getService()
{
return service;
}
public void setService(UserService service)
{
this.service = service;
}
public InputStream getDownloadFile()
{
return this.service.getInputStream();
}
@Override
public String execute() throws Exception
{
return SUCCESS;
}
}
- mytest.rar (31 KB)
- 下载次数: 31
发表评论
-
国际化
2011-01-13 11:36 1050<%@ taglib prefix="s&qu ... -
struts2 alidate验证
2011-01-13 11:35 790------------------------------- ... -
struts2.0 hibernate 3.2 spring 2.0(dwr jquery)所需文件包
2011-01-13 01:43 1190truts2.0 hibernate 3.2 spring 2 ... -
ssh 登录例子(lib里的包已经清除)
2011-01-13 00:45 1520ssh 登录例子 -
ssh框架 mysql sql oracle xml jdbc配置
2011-01-13 00:29 1404------------------------------- ...
相关推荐
这个压缩包中的内容显然提供了一个基础的SSH框架实现,用于展示基本的增删改查(CRUD)操作,并包含了分页功能。下面我们将深入探讨SSH框架的核心组件及其在实际开发中的应用。 **Struts2** 是一个MVC(Model-View-...
SSH框架整合是一个常见的Java Web开发技术栈,由Spring、Struts2和...通过深入学习和实践这个项目,开发者不仅可以掌握SSH框架的整合技巧,还能了解到邮件服务、Excel处理以及文件上传下载等实用功能的实现方式。
这个压缩包文件的标题"ssh框架的lib包"表明它包含了一系列与SSH框架相关的库文件,用于支持SSH框架的运行和开发。描述中提到的"包含了ssh框架使用到的一些通用lib包"暗示了这些库文件是SSH框架正常工作所必需的组件...
在"ssh框架的模糊查询"这个主题中,我们将深入探讨如何在SSH框架下实现对数据的增删改查(CRUD)操作,以及模糊查询和分页显示功能。 首先,让我们来理解增删改查的基础。在SSH框架中,Struts2作为前端控制器处理...
SSH框架,全称为Struts2、Spring和Hibernate的组合,是Java Web开发中常见的三大开源框架集成。这个框架集合提供了模型-视图-控制器(MVC)架构模式,以及依赖注入(DI)和面向切面编程(AOP)等特性,极大地提高了...
SSH框架,全称为Struts2、Spring和Hibernate的组合,是Java Web开发中常见的三大框架。这三大框架分别负责表现层、业务层和持久层的管理,为开发者提供了高效、灵活且可扩展的开发环境。在Java Web项目中,SSH框架的...
2. **导入jar包**:将SSH框架所需的jar包引入到项目的lib目录下,包括Spring、Struts2、Hibernate的核心库以及它们各自的依赖库。 3. **配置Spring**:编写Spring的配置文件(如`applicationContext.xml`),定义...
SSH框架,全称为Spring、Struts和Hibernate,是Java Web开发中的一个经典组合,用于构建高效、可维护的Web应用程序。这些框架各自负责不同的职责,Spring作为基础架构框架,Struts处理视图与控制,Hibernate则专注于...
SSH框架,全称为Struts2、Spring和Hibernate的组合,是Java Web开发中常见的三大开源框架。它们分别负责表现层、业务层和服务层的管理,为开发者提供了强大的功能和灵活的架构。本压缩包提供了搭建SSH环境所需的核心...
这个“ssh框架增删改查小例子”应该包含了一个简单的Web应用,用于演示如何利用SSH框架进行基本的数据操作,即增(Add)、删(Delete)、改(Update)和查(Query)。 Struts2作为MVC(Model-View-Controller)框架...
现在我们来深入探讨SSH框架在"增查删改"(CRUD操作)实例中的应用。 首先,让我们从Spring框架开始。Spring是整个SSH架构的核心,它通过依赖注入(DI)和面向切面编程(AOP)提供了松散耦合和模块化的设计。在增查...
这个简单的SSH例子虽然规模不大,但已经展示了这三个框架如何协同工作。通常,一个HTTP请求首先由Struts2接收,然后根据配置转发到相应的Action。在Action中,通过Spring注入的业务服务对象执行实际的业务逻辑,而...
SSH框架,全称为Struts2、Spring和Hibernate的组合,是Java Web开发中常见的三大开源框架集成。这些框架协同工作,提供了模型-视图-控制器(MVC)架构,简化了开发过程,提高了代码的可维护性和重用性。下面我们将...
SSH框架,全称为Struts2、Spring和Hibernate的组合,是Java Web开发中常见的三大开源框架集成。它们分别负责表现层、业务层和服务层的管理,构建了一个完整的MVC(Model-View-Controller)架构。下面将详细介绍这三...
SSH框架,全称为Struts2、Spring和Hibernate的组合,是Java Web开发中常见的三大开源框架集成。这个压缩包文件"SSHdemo"提供了一个SSH框架搭建登录功能的示例,帮助开发者了解并实践如何整合这三个框架来构建一个...
SSH框架,全称为Spring、Struts、Hibernate,是Java Web开发中的一个经典组合。这个框架集合了Spring的依赖注入和事务管理,Struts的MVC设计模式,以及Hibernate的对象关系映射工具,为开发者提供了强大的企业级应用...
SSH框架是Java Web开发中常用的三大框架:Spring、Struts和Hibernate的缩写。这个压缩包包含的是一些基本的库文件(lib),用于支持基于SSH的Web应用程序的开发。以下是对这些框架及其关联知识点的详细说明: 1. **...
【EasyUI+SSH整合应用详解】 EasyUI与SSH(Struts2、Hibernate、Spring)是两种常见的Java Web开发技术,它们在企业级应用中广泛使用。EasyUI是一款基于jQuery的前端UI框架,提供了丰富的组件,如表格、下拉框、树...