日期:2014-05-16 浏览次数:20435 次
我又回来了,带来了Spring3MVC+MyBatis+ExtJs3整合开发系列之第二篇:菜单模块演示。
public class Menu implements Serializable {
private static final long serialVersionUID = -2726709540069876682L;
private Long id;
private Long parent_id;
private String name;
private String image;
private String url;
private String qtip;
private Integer sortNum;
private String description;
/**
* true:默认为叶子结点,即子菜单
*/
private boolean leaf = true;
private List<Menu> children;@Service
public class MenuService {
@Autowired
private MenuMapper menuMapper;
@Transactional
public List<Menu> getMenuListByUserId(Long userId) {
Map<String,Object> param = new HashMap<String,Object>();
List<Menu> mainMenuList = menuMapper.getMainMenuList(userId);
Iterator<Menu> it = mainMenuList.iterator();
//装载主菜单下所有的子菜单
while(it.hasNext()) {
Menu menu = it.next();
//false:表示为主菜单
menu.setLeaf(false);
Long parentId = menu.getId();
param.put("userId", userId);
param.put("parentId", parentId);
List<Menu> subMenuList = menuMapper.getSubMenuList(param);
menu.setChildren(subMenuList);
}
return mainMenuList;
}
}@Controller
@RequestMapping("/user")
public class UserController {
@Autowired
private UserService userService;
@Autowired
private MenuService menuService;/**
* 获取所有菜单
* @param request
* @param response
* @return
*/
@RequestMapping(value="/Menus",method=RequestMethod.POST)
public @ResponseBody Map<String,Object> getTopMenus(HttpServletRequest request,
HttpServletResponse response){
Map<String,Object> result = new HashMap<String,Object>();
User user = (User)request.getSession().getAttribute("user");
List<Menu> list = menuService.getMenuListByUserId(user.getId());
result.put("success", "true");
result.put("data", list);
return result;
}