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用户. ...
随机推荐
- C/C++判断文件/文件夹是否存在 转
一.判断文件夹是否存在: 1.用CreateDirectory(".//FileManege",NULL);如果文件夹FileManege不存在,则创建. 2.或者 ...
- nodejs 做的带管理后台的东东,主要学习到 ....我忘了学到什么了
效果 http://www.steel-pot.com/ function handleStr(str,isHtml,callback) { if(!isHtml) { callback(str); ...
- ASP.NET Core中怎么实现Url rewrite功能
我们可以使用ASP.NET Core的中间件来实现Url rewrite功能,下面我们定义一个中间件ReplaceQueryStringMiddleware来替换Http请求中的Url参数即Query ...
- keil之编辑环境配置
1.edit-->configuration 2. 3.开始是:ANSI编码,但一去掉:display modules,中文的注视就乱码了:请教Justchen,把编码改为GB2312,一切恢复 ...
- windows下3D文字
windows下3D文字 简单概述 需要在每一帧的视频图像上面添加3D文字,文字可以自由移动位置,变换各种字体属性,还能进行一些简单动画.然后把处理好的视频图像传个下一个步骤去处理.做的过程中参考了G ...
- phpStudy环境安装SSL证书教程(apache)
https://cloud.tencent.com/product/ssl 此链接是检测域名 证书的可以检测一下 下面是证书配置 小白呢亲测 作为PHP程序员,我们一定要学会使用phpStudy环境集 ...
- 大专生自学iOS到找到工作的前前后后
先做个自我介绍,我13年考上一所很烂专科民办的学校,学的是生物专业,具体的学校名称我就不说出来献丑了.13年我就辍学了,我在那样的学校,一年学费要1万多,但是根本没有人学习,我实在看不到希望,我就退学 ...
- shell习题第1题:每日一文件
[题目要求] 请按照这样的日期格式(xxxx-xx-xx)每日生成一个文件 例如生成的文件为2019-04-25.log,并且把磁盘使用情况写入到这个文件中 不用考虑cron,仅仅写脚本即可 [核心要 ...
- CSS动画实例
上一篇讲过css动画transform transition的语法,这一节展示自己做的几个小例子加深印象 1. 线条动画效果 代码:最外层div包含2个小的div : a和b. a有左右边框(高度 ...
- thinkphp5使用workerman定时器定时爬取某站点新闻资讯等内容
1.首先通过 composer 安装workerman,在thinkphp5完全开发手册的扩展->coposer包->workerman有详细说明: #在项目根目录执行以下指令compos ...