Java tutorial
/******************************************************************************* * Copyright (c) 2005, 2014 springside.github.io * * Licensed under the Apache License, Version 2.0 (the "License"); *******************************************************************************/ package com.kingen.web; import java.util.List; import java.util.Map; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.shiro.authz.annotation.RequiresPermissions; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; import com.alibaba.fastjson.JSONObject; import com.google.common.collect.Maps; import com.kingen.aop.ControllerLogAnnotation; import com.kingen.bean.AssetsType; import com.kingen.service.assets.AssetsService; import com.kingen.util.JsonResultBuilder; import com.kingen.util.Page; import com.kingen.util.TreeConverter; import com.kingen.util.mapper.BeanMapper; import com.kingen.vo.TreeNode; /** * * @author wj * @date 2017-2-7 */ @Controller @RequestMapping(value = "/assets") public class AssetsController extends CommonController { private static Logger logger = LoggerFactory.getLogger(AssetsController.class); @Autowired private AssetsService service; @RequestMapping(value = "/type") @RequiresPermissions("assetsType:view") @ControllerLogAnnotation(moduleName = "??", option = "?") public String execute(Model m, HttpServletResponse response) throws Exception { return "assets/type/tree"; } /** * ?grid */ //?jsonstring ?jsonObject @RequestMapping(value = "/type/treeData") public @ResponseBody Object data(Page<AssetsType> page, HttpServletResponse response) { List<AssetsType> all = service.list("AssetsType"); List<TreeNode> allConverted = BeanMapper.mapList(all, TreeNode.class); Map<String, Object> json = Maps.newHashMapWithExpectedSize(1); //?chidlren,return toComplexJsonString(offices);?.jsonString??????? json.put("children", TreeConverter.toComplexTree(allConverted)); return json; } @RequestMapping(value = "type/toEdit") public String typetoEdit(String id, String pid, String action, HttpServletResponse response, HttpServletRequest request, Model model) { model.addAttribute("action", action); model.addAttribute("id", id);//null? ?? model.addAttribute("pid", pid);//null? ?? return "assets/type/edit"; } @RequestMapping(value = "type/one") public void one(String id, HttpServletResponse response) { AssetsType u = service.uniqueEntity("AssetsType", "id", id); writeJson(response, u); } /** * */ @ControllerLogAnnotation(moduleName = "??-?", option = "") @RequestMapping(value = "type/deleteCascade") public void deleteCascade(String id, HttpServletResponse response) { JSONObject json = new JSONObject(); try { service.delCascade(id); json = JsonResultBuilder.success(true).msg("?").json(); } catch (Exception e) { json = JsonResultBuilder.success(false).msg("").json(); logger.error(e.getMessage()); e.printStackTrace(); } writeJson(response, json); } /** * * @throws Exception */ @RequestMapping(value = "type/save") @ControllerLogAnnotation(moduleName = "??-?", option = "") public void save(AssetsType data, HttpServletResponse response) { JSONObject json = new JSONObject(); try { service.addObj(data); json = JsonResultBuilder.success(true).msg("??").json(); } catch (Exception e) {//service ? // TODO Auto-generated catch block e.printStackTrace(); json = JsonResultBuilder.success(false).msg(e.getMessage()).json(); logger.error(e.getMessage()); } writeJson(response, json); } /** * */ @RequestMapping(value = "type/update") @ControllerLogAnnotation(moduleName = "??-?", option = "") public void update(AssetsType data, HttpServletResponse response) { JSONObject json = new JSONObject(); try { service.updateForm(data); json = JsonResultBuilder.success(true).msg("??").json(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); json = JsonResultBuilder.success(false).msg(e.getMessage()).json(); logger.error(e.getMessage()); } writeJson(response, json); } }