标签:
最近我在将APDPlat升级到Java8,发现最新版本的struts2不支持Java8,同时由于之前有很多的同学希望我把APDPlat的struts2替换为spring mvc,所以我就决定试试看。本文我们看两个转换前后的例子:
1、下拉列表服务,此类比较简单,只涉及一个方法store:
使用struts2:
@Scope("prototype") @Controller @Namespace("/dictionary") public class DicAction extends ExtJSSimpleAction<Dic> { @Resource private DicService dicService; private String dic; private String tree; private boolean justCode; /** * * 此类用来提供下拉列表服务,主要有两种下列类型: * 1、普通下拉选项 * 2、树形下拉选项 * @return 不需要返回值,直接给客户端写数据 * */ public String store(){ Dic dictionary=dicService.getDic(dic); if(dictionary==null){ LOG.info("没有找到数据词典 "+dic); return null; } if("true".equals(tree)){ String json = dicService.toStoreJson(dictionary); Struts2Utils.renderJson(json); }else{ List<Map<String,String>> data=new ArrayList<>(); for(DicItem item : dictionary.getDicItems()){ Map<String,String> map=new HashMap<>(); if(justCode){ map.put("value", item.getCode()); }else{ map.put("value", item.getId().toString()); } map.put("text", item.getName()); data.add(map); } Struts2Utils.renderJson(data); } return null; } public void setJustCode(boolean justCode) { this.justCode = justCode; } public void setTree(String tree) { this.tree = tree; } public void setDic(String dic) { this.dic = dic; } }
使用spring mvc:
@Scope("prototype") @Controller @RequestMapping("/dictionary") public class DicAction extends ExtJSSimpleAction<Dic> { @Resource private DicService dicService; /** * * 此类用来提供下拉列表服务,主要有两种下拉类型: * 1、普通下拉选项 * 2、树形下拉选项 * @param dic * @param tree * @param justCode * @return 返回值直接给客户端 */ @ResponseBody @RequestMapping("/dic!store.action") public String store(@RequestParam(required=false) String dic, @RequestParam(required=false) String tree, @RequestParam(required=false) String justCode){ Dic dictionary=dicService.getDic(dic); if(dictionary==null){ LOG.info("没有找到数据词典 "+dic); return ""; } if("true".equals(tree)){ String json = dicService.toStoreJson(dictionary); return json; }else{ List<Map<String,String>> data=new ArrayList<>(); dictionary.getDicItems().forEach(item -> { Map<String,String> itemMap=new HashMap<>(); if("true".equals(justCode)){ itemMap.put("value", item.getCode()); }else{ itemMap.put("value", item.getId().toString()); } itemMap.put("text", item.getName()); data.add(itemMap); }); String json = JSONArray.fromObject(data).toString(); return json; } } }
2、数据字典服务,此类比较复杂,涉及的方法有create、delete、updatePart、retrieve、query、store:
使用struts2:
@Scope("prototype") @Controller @Namespace("/dictionary") public class DicItemAction extends ExtJSSimpleAction<DicItem> { @Resource private DicService dicService; private String node; /** * 返回数据字典目录树 * @return */ public String store() { if (node == null) { return null; } Dic dic=null; if(node.trim().startsWith("root")){ dic = dicService.getRootDic(); }else{ int id=Integer.parseInt(node); dic = dicService.getDic(id); } if (dic != null) { String json = dicService.toJson(dic); Struts2Utils.renderJson(json); } return null; } public void setNode(String node) { this.node = node; } }
使用spring mvc:
@Scope("prototype") @Controller @RequestMapping("/dictionary") public class DicItemAction extends ExtJSSimpleAction<DicItem> { @Resource private DicService dicService; /** * 返回数据字典目录树 * @param node * @return */ @ResponseBody @RequestMapping("/dic-item!store.action") public String store(@RequestParam(required=false) String node) { if (node == null) { return "[]"; } Dic dic=null; if(node.trim().startsWith("root")){ dic = dicService.getRootDic(); }else{ int id=Integer.parseInt(node); dic = dicService.getDic(id); } if (dic != null) { String json = dicService.toJson(dic); return json; } return "[]"; } @ResponseBody @RequestMapping("/dic-item!query.action") public String query(@RequestParam(required=false) Integer start, @RequestParam(required=false) Integer limit, @RequestParam(required=false) String propertyCriteria, @RequestParam(required=false) String orderCriteria, @RequestParam(required=false) String queryString, @RequestParam(required=false) String search){ super.setStart(start); super.setLimit(limit); super.setPropertyCriteria(propertyCriteria); super.setOrderCriteria(orderCriteria); super.setQueryString(queryString); super.setSearch("true".equals(search)); return super.query(); } @ResponseBody @RequestMapping("/dic-item!retrieve.action") public String retrieve(@ModelAttribute DicItem model) { super.model = model; return super.retrieve(); } @ResponseBody @RequestMapping("/dic-item!delete.action") public String delete(@RequestParam String ids) { super.setIds(ids); return super.delete(); } @ResponseBody @RequestMapping("/dic-item!create.action") public String create(@ModelAttribute DicItem model) { super.model = model; return super.create(); } @ResponseBody @RequestMapping("/dic-item!updatePart.action") public String updatePart(@ModelAttribute DicItem model) { super.model = model; return super.updatePart(); } }
标签:
原文地址:http://my.oschina.net/apdplat/blog/403561