Skip to content

Commit

Permalink
🐛 修复菜单管理的bug
Browse files Browse the repository at this point in the history
  • Loading branch information
ruibaby committed Dec 27, 2018
1 parent b609ecb commit 732c4d1
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 17 deletions.
5 changes: 5 additions & 0 deletions src/main/java/cc/ryanc/halo/model/domain/Menu.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.io.Serializable;

/**
Expand Down Expand Up @@ -33,16 +35,19 @@ public class Menu implements Serializable {
/**
* 菜单名称
*/
@NotEmpty(message = "菜单名称不能为空!")
private String menuName;

/**
* 菜单路径
*/
@NotEmpty(message = "菜单路径不能为空!")
private String menuUrl;

/**
* 排序编号
*/
@NotNull(message = "排序编号不能为空!")
private Integer menuSort;

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
package cc.ryanc.halo.web.controller.admin;

import cc.ryanc.halo.model.domain.Menu;
import cc.ryanc.halo.model.dto.JsonResult;
import cc.ryanc.halo.model.enums.ResultCodeEnum;
import cc.ryanc.halo.service.MenuService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
* <pre>
* 后台菜单管理控制器
Expand All @@ -28,6 +34,7 @@ public class MenuController {
* 渲染菜单设置页面
*
* @param model model
*
* @return 模板路径/admin/admin_menu
*/
@GetMapping
Expand All @@ -39,28 +46,36 @@ public String menus() {
* 新增/修改菜单
*
* @param menu menu
*
* @return 重定向到/admin/menus
*/
@PostMapping(value = "/save")
public String saveMenu(@ModelAttribute Menu menu) {
try {
menuService.save(menu);
} catch (Exception e) {
log.error("Saving menu failed: {}" + e.getMessage());
@ResponseBody
public JsonResult saveMenu(@Valid Menu menu, BindingResult result) {
if (result.hasErrors()) {
for (ObjectError error : result.getAllErrors()) {
return new JsonResult(ResultCodeEnum.FAIL.getCode(), error.getDefaultMessage());
}
}
menu = menuService.save(menu);
if (null != menu) {
return new JsonResult(ResultCodeEnum.SUCCESS.getCode(), "菜单保存成功!");
} else {
return new JsonResult(ResultCodeEnum.FAIL.getCode(), "菜单保存成功!");
}
return "redirect:/admin/menus";
}

/**
* 跳转到修改页面
*
* @param menuId 菜单编号
* @param model model
*
* @return 模板路径/admin/admin_menu
*/
@GetMapping(value = "/edit")
public String updateMenu(@RequestParam("menuId") Long menuId, Model model) {
Menu menu = menuService.findByMenuId(menuId).get();
Menu menu = menuService.findByMenuId(menuId).orElse(new Menu());
model.addAttribute("updateMenu", menu);
return "/admin/admin_menu";
}
Expand All @@ -69,6 +84,7 @@ public String updateMenu(@RequestParam("menuId") Long menuId, Model model) {
* 删除菜单
*
* @param menuId 菜单编号
*
* @return 重定向到/admin/menus
*/
@GetMapping(value = "/remove")
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/i18n/messages.properties
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ admin.menus.text.update-menu = 修改菜单
admin.menus.text.all-menus = 所有菜单
admin.menus.text.add-menu = 添加菜单
admin.menus.form.menu-name = 名称
admin.menus.form.menu-name-tips = 页面上所显示的名称
admin.menus.form.menu-name-tips = *页面上所显示的名称
admin.menus.form.menu-url = 路径
admin.menus.form.menu-url-tips = *菜单的路径
admin.menus.form.menu-sort = 排序编号
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/i18n/messages_en_US.properties
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ admin.menus.text.update-menu = Edit menu
admin.menus.text.all-menus = All menus
admin.menus.text.add-menu = Add menu
admin.menus.form.menu-name = Menu name
admin.menus.form.menu-name-tips = The name displayed on the page
admin.menus.form.menu-name-tips = *The name displayed on the page
admin.menus.form.menu-url = Menu url
admin.menus.form.menu-url-tips = *Path to the menu
admin.menus.form.menu-sort = Sort number
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/i18n/messages_zh_CN.properties
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ admin.menus.text.update-menu = 修改菜单
admin.menus.text.all-menus = 所有菜单
admin.menus.text.add-menu = 添加菜单
admin.menus.form.menu-name = 名称
admin.menus.form.menu-name-tips = 页面上所显示的名称
admin.menus.form.menu-name-tips = *页面上所显示的名称
admin.menus.form.menu-url = 路径
admin.menus.form.menu-url-tips = *菜单的路径
admin.menus.form.menu-sort = 排序编号
Expand Down
28 changes: 21 additions & 7 deletions src/main/resources/templates/admin/admin_menu.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<h1 style="display: inline-block;"><@spring.message code='admin.menus.title' /></h1>
<ol class="breadcrumb">
<li>
<a data-pjax="true" href="/admin"><i class="fa fa-dashboard"></i> <@spring.message code='admin.index.bread.index' /></a>
<a data-pjax="true" href="/admin/"><i class="fa fa-dashboard"></i> <@spring.message code='admin.index.bread.index' /></a>
</li>
<li><a data-pjax="true" href="javascript:void(0)"><@spring.message code='admin.themes.bread.appearance' /></a></li>
<li class="active"><@spring.message code='admin.menus.title' /></li>
Expand All @@ -20,7 +20,7 @@
<div class="box-header with-border">
<h3 class="box-title"><@spring.message code='admin.menus.text.update-menu' /><#if updateMenu??>[${updateMenu.menuName}]</#if></h3>
</div>
<form action="/admin/menus/save" method="post" role="form" id="menuAddForm">
<form role="form" id="menuSaveForm">
<input type="hidden" name="menuId" value="${updateMenu.menuId?c}">
<div class="box-body">
<div class="form-group">
Expand All @@ -35,7 +35,7 @@
</div>
<div class="form-group">
<label for="menuSort"><@spring.message code='admin.menus.form.menu-sort' /></label>
<input type="number" class="form-control" id="menuSort" name="menuSort" value="${updateMenu.menuSort!}">
<input type="number" class="form-control" id="menuSort" name="menuSort" value="${updateMenu.menuSort?c}">
</div>
<div class="form-group">
<label for="menuIcon"><@spring.message code='admin.menus.form.menu-icon' /></label>
Expand All @@ -51,15 +51,15 @@
</div>
</div>
<div class="box-footer">
<button type="submit" class="btn btn-primary btn-sm "><@spring.message code='common.btn.define-edit' /></button>
<button type="button" class="btn btn-primary btn-sm" onclick="save()"><@spring.message code='common.btn.define-edit' /></button>
<a data-pjax="true" href="/admin/menus" class="btn btn-info btn-sm "><@spring.message code='common.btn.back-to-add' /></a>
</div>
</form>
<#else >
<div class="box-header with-border">
<h3 class="box-title"><@spring.message code='admin.menus.text.add-menu' /></h3>
</div>
<form action="/admin/menus/save" method="post" role="form" id="menuAddForm">
<form role="form" id="menuSaveForm">
<div class="box-body">
<div class="form-group">
<label for="menuName"><@spring.message code='admin.menus.form.menu-name' /></label>
Expand Down Expand Up @@ -89,7 +89,7 @@
</div>
</div>
<div class="box-footer">
<button type="submit" class="btn btn-primary btn-sm "><@spring.message code='common.btn.define-add' /></button>
<button type="button" class="btn btn-primary btn-sm" onclick="save()"><@spring.message code='common.btn.define-add' /></button>
</div>
</form>
</#if>
Expand All @@ -116,7 +116,7 @@
<tr>
<td>${menu.menuName!}</td>
<td>${menu.menuUrl!}</td>
<td>${menu.menuSort!}</td>
<td>${menu.menuSort?c}</td>
<td>${menu.menuIcon!}</td>
<td>
<#if updateMenu?? && menu.menuId?c==updateMenu.menuId?c>
Expand Down Expand Up @@ -163,6 +163,20 @@
var url=$.trim($("#url").val());
window.location.href=url;
}
/**
* 保存
*/
function save() {
var param = $('#menuSaveForm').serialize();
$.post('/admin/menus/save', param, function (data) {
if (data.code === 1) {
halo.showMsgAndRedirect(data.msg, 'success', 1000,'/admin/menus');
} else {
halo.showMsg(data.msg, 'error', 2000);
}
}, 'JSON');
}
</script>
</div>
<@footer></@footer>
Expand Down

0 comments on commit 732c4d1

Please sign in to comment.