controllerClass.ftl 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. package ${controllerPackage};
  2. import java.util.UUID;
  3. import javax.servlet.http.HttpServletRequest;
  4. import javax.servlet.http.HttpServletResponse;
  5. import org.apache.velocity.VelocityContext;
  6. import org.jeecgframework.minidao.pojo.MiniDaoPage;
  7. import org.jeecgframework.p3.core.common.utils.AjaxJson;
  8. import org.jeecgframework.p3.core.page.SystemTools;
  9. import org.jeecgframework.p3.core.util.plugin.ViewVelocity;
  10. import org.jeecgframework.p3.core.web.BaseController;
  11. import org.springframework.beans.factory.annotation.Autowired;
  12. import org.springframework.stereotype.Controller;
  13. import org.springframework.web.bind.annotation.ModelAttribute;
  14. import org.springframework.web.bind.annotation.RequestMapping;
  15. import org.springframework.web.bind.annotation.RequestMethod;
  16. import org.springframework.web.bind.annotation.RequestParam;
  17. import org.springframework.web.bind.annotation.ResponseBody;
  18. import ${domainPackage}.${className}Entity;
  19. import ${servicePackage}.${className}Service;
  20. /**
  21. * 描述:${codeName}
  22. * @author: ${author}
  23. * @since:${nowDate}
  24. * @version:1.0
  25. */
  26. @Controller
  27. @RequestMapping("/${projectName}/${lowerName}")
  28. public class ${className}Controller extends BaseController{
  29. @Autowired
  30. private ${className}Service ${lowerName}Service;
  31. /**
  32. * 列表页面
  33. * @return
  34. */
  35. @RequestMapping(params = "list",method = {RequestMethod.GET,RequestMethod.POST})
  36. public void list(@ModelAttribute ${className}Entity query,HttpServletRequest request,HttpServletResponse response,
  37. @RequestParam(required = false, value = "pageNo", defaultValue = "1") int pageNo,
  38. @RequestParam(required = false, value = "pageSize", defaultValue = "10") int pageSize) throws Exception{
  39. try {
  40. LOG.info(request, " back list");
  41. //分页数据
  42. MiniDaoPage<${className}Entity> list = ${lowerName}Service.getAll(query,pageNo,pageSize);
  43. VelocityContext velocityContext = new VelocityContext();
  44. velocityContext.put("${lowerName}",query);
  45. velocityContext.put("pageInfos",SystemTools.convertPaginatedList(list));
  46. String viewName = "${projectName}/${bussPackage}/${lowerName}-list.vm";
  47. ViewVelocity.view(request,response,viewName,velocityContext);
  48. } catch (Exception e) {
  49. e.printStackTrace();
  50. }
  51. }
  52. /**
  53. * 详情
  54. * @return
  55. */
  56. @RequestMapping(params="toDetail",method = RequestMethod.GET)
  57. public void ${lowerName}Detail(@RequestParam(required = true, value = "id" ) String id,HttpServletResponse response,HttpServletRequest request)throws Exception{
  58. VelocityContext velocityContext = new VelocityContext();
  59. String viewName = "${projectName}/${bussPackage}/${lowerName}-detail.vm";
  60. ${className}Entity ${lowerName} = ${lowerName}Service.get(id);
  61. velocityContext.put("${lowerName}",${lowerName});
  62. ViewVelocity.view(request,response,viewName,velocityContext);
  63. }
  64. /**
  65. * 跳转到添加页面
  66. * @return
  67. */
  68. @RequestMapping(params = "toAdd",method ={RequestMethod.GET, RequestMethod.POST})
  69. public void toAddDialog(HttpServletRequest request,HttpServletResponse response)throws Exception{
  70. VelocityContext velocityContext = new VelocityContext();
  71. String viewName = "${projectName}/${bussPackage}/${lowerName}-add.vm";
  72. ViewVelocity.view(request,response,viewName,velocityContext);
  73. }
  74. /**
  75. * 保存信息
  76. * @return
  77. */
  78. @RequestMapping(params = "doAdd",method ={RequestMethod.GET, RequestMethod.POST})
  79. @ResponseBody
  80. public AjaxJson doAdd(@ModelAttribute ${className}Entity ${lowerName}){
  81. AjaxJson j = new AjaxJson();
  82. try {
  83. String randomSeed = UUID.randomUUID().toString().replaceAll("-", "").toUpperCase();
  84. ${lowerName}.setId(randomSeed);
  85. ${lowerName}Service.insert(${lowerName});
  86. j.setMsg("保存成功");
  87. } catch (Exception e) {
  88. log.info(e.getMessage());
  89. j.setSuccess(false);
  90. j.setMsg("保存失败");
  91. }
  92. return j;
  93. }
  94. /**
  95. * 跳转到编辑页面
  96. * @return
  97. */
  98. @RequestMapping(params="toEdit",method = RequestMethod.GET)
  99. public void toEdit(@RequestParam(required = true, value = "id" ) String id,HttpServletResponse response,HttpServletRequest request) throws Exception{
  100. VelocityContext velocityContext = new VelocityContext();
  101. ${className}Entity ${lowerName} = ${lowerName}Service.get(id);
  102. velocityContext.put("${lowerName}",${lowerName});
  103. String viewName = "${projectName}/${bussPackage}/${lowerName}-edit.vm";
  104. ViewVelocity.view(request,response,viewName,velocityContext);
  105. }
  106. /**
  107. * 编辑
  108. * @return
  109. */
  110. @RequestMapping(params = "doEdit",method ={RequestMethod.GET, RequestMethod.POST})
  111. @ResponseBody
  112. public AjaxJson doEdit(@ModelAttribute ${className}Entity ${lowerName}){
  113. AjaxJson j = new AjaxJson();
  114. try {
  115. ${lowerName}Service.update(${lowerName});
  116. j.setMsg("编辑成功");
  117. } catch (Exception e) {
  118. log.info(e.getMessage());
  119. j.setSuccess(false);
  120. j.setMsg("编辑失败");
  121. }
  122. return j;
  123. }
  124. /**
  125. * 删除
  126. * @return
  127. */
  128. @RequestMapping(params="doDelete",method = RequestMethod.GET)
  129. @ResponseBody
  130. public AjaxJson doDelete(@RequestParam(required = true, value = "id" ) String id){
  131. AjaxJson j = new AjaxJson();
  132. try {
  133. ${className}Entity ${lowerName} = new ${className}Entity();
  134. ${lowerName}.setId(id);
  135. ${lowerName}Service.delete(${lowerName});
  136. j.setMsg("删除成功");
  137. } catch (Exception e) {
  138. log.info(e.getMessage());
  139. j.setSuccess(false);
  140. j.setMsg("删除失败");
  141. }
  142. return j;
  143. }
  144. }