直接进入正题
package com.ray.ray.common.base;
import java.util.HashMap;
/**
* @author Ray
* @date 2018/11/12 0012
* 操作消息提醒
*/
public class AjaxResult extends HashMap<String, Object> {
/**
* 初始化一个新的 Message 对象
*/
public AjaxResult() {}
/**
* 返回错误消息
* @return 错误消息
*/
public static AjaxResult error() {
return error(500, "操作失败");
}
/**
* 返回错误消息
* @param msg 内容
* @return 错误消息
*/
public static AjaxResult error(String msg) {
return error(500, msg);
}
/**
* 返回错误消息
* @param code 错误码
* @param msg 内容
* @return 错误消息
*/
public static AjaxResult error(int code, String msg) {
AjaxResult ajaxResult = new AjaxResult();
ajaxResult.put("code", code);
ajaxResult.put("msg", msg);
return ajaxResult;
}
/**
* 返回成功消息
* @param msg 内容
* @return 成功消息
*/
public static AjaxResult success(String msg) {
AjaxResult ajaxResult = new AjaxResult();
ajaxResult.put("msg", msg);
ajaxResult.put("code", 0);
return ajaxResult;
}
/**
* 返回成功消息
* @return 成功消息
*/
public static AjaxResult success() {
return AjaxResult.success("操作成功");
}
/**
* 返回成功消息
* @param key 键值
* @param value 内容
* @return 成功消息
*/
@Override
public AjaxResult put(String key, Object value) {
super.put(key, value);
return this;
}
}
package com.ray.ray.common.base;
/**
* @author Ray
* @date 2018/11/12 0012
* web层通用数据处理
*/
public class BaseController {
/**
* 响应返回结果
* 参数result 类型可自行更换
* 如:
* boolean result -> return result == true ? success() : error();
* int result -> return result > 0 ? success() : error();
*/
protected AjaxResult toAjax(boolean result) {
return result == true ? success() : error();
}
/**
* 返回成功
*/
public AjaxResult success() {
return AjaxResult.success();
}
/**
* 返回失败
*/
public AjaxResult error() {
return AjaxResult.error();
}
/**
* 返回成功消息
*/
public AjaxResult success(String message)
{
return AjaxResult.success(message);
}
/**
* 返回失败消息
*/
public AjaxResult error(String message)
{
return AjaxResult.error(message);
}
/**
* 返回错误码消息
*/
public AjaxResult error(int code, String message)
{
return AjaxResult.error(code, message);
}
}
/**
* 新增
* @param sysUser 实体类
* @return 视图
*/
@ApiOperation(value = "新增用户", notes = "返回用户列表视图")
@ApiImplicitParam(name = "sysUser", value = "使用@ModelAttribute注解接收前端数据,使用@Validated注解验证后台数据,内容是sysUser实体", dataType = "SysUser")
@PostMapping("/user/toAdd")
@ResponseBody
public AjaxResult toAdd(@Validated @ModelAttribute SysUser sysUser) {
// 操作一: return error 或 success,直接返回msg
// if (StringUtils.isNotNull(user.getUserId()) && SysUser.isAdmin(user.getUserId()))
// {
// return error("不允许修改超级管理员用户");
// }
// 操作二: return toAjax ,需判断
return toAjax(sysUserService.save(sysUser));
}
<script th:src="@{/js/jquery-1.11.1.js}"></script>
<script th:src="@{/js/jquery-validation/jquery.validator.js}"></script>
<script>
$("#fm").validator({
valid: function (form) {
var me = this;
// 提交表单之前,hold住表单,防止重复提交
me.holdSubmit();
$.ajax({
url: "/system/user/toAdd",
data: $(form).serialize(),
type: "POST",
dataType: 'json',
success: function (result) {
if (result.code == web_status.SUCCESS) {
var index = parent.layer.getFrameIndex(window.name);
parent.layer.msg(result.msg, {
icon: 1,
time: 2000
}, function () {
window.parent.location.reload();
parent.layer.clone(index);
});
} else {
parent.layer.msg(result.msg,{
icon: 2,
time: 2000 //2秒关闭(如果不配置,默认是3秒)
});
me.holdSubmit(false);
}
}
});
}
});
/** 消息状态码 */
web_status = {
SUCCESS: 0,
FAIL: 500
};
</script>