>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>欢迎转载,转载请注明出处-VirgoArt,www.cnblogs.com
一、POM
<dependency> <groupId>commons-digester</groupId> <artifactId>commons-digester</artifactId> <version>2.1</version> </dependency>
二、XML文件准备

<?xml version="1.0" encoding="UTF-8"?> <webTemplate-methods> <method> <name>toListJSP</name> <id>toListJSP</id> <discribe> 列表页面跳转Controller </discribe> <jimport> org.springframework.web.bind.annotation.RequestMapping; org.springframework.web.bind.annotation.RequestMethod; </jimport> <jbean></jbean> <comment> /** * **/ </comment> <body> <![CDATA[ @RequestMapping(value = "/to${className?cap_first}ListJSP",method =RequestMethod.GET) public String to${className?cap_first}ListJSP() { return "${className}/${className}ListJSP"; } ]]> </body> </method> <method> <name>searchListMethod</name> <id>searchListMethod</id> <discribe> 列表数据查询Controller </discribe> <jimport> java.util.Map; java.util.HashMap; org.springframework.web.bind.annotation.RequestMapping; org.springframework.web.bind.annotation.RequestMethod; org.springframework.web.bind.annotation.ResponseBody; </jimport> <jbean> private ${packagePath}.service.${className?cap_first}Service ${className}Service; </jbean> <comment> /** * **/ </comment> <body> <![CDATA[ @RequestMapping(value = "/searchListMethod", method = RequestMethod.GET) @ResponseBody public Map<String, Object> search${className?cap_first}ListMethod() { Map<String , Object> map = new HashMap<String , Object>(); map.put("list",${className}Service.queryAll()); return map; } ]]> </body> </method> <method> <name>toAddJSP</name> <id>toAddJSP</id> <discribe> 数据添加页面跳转Controller </discribe> <jimport> org.springframework.web.bind.annotation.RequestMapping; org.springframework.web.bind.annotation.RequestMethod; </jimport> <jbean></jbean> <comment> /** * **/ </comment> <body> <![CDATA[ @RequestMapping(value = "/toAdd${className?cap_first}JSP",method = RequestMethod.GET) public String toAdd${className?cap_first}JSP() { return "${className}/${className}AddJSP"; } ]]> </body> </method> <method> <name>addMethod</name> <id>addMethod</id> <discribe> 数据添加处理Controller </discribe> <jimport> java.util.Map; java.util.HashMap; org.springframework.web.bind.annotation.RequestMapping; org.springframework.web.bind.annotation.RequestMethod; org.springframework.web.bind.annotation.RequestBody; org.springframework.web.bind.annotation.ResponseBody; </jimport> <jbean> private ${packagePath}.service.${className?cap_first}Service ${className}Service; </jbean> <comment> /** * **/ </comment> <body> <![CDATA[ @RequestMapping(value = "/add${className?cap_first}Method",method =RequestMethod.POST) @ResponseBody public Map<String, Object> add${className?cap_first}Method(@RequestBody ${packagePath}.entity.${className?cap_first} ${className}) { Map <String ,Object> map =new HashMap<String ,Object>(); int flag = ${className}Service.add(${className}); if(flag>0){ map.put("msg","SUCCESS"); }else{ map.put("msg","FAILED"); } return map; } ]]> </body> </method> <method> <name>toUpdateJSP</name> <id>toUpdateJSP</id> <discribe> 数据更新页面跳转Controller </discribe> <jimport> org.springframework.web.bind.annotation.RequestMapping; org.springframework.web.bind.annotation.RequestMethod; </jimport> <jbean></jbean> <comment> /** * **/ </comment> <body> <![CDATA[ @RequestMapping(value = "/toUpdate${className?cap_first}JSP",method =RequestMethod.GET) public String toUpdate${className?cap_first}JSP() { return "${className}/${className}UpdateJSP"; } ]]> </body> </method> <method> <name>queryByIdMethod</name> <id>queryByIdMethod</id> <discribe> 数据更新页面数据获取Controller </discribe> <jimport> org.springframework.web.bind.annotation.RequestMapping; org.springframework.web.bind.annotation.RequestMethod; org.springframework.web.bind.annotation.ResponseBody; </jimport> <jbean> private ${packagePath}.service.${className?cap_first}Service ${className}Service; </jbean> <comment> /** * **/ </comment> <body> <![CDATA[ @RequestMapping(value = "/query${className?cap_first}ByIdMethod",method =RequestMethod.GET) @ResponseBody public Map<String,Object> query${className?cap_first}ByIdMethod(Integer id) { Map <String ,Object> map = new HashMap<String, Object>(); map.put("data",${className}Service.queryById(id)); return map; } ]]> </body> </method> <method> <name>updateMethod</name> <id>updateMethod</id> <discribe> 数据更新处理Controller </discribe> <jimport> java.util.Map; java.util.HashMap; org.springframework.web.bind.annotation.RequestMapping; org.springframework.web.bind.annotation.RequestMethod; org.springframework.web.bind.annotation.RequestBody; org.springframework.web.bind.annotation.ResponseBody; </jimport> <jbean> private ${packagePath}.service.${className?cap_first}Service ${className}Service; </jbean> <comment> /** * **/ </comment> <body> <![CDATA[ @RequestMapping(value = "/update${className?cap_first}Method",method =RequestMethod.POST) @ResponseBody public Map<String, Object> update${className?cap_first}Method(@RequestBody ${packagePath}.entity.${className?cap_first} ${className}) { Map <String ,Object> map = new HashMap<String, Object>(); int flag =${className}Service.update(${className}); if(flag>0){ map.put("msg","SUCCESS"); }else{ map.put("msg","FAILED"); } return map; } ]]> </body> </method> <method> <name>deleteMethod</name> <id>deleteMethod</id> <discribe> 数据删除处理Controller </discribe> <jimport> java.util.Map; java.util.HashMap; org.springframework.web.bind.annotation.RequestMapping; org.springframework.web.bind.annotation.RequestMethod; org.springframework.web.bind.annotation.RequestParam; org.springframework.web.bind.annotation.ResponseBody; </jimport> <jbean> private ${packagePath}.service.${className?cap_first}Service ${className}Service; </jbean> <comment> /** * **/ </comment> <body> <![CDATA[ @RequestMapping(value = "/delete${className?cap_first}Method",method =RequestMethod.GET) @ResponseBody public Map<String, Object> delete${className?cap_first}Method(@RequestParam String id) { Map <String ,Object> map = new HashMap<String ,Object>(); int flag = ${className}Service.delete(Integer.parseInt(id)); if(flag>0){ map.put("msg","SUCCESS"); }else{ map.put("msg","FAILED"); } return map; } ]]> </body> </method> <method> <name>deletesMethod</name> <id>deletesMethod</id> <discribe> 数据批量删除处理Controller </discribe> <jimport> java.util.Map; java.util.HashMap; org.springframework.web.bind.annotation.RequestMapping; org.springframework.web.bind.annotation.RequestMethod; org.springframework.web.bind.annotation.RequestParam; org.springframework.web.bind.annotation.ResponseBody; </jimport> <jbean> private ${packagePath}.service.${className?cap_first}Service ${className}Service; </jbean> <comment> /** * **/ </comment> <body> <![CDATA[ @RequestMapping(value = "/deletes${className?cap_first}Method",method =RequestMethod.GET) @ResponseBody public Map<String, Object> deletes${className?cap_first}Method(@RequestParam List<Integer> ids) { Map <String ,Object> map = new HashMap<String ,Object>(); int flag = ${className}Service.deletes(ids); map.put("msg","SUCCESS"); return map; } ]]> </body> </method> <method> <name>fileDownLoadMethod</name> <id>fileDownLoadMethod</id> <discribe> 文件下载Controller </discribe> <jimport> java.io.FileInputStream; java.io.IOException; java.io.InputStream; java.io.OutputStream; org.springframework.web.bind.annotation.RequestMapping; org.springframework.web.bind.annotation.RequestMethod; javax.servlet.http.HttpServletRequest; javax.servlet.http.HttpServletResponse; org.springframework.web.context.ContextLoader; cn.com.panji.common.util.FileUtils; </jimport> <jbean> </jbean> <comment> /** * **/ </comment> <body> <![CDATA[ @RequestMapping(value ="/${className}/FileDownLoadMethod",method =RequestMethod.GET) public void fileDownLoadMethod(HttpServletRequest request, HttpServletResponse response) { String filePath = (String)request.getParameter("filepath"); String fileName = (String)request.getParameter("filename"); response.setContentType(ContextLoader.getCurrentWebApplicationContext().getServletContext().getMimeType(fileName)); response.setHeader("Content-Disposition","attachment;filename*=utf-8'zh_cn'" + fileName); try { InputStream in =new FileInputStream(filePath); OutputStream out = response.getOutputStream(); int b; while ((b = in.read()) != -1) { out.write(b); } in.close(); out.flush(); out.close(); } catch (IOException e) { e.printStackTrace(); } } ]]> </body> </method> <method> <name>fileUpLoadMethod</name> <id>fileUpLoadMethod</id> <discribe> 文件上传Controller </discribe> <jimport> java.io.File; java.util.HashMap; java.util.Map; org.springframework.web.bind.annotation.RequestMapping; org.springframework.web.bind.annotation.RequestMethod; org.springframework.web.bind.annotation.RequestParam; javax.servlet.http.HttpServletRequest; javax.servlet.http.HttpServletResponse; org.springframework.web.multipart.MultipartFile; cn.com.panji.common.util.FileUtils; </jimport> <jbean> </jbean> <comment> /** * **/ </comment> <body> <![CDATA[ @RequestMapping(value ="/${className?cap_first}/FileUpLoadMethod",method= RequestMethod.GET) @ResponseBody public Map<String ,Object>fileUpLoadMethod(HttpServletRequest request,HttpServletResponse response,@RequestParam("file") MultipartFile file) { Map<String ,Object> map = new HashMap<String ,Object>(); File saveFile =FileUtils.saveFile(file, FileUtils.getRootPath(this) + "/loadFile/"); if(null!=saveFile){ map.put("msg","SUCCESS"); }else{ map.put("msg","FAILED"); } return map; } ]]> </body> </method> <method> <name>searchByIdMethod</name> <id>searchByIdMethod</id> <discribe> 数据查询--By Id--Controller </discribe> <jimport> java.util.Map; java.util.HashMap; org.springframework.web.bind.annotation.RequestMapping; org.springframework.web.bind.annotation.RequestMethod; org.springframework.web.bind.annotation.ResponseBody; </jimport> <jbean> private ${packagePath}.service.${className?cap_first}Service ${className}Service; </jbean> <comment> /** * **/ </comment> <body> <![CDATA[ @RequestMapping(value = "/searchByIdMethod", method = RequestMethod.GET) @ResponseBody public Map<String, Object> search${className?cap_first}ByIdMethod(@RequestParam String id) { Map<String , Object> map = new HashMap(); map.put("data",${className}Service.queryById(Integer.parseInt(id))); return map; } ]]> </body> </method> </webTemplate-methods>
三、创建映射对象

package cn.com.panji.common.resourcemanager.bean.web; import java.util.ArrayList; import java.util.List; import lombok.Data; /** Web资源文件方法集合 * * @author */ @Data public class WebTemplateMethods { private List<WebTemplateMethod> methods = new ArrayList<>(); public List<WebTemplateMethod> getMethods() { return methods; } public void setMethods(List<WebTemplateMethod> methods) { this.methods = methods; } public void addWebTemplateMethod(WebTemplateMethod method) { methods.add(method); } }

package cn.com.panji.common.resourcemanager.bean.web; import java.util.Arrays; import java.util.List; import cn.com.panji.common.util.StringUtils; import lombok.Data; /** Web资源文件方法实体 * * @author */ @Data public class WebTemplateMethod { private String name; private String discribe; private String id; private String comment; private String jimport; private String jbean; private String body; public String getBody() { // body.replace("<", "<"); // body.replace(">", ">"); return body; } public void setBody(String body) { this.body = body; } public String getDiscribe() { return discribe; } public void setDiscribe(String discribe) { this.discribe = discribe; } public String getComment() { return comment; } public void setComment(String comment) { this.comment = comment; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getJimport() { return jimport; } public List<String> getJimportList() { jimport = StringUtils.removeAllBlank(jimport); return Arrays.asList(jimport.split(";")); } public void setJimport(String jimport) { this.jimport = jimport; } public String getJbean() { return jbean; } public List<String> getJbeanList() { jbean = StringUtils.trim(jbean); return Arrays.asList(jbean.split(";")); } public void setJbean(String jbean) { this.jbean = jbean; } public String getId() { return id; } public void setId(String id) { this.id = id; } }
四、Digester解析配置与应用

package cn.com.panji.common.resourcemanager.api.impl; import java.io.IOException; import java.util.ArrayList; import java.util.Arrays; import java.util.HashMap; import java.util.List; import java.util.Map; import org.apache.commons.digester.Digester; import org.xml.sax.SAXException; import cn.com.panji.common.resourcemanager.ResourceEnum; import cn.com.panji.common.resourcemanager.api.TemplateXMLReader; import cn.com.panji.common.resourcemanager.bean.web.WebTemplateMethod; import cn.com.panji.common.resourcemanager.bean.web.WebTemplateMethods; public class WebTemplateXMLReader extends TemplateXMLReader { static final String WEB_TEMPLEATE_XML_PATH = "resourceTemplates/" + ResourceEnum.XML_TEMPLATE_WEB.getResourceTemplatePath() + ".xml"; private WebTemplateMethods methods = null; private WebTemplateXMLReader() { configDigester(); parse(); } @Override public void configDigester() { this.digester = new Digester(); this.digester.setValidating(false); // 配置映射 this.digester.addObjectCreate("webTemplate-methods", WebTemplateMethods.class); this.digester.addObjectCreate("webTemplate-methods/method", WebTemplateMethod.class); this.digester.addBeanPropertySetter("webTemplate-methods/method/name", "name"); this.digester.addBeanPropertySetter("webTemplate-methods/method/id", "id"); this.digester.addBeanPropertySetter("webTemplate-methods/method/discribe", "discribe"); this.digester.addBeanPropertySetter("webTemplate-methods/method/jimport", "jimport"); this.digester.addBeanPropertySetter("webTemplate-methods/method/jbean", "jbean"); this.digester.addBeanPropertySetter("webTemplate-methods/method/comment", "comment"); this.digester.addBeanPropertySetter("webTemplate-methods/method/body", "body"); this.digester.addSetNext("webTemplate-methods/method", "addWebTemplateMethod"); } @Override public void parse() { try { methods = (WebTemplateMethods) this.digester .parse(this.getClass().getClassLoader().getResourceAsStream(WEB_TEMPLEATE_XML_PATH)); } catch (IOException | SAXException e) { e.printStackTrace(); } } @Override public WebTemplateMethod findResource(String id) { for (WebTemplateMethod m : methods.getMethods()) { if (id.equals(m.getId())) { return m; } } return null; } @Override public Object findResourceMap(String[] ids) { Map<String, WebTemplateMethod> map = new HashMap<>(); List<String> idList = Arrays.asList(ids); for (WebTemplateMethod m : methods.getMethods()) { if (idList.contains(m.getId())) { map.put(m.getId(), m); } } return map; } @Override public Object findResourceList(String[] ids) { List<WebTemplateMethod> list = new ArrayList<WebTemplateMethod>(); List<String> idList = Arrays.asList(ids); for (WebTemplateMethod m : methods.getMethods()) { if (idList.contains(m.getId())) { list.add(m); } } return list; } public static TemplateXMLReader getInstance() { return new WebTemplateXMLReader(); } }
WebTemplateMethod web = (WebTemplateMethod) TemplateReaderFactory.getTemplateXMLReaderInstance(ResourceEnum.XML_TEMPLATE_WEB).findResource("updateMethod");
五、解释及注意事项
1.XML结构应与映射对象层次结构相同,解析时,可解析到最末端元素,关于List数据,使用事件驱动器进行反射,当扫描到元素时,调动相应的Add方法。
2.Digester使用反射Setter方式进行属性注入,需要保证存在有效的Setter方法。
3.项目中可用该方式实现更加清晰的项目参数配置文件以及解析。
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>关于节点属性数据获取等内容后续更新