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
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



