栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

实用功能汇总

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

实用功能汇总

实用功能汇总 1.上传文件(图片)

controller

package com.example.admin.controller;

import org.springframework.stereotype.Controller;
import org.springframework.util.ResourceUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.text.SimpleDateFormat;
import java.util.Date;

@Controller
@RequestMapping("/file_test")

public class File_uploadtTest {
    @RequestMapping("/add")
    @ResponseBody
    public String addUser(HttpServletRequest request, HttpServletResponse response,
                          MultipartFile file) throws IOException {

        String fileName = file.getOriginalFilename();//获取文件名
        System.out.println("获取文件名"+fileName);
        fileName = getFileName(fileName);          //添加时间戳后的文件名
        System.out.println("获取文件名加时间"+fileName);
        request.getSession().setAttribute("fileImgName",fileName);
        String filepath = getUploadPath();     //获取当前系统路径
        if (!file.isEmpty()) {                  //如果文件不为空
            try (BufferedOutputStream out = new BufferedOutputStream(   //上传
                    new FileOutputStream(new File(filepath + File.separator + fileName)))) {
                out.write(file.getBytes());
                out.flush();
            } catch (FileNotFoundException e) {
                System.out.println("上传文件失败 FileNotFoundException:" + e.getMessage());
            } catch (IOException e) {
                System.out.println("上传文件失败 IOException:" + e.getMessage());
            }

        } else {
            System.out.println("上传文件失败,文件为空");
        }
        return "ok";
    }
    
    
    private String getFileName(String fileName) {
        int index = fileName.lastIndexOf(".");
        final SimpleDateFormat sDateFormate = new SimpleDateFormat("yyyymmddHHmmss");  //设置时间格式
        String nowTimeStr = sDateFormate.format(new Date()); // 当前时间
        fileName = fileName.substring(0, index) + "_" + nowTimeStr + fileName.substring(index);
        return fileName;
    }

    
    private String getUploadPath() {
        File path = null;
        try {
            path = new File(ResourceUtils.getURL("classpath:").getPath());
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        if (!path.exists()) path = new File("");
        File upload = new File(path.getAbsolutePath(), "src/main/resources/static/img/");
        if (!upload.exists()) upload.mkdirs();
        return upload.getAbsolutePath();
    }

}

html页面

  

注意:

2.layui页面的添加之后自动关闭并且刷新

点击确认 添加成功 就自动关闭 并且刷新数据

 function add_art() {
      var lid=$("#lid").val();
      var art_title=$("#art_title").val();
      var art_content=$("#art_content").val();
      $.ajax({
        url:"article/add",
        data:{"lid":lid,"title":art_title,"content":art_content},
        dataType:"json",
        type:"post",
        success:function (data) {
          if(data>0){
            alert("添加成功");
            //location.href="javascript:location.replace(location.href)";(这个是 刷新当前页面)
            // 获得frame索引
            var index = parent.layer.getframeIndex(window.name);
            //关闭当前frame
            parent.layer.close(index);
            window.parent.location.reload();

          }else {
            alert("添加失败");
          }
        },
        error:function (data) {
          alert("后台没传数据");
        }
      })
    }
3.标签select 通过后台拿到数据显示到option中
4.修改(ajax在两个页面中)

注意:需要写两个ajax发送请求 拿到数据 在写一个ajax将数据显示到 update.html的页面上

list的显示条数的页面上 点击查询

在update页面上来就加载 后台存的session的值

5.ajax+mybatis实现分页查询+模糊查询+多条件查询

实现的效果

前台js


controller层

@RequestMapping("/mh")
    @ResponseBody
    public String mh(HttpServletRequest request){ //模糊查询+分页+关联
        String page=request.getParameter("page");
        if(page==null || "".equals(page)){
            page="1";
        }
        String seach_title=request.getParameter("seach_title");//搜索标题
        String seach_lname=request.getParameter("seach_lname");//搜索标签
        User user = (User) request.getSession().getAttribute("user");
        //页面传来的值 存在condition对象中
        condition condition=new condition();
        condition.setPage(Integer.parseInt(page));
        condition.setSeach_title(seach_title);
        condition.setSeach_lname(seach_lname);
        condition.setUid(user.getId());

        List> list = articleService.mh(condition);//模糊查询+分页+关联
        //模糊查询总条数
        List> count2 = articleService.count2(condition);
        int sum = count2.size();//总条数
        int sumpage=sum%4>0?(sum/4+1):(sum/4);//页面的总页数
        //将从后台查询到的值封装在dgb对象中
        datagridebean dgb=new datagridebean();
        dgb.setList(list);
        dgb.setCount(sum);
        dgb.setPage(Integer.parseInt(page));//当前页
        dgb.setSumpage(sumpage);
        //将数据转为json格式 给页面
        String json = JSON.toJSONStringWithDateFormat(dgb, "yyyy-MM-dd");
        return json;
    }

service的impl

  @Override
    public List> mh(condition condition) {
        int page = condition.getPage();
        int f=(page-1)*4;
        condition.setPage(f);
        return adminlableMapper.mh(condition);
    }

mapper.xml

 
        select art.*,u.uname,la.lname
        from article art,user u,lable la
        where art.uid=u.id and art.lid=la.id and title like '%${seach_title}%'
        and lname like '%${seach_lname}%' and u.id=#{uid}
    

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/781105.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号