controllerTemplate.ftl 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. package ${bussiPackage}.controller.${entityPackage};
  2. import java.util.List;
  3. import javax.servlet.http.HttpServletRequest;
  4. import javax.servlet.http.HttpServletResponse;
  5. import org.apache.log4j.Logger;
  6. import org.springframework.beans.factory.annotation.Autowired;
  7. import org.springframework.stereotype.Controller;
  8. import org.springframework.web.bind.annotation.RequestMapping;
  9. import org.springframework.web.bind.annotation.ResponseBody;
  10. import org.springframework.web.servlet.ModelAndView;
  11. import org.jeecgframework.core.common.controller.BaseController;
  12. import org.jeecgframework.core.common.hibernate.qbc.CriteriaQuery;
  13. import org.jeecgframework.core.common.model.json.AjaxJson;
  14. import org.jeecgframework.core.common.model.json.DataGrid;
  15. import org.jeecgframework.core.constant.Globals;
  16. import org.jeecgframework.core.util.StringUtil;
  17. import org.jeecgframework.tag.core.easyui.TagUtil;
  18. import org.jeecgframework.web.system.pojo.base.TSDepart;
  19. import org.jeecgframework.web.system.service.SystemService;
  20. import org.jeecgframework.core.util.MyBeanUtils;
  21. import ${bussiPackage}.entity.${entityPackage}.${entityName}Entity;
  22. import ${bussiPackage}.service.${entityPackage}.${entityName}ServiceI;
  23. <#-- restful 通用方法生成 -->
  24. import org.springframework.http.ResponseEntity;
  25. import org.springframework.stereotype.Controller;
  26. import org.springframework.web.bind.annotation.PathVariable;
  27. import org.springframework.web.bind.annotation.RequestBody;
  28. import org.springframework.web.bind.annotation.RequestMapping;
  29. import org.springframework.web.bind.annotation.RequestMethod;
  30. import org.springframework.web.bind.annotation.ResponseBody;
  31. import org.springframework.web.bind.annotation.ResponseStatus;
  32. import org.springframework.http.HttpHeaders;
  33. import org.springframework.http.HttpStatus;
  34. import org.jeecgframework.core.beanvalidator.BeanValidators;
  35. import java.util.Set;
  36. import javax.validation.ConstraintViolation;
  37. import javax.validation.Validator;
  38. import java.net.URI;
  39. import org.springframework.http.MediaType;
  40. import org.springframework.web.util.UriComponentsBuilder;
  41. <#-- restful 通用方法生成 -->
  42. /**
  43. * @Title: Controller
  44. * @Description: ${ftl_description}
  45. * @author zhangdaihao
  46. * @date ${ftl_create_time}
  47. * @version V1.0
  48. *
  49. */
  50. @Controller
  51. @RequestMapping("/${entityName?uncap_first}Controller")
  52. public class ${entityName}Controller extends BaseController {
  53. /**
  54. * Logger for this class
  55. */
  56. private static final Logger logger = Logger.getLogger(${entityName}Controller.class);
  57. @Autowired
  58. private ${entityName}ServiceI ${entityName?uncap_first}Service;
  59. @Autowired
  60. private SystemService systemService;
  61. @Autowired
  62. private Validator validator;
  63. /**
  64. * ${ftl_description}列表 页面跳转
  65. *
  66. * @return
  67. */
  68. @RequestMapping(params = "list")
  69. public ModelAndView list(HttpServletRequest request) {
  70. return new ModelAndView("${bussiPackage?replace(".","/")}/${entityPackage}/${entityName?uncap_first}List");
  71. }
  72. /**
  73. * easyui AJAX请求数据
  74. *
  75. * @param request
  76. * @param response
  77. * @param dataGrid
  78. * @param user
  79. */
  80. @RequestMapping(params = "datagrid")
  81. public void datagrid(${entityName}Entity ${entityName?uncap_first},HttpServletRequest request, HttpServletResponse response, DataGrid dataGrid) {
  82. CriteriaQuery cq = new CriteriaQuery(${entityName}Entity.class, dataGrid);
  83. //查询条件组装器
  84. org.jeecgframework.core.extend.hqlsearch.HqlGenerateUtil.installHql(cq, ${entityName?uncap_first}, request.getParameterMap());
  85. this.${entityName?uncap_first}Service.getDataGridReturn(cq, true);
  86. TagUtil.datagrid(response, dataGrid);
  87. }
  88. /**
  89. * 删除${ftl_description}
  90. *
  91. * @return
  92. */
  93. @RequestMapping(params = "del")
  94. @ResponseBody
  95. public AjaxJson del(${entityName}Entity ${entityName?uncap_first}, HttpServletRequest request) {
  96. String message = null;
  97. AjaxJson j = new AjaxJson();
  98. ${entityName?uncap_first} = systemService.getEntity(${entityName}Entity.class, ${entityName?uncap_first}.getId());
  99. message = "${ftl_description}删除成功";
  100. ${entityName?uncap_first}Service.delete(${entityName?uncap_first});
  101. systemService.addLog(message, Globals.Log_Type_DEL, Globals.Log_Leavel_INFO);
  102. j.setMsg(message);
  103. return j;
  104. }
  105. /**
  106. * 添加${ftl_description}
  107. *
  108. * @param ids
  109. * @return
  110. */
  111. @RequestMapping(params = "save")
  112. @ResponseBody
  113. public AjaxJson save(${entityName}Entity ${entityName?uncap_first}, HttpServletRequest request) {
  114. String message = null;
  115. AjaxJson j = new AjaxJson();
  116. if (StringUtil.isNotEmpty(${entityName?uncap_first}.getId())) {
  117. message = "${ftl_description}更新成功";
  118. ${entityName}Entity t = ${entityName?uncap_first}Service.get(${entityName}Entity.class, ${entityName?uncap_first}.getId());
  119. try {
  120. MyBeanUtils.copyBeanNotNull2Bean(${entityName?uncap_first}, t);
  121. ${entityName?uncap_first}Service.saveOrUpdate(t);
  122. systemService.addLog(message, Globals.Log_Type_UPDATE, Globals.Log_Leavel_INFO);
  123. } catch (Exception e) {
  124. e.printStackTrace();
  125. message = "${ftl_description}更新失败";
  126. }
  127. } else {
  128. message = "${ftl_description}添加成功";
  129. ${entityName?uncap_first}Service.save(${entityName?uncap_first});
  130. systemService.addLog(message, Globals.Log_Type_INSERT, Globals.Log_Leavel_INFO);
  131. }
  132. j.setMsg(message);
  133. return j;
  134. }
  135. /**
  136. * ${ftl_description}列表页面跳转
  137. *
  138. * @return
  139. */
  140. @RequestMapping(params = "addorupdate")
  141. public ModelAndView addorupdate(${entityName}Entity ${entityName?uncap_first}, HttpServletRequest req) {
  142. if (StringUtil.isNotEmpty(${entityName?uncap_first}.getId())) {
  143. ${entityName?uncap_first} = ${entityName?uncap_first}Service.getEntity(${entityName}Entity.class, ${entityName?uncap_first}.getId());
  144. req.setAttribute("${entityName?uncap_first}Page", ${entityName?uncap_first});
  145. }
  146. return new ModelAndView("${bussiPackage?replace(".","/")}/${entityPackage}/${entityName?uncap_first}");
  147. }
  148. <#-- restful 通用方法生成 -->
  149. @RequestMapping(method = RequestMethod.GET)
  150. @ResponseBody
  151. public List<${entityName}Entity> list() {
  152. List<${entityName}Entity> list${entityName}s=${entityName?uncap_first}Service.getList(${entityName}Entity.class);
  153. return list${entityName}s;
  154. }
  155. @RequestMapping(value = "/{id}", method = RequestMethod.GET)
  156. @ResponseBody
  157. public ResponseEntity<?> get(@PathVariable("id") String id) {
  158. ${entityName}Entity task = ${entityName?uncap_first}Service.get(${entityName}Entity.class, id);
  159. if (task == null) {
  160. return new ResponseEntity(HttpStatus.NOT_FOUND);
  161. }
  162. return new ResponseEntity(task, HttpStatus.OK);
  163. }
  164. @RequestMapping(method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE)
  165. @ResponseBody
  166. public ResponseEntity<?> create(@RequestBody ${entityName}Entity ${entityName?uncap_first}, UriComponentsBuilder uriBuilder) {
  167. //调用JSR303 Bean Validator进行校验,如果出错返回含400错误码及json格式的错误信息.
  168. Set<ConstraintViolation<${entityName}Entity>> failures = validator.validate(${entityName?uncap_first});
  169. if (!failures.isEmpty()) {
  170. return new ResponseEntity(BeanValidators.extractPropertyAndMessage(failures), HttpStatus.BAD_REQUEST);
  171. }
  172. //保存
  173. ${entityName?uncap_first}Service.save(${entityName?uncap_first});
  174. //按照Restful风格约定,创建指向新任务的url, 也可以直接返回id或对象.
  175. String id = ${entityName?uncap_first}.getId();
  176. URI uri = uriBuilder.path("/rest/${entityName?uncap_first}Controller/" + id).build().toUri();
  177. HttpHeaders headers = new HttpHeaders();
  178. headers.setLocation(uri);
  179. return new ResponseEntity(headers, HttpStatus.CREATED);
  180. }
  181. @RequestMapping(value = "/{id}", method = RequestMethod.PUT, consumes = MediaType.APPLICATION_JSON_VALUE)
  182. public ResponseEntity<?> update(@RequestBody ${entityName}Entity ${entityName?uncap_first}) {
  183. //调用JSR303 Bean Validator进行校验,如果出错返回含400错误码及json格式的错误信息.
  184. Set<ConstraintViolation<${entityName}Entity>> failures = validator.validate(${entityName?uncap_first});
  185. if (!failures.isEmpty()) {
  186. return new ResponseEntity(BeanValidators.extractPropertyAndMessage(failures), HttpStatus.BAD_REQUEST);
  187. }
  188. //保存
  189. ${entityName?uncap_first}Service.saveOrUpdate(${entityName?uncap_first});
  190. //按Restful约定,返回204状态码, 无内容. 也可以返回200状态码.
  191. return new ResponseEntity(HttpStatus.NO_CONTENT);
  192. }
  193. @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
  194. @ResponseStatus(HttpStatus.NO_CONTENT)
  195. public void delete(@PathVariable("id") String id) {
  196. ${entityName?uncap_first}Service.deleteEntityById(${entityName}Entity.class, id);
  197. }
  198. <#-- restful 通用方法生成 -->
  199. }