码迷,mamicode.com
首页 > Web开发 > 详细

实现ajax图片的刷新

时间:2019-10-28 00:45:50      阅读:196      评论:0      收藏:0      [点我收藏+]

标签:block   image   remove   ===   lex   测试   加载   dom   not   

 

技术图片

 

 

直接上代码:

这里我使用mybatis框架的逆反工程:

所以我这里省略了pojo dao 以及 dao的对应接口

需要util工具(也可以自己写):

技术图片
package cn.util;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;

public class MyBatisUtil {
    private static SqlSessionFactory factory;
    
    static{//在静态代码块下,factory只会被创建一次
        System.out.println("static factory===============");
        try {
            InputStream is = Resources.getResourceAsStream("mybatis-config.xml");
            factory = new SqlSessionFactoryBuilder().build(is);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } 
    }
    
    public static SqlSession createSqlSession(){
        return factory.openSession(false);//true 为自动提交事务
    }
    
    public static void closeSqlSession(SqlSession sqlSession){
        if(null != sqlSession) 
            sqlSession.close();
    }
}
MybatisUtil

 

Service层:

技术图片
package cn.service;

import cn.pojo.Img;

import java.util.List;

public interface ImgMapperService {

    List<Img> selectAll();
}
ServiceDao.interface

 

技术图片
package cn.service;

import cn.dao.ImgMapper;
import cn.pojo.Img;
import cn.util.MyBatisUtil;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

public class ImgMapperServiceImpl implements ImgMapperService {

    private static final SqlSession sqlSession = MyBatisUtil.createSqlSession();
    private static final ImgMapper mapper = sqlSession.getMapper(ImgMapper.class);

    public List<Img> selectAll() {
        List<Img> imgs = mapper.selectAll();
        return imgs;
    }
}
ServiceImpl.class

 

Servlet层:

技术图片
package cn.servlet;


import cn.dao.ImgMapper;
import cn.pojo.Img;
import cn.service.ImgMapperService;
import cn.service.ImgMapperServiceImpl;
import cn.util.MyBatisUtil;
import com.google.gson.Gson;
import org.apache.ibatis.session.SqlSession;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/Servlet01")
public class Servlet01 extends HttpServlet {
    ImgMapperService imgMapperService = new ImgMapperServiceImpl();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html;charset=UTF-8");

        Gson gson = new Gson();

        List<Img> imgs = imgMapperService.selectAll();
        System.out.println(gson.toJson(imgs));
        resp.getWriter().println(gson.toJson(imgs));
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        super.doPost(req, resp);
    }
}
Servlet

 

Web方面: 需要准备 img 以及路径

index.jsp:

技术图片
<%--
  Created by IntelliJ IDEA.
  User: admin
  Date: 2019/10/27
  Time: 21:40
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>测试</title>
<%--    <script src="js/jquery.min.js"></script>--%>
    <script src="js/jquery.min.js"></script>
    <script>
        $(function () {

            $("#bto1").click(function () {
                alert("aaa")
                $("#bto1").remove();
                $.ajax({

                    type: "Get",
                    dataType: "json",
                    url: "Servlet01",
                    data: "userName="+Math.random(),
                    success: function(data){
                        $.each(data,function (i,val) {
                            // alert(data[i].url)

                            $("#b").html(data[i].url)
                        })

                    },
                    error: function(msg){
                        alert("请联系客户")
                    }
                })
            })

        })
    </script>
    <style>
        .bto1{
                    width: 340px;
                    height: 340px;
                    display: flex;
                    border-radius: 50%;
                    align-items: center;
                    justify-content: center;
                    overflow: hidden;
        }
    </style>
</head>
<body>


<div id="bto1"class="bto1">
    <img src="img/d2.png" alt="第二张"/>
</div>





<div id="b"class="bto1"></div>
</body>
</html>
index.jsp

 

 

 

 

注意: 这里的图片经常在加载时会出错,所以最好先弄一个jsp文件然后把图片测试一遍

正确的路径存入数据库:

数据库:

技术图片

 

实现ajax图片的刷新

标签:block   image   remove   ===   lex   测试   加载   dom   not   

原文地址:https://www.cnblogs.com/bichen-01/p/11749845.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!