controllerTemplate.ftl 10 KB

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