ssh整合web导出excel案例.docx
《ssh整合web导出excel案例.docx》由会员分享,可在线阅读,更多相关《ssh整合web导出excel案例.docx(35页珍藏版)》请在冰豆网上搜索。
![ssh整合web导出excel案例.docx](https://file1.bdocx.com/fileroot1/2022-11/30/cd0ec961-a0f7-48b6-83d2-798145a254c1/cd0ec961-a0f7-48b6-83d2-798145a254c11.gif)
ssh整合web导出excel案例
基于Spring,Struts2,Hibernate整合,
jsp页面导出(下载)excel文件的简单应用
1)本例不采用Java任何导出excel文件常用的poi或者jxl等第三方jar包,仅仅基于I/O,做一个最简单的ssh整合excel文件导出(下载)。
2)2-3为功能展示
a.主页展示
·导出当前页到excel文件
·导出全部到excel文件
b.详细信息展示(下属子地名)
·导出当前页到excel文件
3)4-7为ssh整合相关xml配置
a.Spring相关xml配置
b.Hibernate相关xml配置
c.Struts2相关xml配置
d.web.xml配置(Spring容器的实例化,struts过滤器的配置)
3)8-14为源代码
a.action代码
b.service代码(导出excel文件具体实现,包括下载文件名中文乱码问题,详见代码注释)
c.dao代码
4)15-17为jsp页面
a.index.jsp(主页)
b.list.jsp(列表显示)
c.detail.jsp(详细信息-子地址列表显示)
1.导入相关jar包(ssh+dbcp)
2.本例导出全国地址详细信息,数据库中数据内容如下:
3.功能展示
a.主页
b.导出当前页(文件名为当前页起始id到结束id)
c.导出当前页的excel文件内容
d.导出全部
e.下属地名详细信息
f.导出下属地名
4.spring配置
xmlversion="1.0"encoding="UTF-8"?
>
//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-2.5.xsd
http:
//www.springframework.org/schema/beans
http:
//www.springframework.org/schema/beans/spring-beans-2.5.xsd
http:
//www.springframework.org/schema/context
http:
//www.springframework.org/schema/context/spring-context-2.5.xsd
http:
//www.springframework.org/schema/jee
http:
//www.springframework.org/schema/jee/spring-jee-2.5.xsd
http:
//www.springframework.org/schema/tx
http:
//www.springframework.org/schema/tx/spring-tx-2.5.xsd">
mysql:
//localhost:
3306/photo">
org.hibernate.dialect.MySQLDialect
true
true
com/luo/ssh/entity/Address.hbm.xml
5.hibernate映射配置(基本)
xmlversion="1.0"encoding="utf-8"?
>
DOCTYPEhibernate-mappingPUBLIC"-//Hibernate/HibernateMappingDTD3.0//EN"
"
6.struts配置
xmlversion="1.0"encoding="UTF-8"?
>
DOCTYPEstrutsPUBLIC
"-//ApacheSoftwareFoundation//DTDStrutsConfiguration2.1.7//EN"
"http:
//struts.apache.org/dtds/struts-2.1.7.dtd">
36
/list.jsp
/detail.jsp
7.web.xml配置
xmlversion="1.0"encoding="UTF-8"?
>
xmlns="
xmlns:
xsi="http:
//www.w3.org/2001/XMLSchema-instance"
xsi:
schemaLocation="
--指明spring容器位置-->
contextConfigLocation
classpath:
ssh.xml
org.springframework.web.context.ContextLoaderListener
struts2
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
struts2
/*
index.jsp
8.实体类Address
packagecom.luo.ssh.entity;
importjava.io.Serializable;
/**
*地名类
*@author罗立明
*
*/
publicclassAddressimplementsSerializable{
/**地名的id,唯一*/
privateintid;
/**地名*/
privateStringname;
/**地名的下属级别,直辖市,省,自治区为1,其他没下一级级别+1*/
privateintlevel;
/**地名上一级的id,直辖市,省,自治区的上级id为0*/
privateintupid;
/**保留字段,地名备注及描述*/
privateStringinfo;
publicintgetId(){
returnid;
}
publicvoidsetId(intid){
this.id=id;
}
publicStringgetName(){
returnname;
}
publicvoidsetName(Stringname){
this.name=name;
}
publicintgetLevel(){
returnlevel;
}
publicvoidsetLevel(intlevel){
this.level=level;
}
publicintgetUpid(){
returnupid;
}
publicvoidsetUpid(intupid){
this.upid=upid;
}
publicStringgetInfo(){
returninfo;
}
publicvoidsetInfo(Stringinfo){
this.info=info;
}
@Override
publicinthashCode(){
returnid<<16;
}
@Override
publicbooleanequals(Objectobj){
if(this==obj)
returntrue;
if(obj==null)
returnfalse;
if(getClass()!
=obj.getClass())
returnfalse;
Addressother=(Address)obj;
if(id!
=other.id)
returnfalse;
returntrue;
}
@Override
publicStringtoString(){
return"id:
"+id+"\n地名:
"+name+"\n级别:
"+level+"\n上一级id:
"+upid;
}
}
9.dao接口定义
packagecom.luo.ssh.dao;
importjava.util.List;
importcom.luo.ssh.entity.Address;
publicinterfaceAddressDao{
/**
*查询地址总数
*@return
*/
publicintcount();
/**
*根据id查找地址
*@paramid
*@return
*/
publicAddressfindById(intid);
/**
*根据id查找所有下级地址
*@paramaddress
*@return
*/
publicList
findChildren(Addressaddress);
/**
*根据id查找该地址的详细信息,追溯到最高上级
*@paramid
*@return结果形如:
湖北省-荆州市-公安县-狮子口镇
*/
publicStringfindDetailById(intid);
/**
*根据地名模糊查询所有满足条件的地址
*@paramname
*@return
*/
publicList
findLikeName(Stringname);
/**
*分页查询地址信息
*@parampage
*@parampageSize
*@return
*/
publicList
findPage(intpage,intpageSize);
/**
*查询全部
*@return
*/
publicList
findAll();}
10.dao的Hibernate实现
packagecom.luo.ssh.dao.impl;
importjava.sql.SQLException;
importjava.util.List;
importorg.hibernate.HibernateException;
importorg.hibernate.Query;
importorg.hibernate.Session;
importorg.springframework.orm.hibernate3.HibernateCallback;
importorg.springframework.orm.hibernate3.HibernateTemplate;
importorg.springframework.orm.hibernate3.support.HibernateDaoSupport;
importcom.luo.ssh.dao.AddressDao;
importcom.luo.ssh.entity.Address;
publicclassAddressDaoHibernateImplextendsHibernateDaoSupportimplementsAddressDao{
publicAddressfindById(intid){
HibernateTemplatetemplate=getHibernateTemplate();
Addressaddress=(Address)template.get(Address.class,id);
returnaddress;
}
publicList
findChildren(Addressaddress){Stringhql="fromAddresswhereupid=?
";
returngetHibernateTemplate().find(hql,address.getId());
}
publicStringfindDetailById(intid){
Addressadd=findById(id);
Stringdetail=add.getName();
while(add.getLevel()!
=1){
add=findById(add.getUpid());
detail=add.getName()+"-"+detail;
}
returndetail;
}
publicList
findLikeName(Stringname){Stringhql="fromAddresswherenamelike?
";
returngetHibernateTemplate().find(hql,"%"+name+"%");
}
publicintcount(){
Stringhql="selectcount(*)fromAddress";
returnInteger.parseInt(getHibernateTemplate().find(hql).get(0).toString());
}
publicList
findPage(finalintpage,finalintpageSize){HibernateTemplatetemplate=getHibernateTemplate();
List
list=(List)template.execute(newHibernateCallback(){publicObjectdoInHibernate(Sessionsession)throwsHibernateException,
SQLException{
Stringhql="fromAddress";
Queryquery=session.createQuery(hql);
query.setFirstResult((page-1)*pageSize);
query.setMaxResults(pageSize);
returnquery.list();
}
});
returnlist;
}
publicList
findAll(){Stringhql="fromAddress";
returngetHibernateTemplate().find(hql);
}
}
11