Struts2(十.在修改页显示照片列表并增加删除照片功能)
一.显示照片列表功能
struts2中一般的处理方式:
先在action中,准备数据,转到jsp中显示
1.UserAction
/**
* 点击修改用户按钮跳转到修改用户界面
* 为用户准备照片,以便在modify.jsp中显示
* @return
* @throws SQLException
* @throws ClassNotFoundException
* @throws NamingException
*/
public String modify() throws SQLException, ClassNotFoundException, NamingException
{
UserDAO dao=new UserDAO();
//这个user会在Value Stack中出现
user=dao.getUserById(user.getId()); //通过当前用户id取得它的所有照片
PictureDAO pdao = new PictureDAO();
ArrayList<Picture> list=pdao.getPicture(user.getId());
//把照片存入ActionContext范围之内
ActionContext ctx = ActionContext.getContext();
ctx.put("PICTURES", list);
return "modify";
}
2.modify.jsp
当用户有照片的时候才显示,没有照片就不显示
if标签的参考文档
http://blog.csdn.net/chinajust/article/details/3922718
ognl的参考文档
http://www.blogjava.net/parable-myth/archive/2010/10/28/336353.html
EL表达式: $
OGNL表达式:%
<s:if test="%{#PICTURES.size()>0}"> <!-- OGNL表达式 -->
显示用户所有照片
<br>
<br>
<!-- 照片显示 -->
<table class="bordered">
<thead>
<tr><th>序号</th><th>照片名称</th><th>显示</th><th>删除</th></tr>
</thead>
<!-- PICTURES,cpic,s存入的Stack Context -->
<s:iterator value="#PICTURES" id="cpic" status="s">
<tr>
<td><s:property value="#s.index+1"/></td>
<td><s:property value="#cpic.name"/></td>
<td><a href="#" class="display" lang="<s:property value="#cpic.id"/>">显示</a></td>
<td><a href="#" class="delete" lang="<s:property value="#cpic.id"/>!<s:property value="#cpic.name"/>">删除</a></td>
</tr>
</s:iterator>
</table>
</s:if>
js:
//显示照片
$(".display").click(function(){
layer.use('extend/layer.ext.js');
$.getJSON("${pageContext.request.contextPath}/picture/display",{"picture.id":this.lang},function(data){
layer.photos({
html:"",
json: data
});
});
});
3.PictureAciton
/**
* 通过照片id显示照片
* @return
* @throws ClassNotFoundException
* @throws SQLException
* @throws NamingException
* @throws IOException
*/
public String display() throws ClassNotFoundException, SQLException, NamingException, IOException{
PictureDAO dao=new PictureDAO();
//通过照片id得到照片
ArrayList<Picture> list = dao.displayPicture(picture.getId());
//获取网站部署的根目录
String path=ServletActionContext.getRequest().getContextPath();
//调用PictureService中的getJSON方法
String json=PictureService.getJSON(list, path);
response.setCharacterEncoding("utf-8");
out=response.getWriter();
out.print(json);
//输出到控制台看一看
System.out.println(json);
return null;
}
4.PictureDAO
/**
* 通过照片id显示照片
* @param id
* @return
* @throws NamingException
* @throws SQLException
*/
public ArrayList<Picture> displayPicture(int id) throws NamingException, SQLException{
if(conn.isClosed())
{
Context ctx = new InitialContext();
DataSource ds = (DataSource) ctx.lookup("java:comp/env/jdbc/mysql");
conn = ds.getConnection();
}
ArrayList<Picture> pics=new ArrayList<Picture>();
sql="select * from pictures where id=?";
ps=conn.prepareStatement(sql);
ps.setInt(1, id);
ResultSet rs = ps.executeQuery();
while(rs.next()){
Picture pic =new Picture();
pic.setId(rs.getInt(1));
pic.setUid(rs.getInt(2));
pic.setName(rs.getString(3));
pic.setUrl(rs.getString(4));
pics.add(pic);
}
conn.close();
return pics;
}
5.效果

二.删除照片功能
删除功能:
把本地存的对应图片也删除
把数据库表中的记录删除
1.modify.jsp
js:
//删除照片
$(".delete").click(function(){
var str=this.lang.split("!");
if(!confirm("你确定要删除"+ str[1] +"这张照片吗?"))
{
return;
}
//jquery ajax方式请求action
$.post("${pageContext.request.contextPath}/picture/delete",{"picture.id":str[0]},function(){
location.href="${pageContext.request.contextPath}/user/modify?user.id=" + $("[name='user.id']").val();
});
});
2.PictureAciton
/**
* 通过照片id删除照片
* @return
* @throws ClassNotFoundException
* @throws SQLException
* @throws NamingException
*/
public String delete() throws ClassNotFoundException, SQLException, NamingException{
//注意先后顺序,因为需要从数据库里面获取url,如果先删除数据库中的信息就获取不到了,所以先删除本地照片
PictureDAO dao=new PictureDAO();
//通过照片id得到数据库中照片的url,以便在本地删除照片
String url = dao.getUrl(picture.getId());
ServletContext app=ServletActionContext.getServletContext();
String path=app.getRealPath("") + "/" + url;
File myfile=new File(path);
//FileUtils(需要一个file类型文件来处理),deleteQuietly(不会提示是否删除)
FileUtils.deleteQuietly(myfile);
//删除数据库中照片信息
dao.deletePicture(picture.getId());
return null;
}
3.PictureDAO
/**
* 通过照片id得到数据库中照片的url,以便在本地删除照片
* @param id
* @return
* @throws NamingException
* @throws SQLException
*/
public String getUrl(int id) throws NamingException, SQLException{
if(conn.isClosed())
{
Context ctx = new InitialContext();
DataSource ds = (DataSource) ctx.lookup("java:comp/env/jdbc/mysql");
conn = ds.getConnection();
}
sql="select url from pictures where id = ?";
ps=conn.prepareStatement(sql);
ps.setInt(1, id);
ResultSet rs = ps.executeQuery();
rs.next();
String url=rs.getString(1);
conn.close();
return url;
}
/**
* 通过照片id删除数据库中的照片数据
* @param id
* @throws NamingException
* @throws SQLException
*/
public void deletePicture(int id) throws NamingException, SQLException{
if(conn.isClosed())
{
Context ctx = new InitialContext();
DataSource ds = (DataSource) ctx.lookup("java:comp/env/jdbc/mysql");
conn = ds.getConnection();
}
sql="delete from pictures where id = ?";
ps=conn.prepareStatement(sql);
ps.setInt(1, id);
ps.execute();
conn.close();
}
Struts2(十.在修改页显示照片列表并增加删除照片功能)的更多相关文章
- dedecms后台每页文章条数如何修改(“文档列表”每一页显示的文档条数)
小明在学习采集,弄了个dedecms作为发布平台,几个小时后跑来报喜说好简单,但又不想制造那么多spam,每个分类只保留几条就好.在后台删除这些文章,每页只显示30个,看了下有100多页,立马沮丧了, ...
- MVC5 网站开发之六 管理员 2、添加、删除、重置密码、修改密码、列表浏览
目录 奔跑吧,代码小哥! MVC5网站开发之一 总体概述 MVC5 网站开发之二 创建项目 MVC5 网站开发之三 数据存储层功能实现 MVC5 网站开发之四 业务逻辑层的架构和基本功能 MVC5 网 ...
- NeHe OpenGL教程 第十二课:显示列表
转自[翻译]NeHe OpenGL 教程 前言 声明,此 NeHe OpenGL教程系列文章由51博客yarin翻译(2010-08-19),本博客为转载并稍加整理与修改.对NeHe的OpenGL管线 ...
- Flutter实战视频-移动电商-31.列表页_列表切换交互制作
31.列表页_列表切换交互制作 博客地址:https://jspang.com/post/FlutterShop.html#toc-c42 点击左侧的大类右边的小类也跟着变化 新建provide 要改 ...
- 显示 EXCEL 的页签列表
如果你的EXCEL表有很多页签,反复点击左右箭头可能会很费时间. 不妨试试在 左箭头 或者 右箭头 上点击 右键,会有页签列表弹出.
- Android6.0 源码修改之Setting列表配置项动态添加和静态添加
写在前面 最近客户有个需求,要求增加操作Setting列表配置项的功能,是不是一脸懵,没关系,一图胜千言,接下来就上图.诺,就是这么个意思. 原来的列表配置项 增加了单个配置项 增 ...
- php 实现微信模拟登陆、获取用户列表及群发消息功能示例
本文实例讲述了php实现微信模拟登陆.获取用户列表及群发消息功能.分享给大家供大家参考,具体如下: ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 ...
- 在MyEclipse显示struts2源码和doc文档及自动完成功能
分类: struts2 2010-01-07 16:34 1498人阅读 评论(1) 收藏 举报 myeclipsestruts文档xmlfileurl 在MyEclipse显示struts2源码和d ...
- CentOS6修改主机名(hostname)及 修改/etc/hosts 文件,增加ip和hostname的映射关系(转)
CentOS修改主机名(hostname) 需要修改两处:一处是/etc/sysconfig/network,另一处是/etc/hosts,只修改任一处会导致系统启动异常.首先切换到root用户. ...
随机推荐
- 报错Caused by: org.hibernate.AnnotationException: No identifier specified for entity:
Caused by: org.hibernate.AnnotationException: No identifier specified for entity:. 原因: 1.没有给实体类ID 解决 ...
- git日常基本用法
git作为项目管理现在已经是越来越广泛应用,结合自己平时的一些基本操作加上git说明文档里面的一些补充,我总结了一下git的基本用法: mkdir project # 创建项目目录 cd projec ...
- unittest单元测试框架之测试套件(三)
1.测试套件(注意:测试用例先添加先执行,后添加后执行,由此组织与设定测试用例的执行顺序) addTests:添加多个测试用例 addTest:添加单个测试用例 import unittest fro ...
- python3带tkinter窗口的ftp服务器,并使用pyinstaller打包成exe
python带tkinter窗口的ftp服务器,使用python3编写,打包使用pyinstaller,命令 pyinstaller -F .\ftpserver.py 代码也可在我的github上下 ...
- GoldenGate -- OGG EXTRACT进程 OGG-00446 报错
-- :: INFO OGG- Positioning to (Thread ) Sequence , RBA , SCN 0.470706262. Source Context : SourceMo ...
- HDU 5536--Chip Factory(暴力)
Chip Factory Time Limit: 18000/9000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)T ...
- MySQL常用参数说明(持续更新)
##innodb correlate innodb_flush_log_at_trx_commit value: 0,[1],2 effect: control the flush opera ...
- 配置SpringBoot方便的切换jar和war
配置SpringBoot方便的切换jar和war 网上关于如何切换,其实说的很明确,本文主要通过profile进行快速切换已实现在不同场合下,用不同的打包方式. jar到war修改步骤 pom文件修改 ...
- jquery输入框动态查询l<li></li>列表
1.html代码如下: 2.js代码如下: $('#search').bind('input propertychange', function () { searchKpoint(); }); fu ...
- Python入门 —— 06语音识别
Python 语音 实现语音操控的原理 语音操控分为语音识别和语音朗读两部分 我们使用speech模块实现语音模块(python 2.7) SAPI是微软Speech API , 是微软公司推出的语音 ...