| | |
| | | import ${entity.clazz}; |
| | | import ${service.clazz}; |
| | | import ${query.clazz}; |
| | | |
| | | <#if responseBody> |
| | | <#else> |
| | | import java.io.PrintWriter; |
| | | </#if> |
| | | @Controller |
| | | @RequestMapping("${mapping}") |
| | | public class ${controllerName} { |
| | |
| | | @Resource |
| | | private ${service.name} ${serviceObjName}; |
| | | |
| | | |
| | | private String loadPrint(String callback, String root){ |
| | | <#macro print data> |
| | | <#if responseBody> |
| | | <#if jsonp> |
| | | return JsonUtil.loadJSONP(callback,root); |
| | | return JsonUtil.loadJSONP(callback,${data}); |
| | | <#else> |
| | | return root; |
| | | return ${data}; |
| | | </#if> |
| | | } |
| | | <#else> |
| | | <#if jsonp> |
| | | out.print(JsonUtil.loadJSONP(callback,${data})); |
| | | return; |
| | | <#else> |
| | | out.print(${data}); |
| | | return; |
| | | </#if> |
| | | </#if> |
| | | </#macro> |
| | | |
| | | @ResponseBody |
| | | ${responseBody?string("@ResponseBody","")} |
| | | @RequestMapping("list") |
| | | public String list(${query.name} query, int page, int limit, String callback) { |
| | | public ${responseBody?string("String","void")} list(${query.name} query, int page, int limit${jsonp?string(", String callback","")}${responseBody?string("",",PrintWriter out")} ) { |
| | | List<${entity.name}> list = ${serviceObjName}.list(query,page,limit); |
| | | long count = ${serviceObjName}.count(query); |
| | | JSONObject data = new JSONObject(); |
| | |
| | | |
| | | data.put("list", gson.toJson(list)); |
| | | data.put("count", count); |
| | | return loadPrint(callback,JsonUtil.loadTrueResult(data)); |
| | | <@print data="JsonUtil.loadTrueResult(data)" > </@print> |
| | | } |
| | | |
| | | <#if delete> |
| | | @ResponseBody |
| | | ${responseBody?string("@ResponseBody","")} |
| | | @RequestMapping("delete") |
| | | public String delete(String ids, String callback) { |
| | | public ${responseBody?string("String","void")} delete(String ids${jsonp?string(", String callback","")}${responseBody?string("",",PrintWriter out")}) { |
| | | Type type = new TypeToken<List<${identifyIdType}>>(){}.getType(); |
| | | List<${identifyIdType}> idList=new Gson().fromJson(ids,type); |
| | | ${serviceObjName}.delete(idList); |
| | | return loadPrint(callback, JsonUtil.loadTrueResult("")); |
| | | <@print data="JsonUtil.loadTrueResult(\"\")" > </@print> |
| | | } |
| | | </#if> |
| | | |
| | | <#if add> |
| | | @ResponseBody |
| | | ${responseBody?string("@ResponseBody","")} |
| | | @RequestMapping("add") |
| | | public String add(${entity.name} bean, HttpSession session, String callback) { |
| | | public ${responseBody?string("String","void")} add(${entity.name} bean, HttpSession session${jsonp?string(", String callback","")}${responseBody?string("",",PrintWriter out")}) { |
| | | try{ |
| | | ${serviceObjName}.add(bean); |
| | | return loadPrint(callback, JsonUtil.loadTrueResult("")); |
| | | <@print data="JsonUtil.loadTrueResult(\"\")" > </@print> |
| | | }catch(Exception e){ |
| | | return loadPrint(callback, JsonUtil.loadFalseResult(e.getMessage())); |
| | | <@print data="JsonUtil.loadFalseResult(e.getMessage())" > </@print> |
| | | } |
| | | } |
| | | </#if> |
| | | |
| | | @ResponseBody |
| | | ${responseBody?string("@ResponseBody","")} |
| | | @RequestMapping("get") |
| | | public String get(${identifyIdType} id, HttpSession session, String callback) { |
| | | public ${responseBody?string("String","void")} get(${identifyIdType} id, HttpSession session${jsonp?string(", String callback","")}${responseBody?string("",",PrintWriter out")}) { |
| | | ${entity.name} entity = ${serviceObjName}.get(id); |
| | | if (entity !=null){ |
| | | return loadPrint(callback,JsonUtil.loadTrueResult(entity)); |
| | | <@print data="JsonUtil.loadTrueResult(entity)" > </@print> |
| | | } else { |
| | | return loadPrint(callback,JsonUtil.loadFalseResult("ID不存在")); |
| | | <@print data="JsonUtil.loadFalseResult(\"ID不存在\")" > </@print> |
| | | } |
| | | } |
| | | |
| | | |
| | | <#if update> |
| | | @ResponseBody |
| | | ${responseBody?string("@ResponseBody","")} |
| | | @RequestMapping("update") |
| | | public String update(${entity.name} bean, HttpSession session,String callback) { |
| | | public ${responseBody?string("String","void")} update(${entity.name} bean, HttpSession session${jsonp?string(", String callback","")}${responseBody?string("",",PrintWriter out")}) { |
| | | if (bean.getId() == null) { |
| | | return loadPrint(callback, JsonUtil.loadFalseResult("ID不能为空")); |
| | | <@print data="JsonUtil.loadFalseResult(\"ID不能为空\")" > </@print> |
| | | } |
| | | try{ |
| | | ${serviceObjName}.update(bean); |
| | | }catch(Exception e){ |
| | | return loadPrint(callback,JsonUtil.loadFalseResult(e.getMessage())); |
| | | <@print data="JsonUtil.loadFalseResult(e.getMessage())" > </@print> |
| | | } |
| | | return loadPrint(callback,JsonUtil.loadTrueResult("")); |
| | | <@print data="JsonUtil.loadTrueResult(\"\")" > </@print> |
| | | } |
| | | </#if> |
| | | |