文章目录
效果
局部刷新
不会出现表单重复提交
ajax.html
index
Controller
package com.ths.arsenaldnsnginxconfig.controller;
import cn.hutool.json.JSON;
import cn.hutool.json.JSONObject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;
//@Controller
@RestController
public class AjaxController {
@RequestMapping("/ajax")
public ModelAndView ajax() {
return new ModelAndView("ajax");
}
@RequestMapping("/refresh")
public String refresh(){
return "refresh";
}
@RequestMapping("/json")
public JSON json(String name, String password, MultipartFile file) {
JSonObject jsonObject = new JSONObject();
jsonObject.putOpt("name", name);
jsonObject.putOpt("pwd", password);
System.out.println(file);
System.out.println(file.getOriginalFilename());
System.out.println(file.getOriginalFilename().length());
if (file.getOriginalFilename().length() != 0) {
jsonObject.putOpt("file", file.getOriginalFilename());
}else {
jsonObject.putOpt("file", "错误:请上传文件,再重新提交");
}
return jsonObject;
}
}