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用户. ...
随机推荐
- 十四、详述 IntelliJ IDEA 提交代码前的 Code Analysis 机制
在我们用 IntelliJ IDEA 向 SVN 或者 Git 提交代码的时候,IntelliJ IDEA 提供了一个自动分析代码的功能,即Perform code analysis: 如上图所示,当 ...
- MVC学习十二:Ajax.ActionLink用法
Ajax.ActionLink用法 <!--使用Ajax.BeginForm必须引用的js文件--> <script type="text/javascript" ...
- 【luogu P2296 寻找道路】 题解
题目链接:https://www.luogu.org/problemnew/show/P2296 题意:给定起点终点,找一条从起点到终点的最短路径使路上的每个点都能有路径到达终点. 我们先反着建一遍图 ...
- EF Core 2.1 中的 Eager loading、Explicit loading和LazyLoading (转自MSDN)
Entity Framework Core allows you to use the navigation properties in your model to load related enti ...
- 轻量ORM-SqlRepoEx (十)SqlRepoEx Nuget包下载说明
ORM-SqlRepoEx 是 .Net平台下兼容.NET Standard 2.0,一个实现以Lambda表达式转转换标准SQL语句,使用强类型操作数据的轻量级ORM工具,在减少魔法字串同时,通过灵 ...
- Reading Notes : 180213 计算机的硬件构成与处理流程
读书<计算机组成原理>,<鸟哥的Linux私房菜基础篇> 基本上接触过计算机的人,都多少知道计算机的具体构成,但是真正能讲明白的却说了很多,本节将讲解一下计算机的基本硬件构成和 ...
- 《Linux 学习》01---redis安装, 并使用Redis Desktop Manager 连接
一.环境简介: linux 系统:centos 7.X 二.安装大纲: 1.下载安装包 2.安装 3.统一管理redis 配置文件 4.编辑redis配置文件,设置常用的功能 5.(1)命令启动,连接 ...
- iOS多语言设置
最近公司做的项目需要向国外推广,因此app需要添加相应的语言设置,为此整理记录下多语言设置的过程.如有不对的地方,欢迎各位大神指正.下面就详细介绍下设置的过程: 1.基本设置 第一步:首先在 项目工程 ...
- php连接数据库(一)
1.php链接数据库: 1.链接数据库 2.判断是否连接成功 3.设置字符集 4.选择数据库 5.准备SQL语句 6.发送SQL语句 7.处理结果集 8.释放资源(关闭数据库) $result = m ...
- php http_build_query stream_context_create post请求
<?php function send_post($url, $post_data) { $postdata = http_build_query($post_data); $options = ...