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();
    }

猜你喜欢

转载自www.cnblogs.com/zizi-tothemoon/p/9361390.html
今日推荐