>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>欢迎转载,转载请注明出处-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("&lt;", "<");
// body.replace("&gt;", ">");
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.项目中可用该方式实现更加清晰的项目参数配置文件以及解析。

>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>关于节点属性数据获取等内容后续更新

XML数据读取——Digester简单使用的更多相关文章

  1. XML数据读取方式性能比较(一)

    原文:XML数据读取方式性能比较(一) 几个月来,疑被SOA,一直在和XML操作打交道,SQL差不多又忘光了.现在已经知道,至少有四种常用人XML数据操作方式(好像Java差不多),不过还没有实际比较 ...

  2. 以对象的方式来访问xml数据表(二)

    为什么要以对象的方式来访问xml数据表? 还记得,自己是在一次完成师兄布置的任务时接触到了xml,那时候需要用xml来作为数据文件,保存一个简单的图书管理系统的数据.于是就知道了,可以用xml文件来保 ...

  3. c#调用JAVA的Webservice处理XML数据及批量轮询的实现方法

    前段时间做一个调用外单位WEBSERVICE的项目,项目完成的功能其实很简单,就是我们单位有很多车友会员,我们想对他们提供车辆违章信息告之服务!我们这边交警部门给我们开放了WS的接口,我们就是想通过这 ...

  4. 使用dom4j中SAXReader解析xml数据

    public ApiConfig(String configFilePath) throws DocumentException{ SAXReader reader = new SAXReader() ...

  5. Extjs的数据读取器store和后台返回类型简单解析

    工作中用到了Extjs,从后台获取数据的时候,用到了extjs自己的Ext.data.store方法,然后封装了ExtGridReturn方法, 目的:前台用到Ext.data.store读取从后台传 ...

  6. InnerException 消息是“反序列化对象 属于类型 *** 时出现错误。读取 XML 数据时,超出最大字符串内容长度配额 (8192)。(注意细节)

    WEB站点在调用我们WCF服务的时候,只要传入的参数过长,就报如下错误: 格式化程序尝试对消息反序列化时引发异常: 尝试对参数 http://tempuri.org/ 进行反序列化时出错: formD ...

  7. C#基础笔记---浅谈XML读取以及简单的ORM实现

    背景: 在开发ASP.NETMVC4 项目中,虽然web.config配置满足了大部分需求,不过对于某些特定业务,我们有时候需要添加新的配置文件来记录配置信息,那么XML文件配置无疑是我们选择的一个方 ...

  8. C#基础---浅谈XML读取以及简单的ORM实现

    背景: 在开发ASP.NETMVC4 项目中,虽然web.config配置满足了大部分需求,不过对于某些特定业务,我们有时候需要添加新的配置文件来记录配置信息,那么XML文件配置无疑是我们选择的一个方 ...

  9. XML教程、语法手册、数据读取方式大全

    XML简单易懂教程 本文提供全流程,中文翻译.Chinar坚持将简单的生活方式,带给世人!(拥有更好的阅读体验 -- 高分辨率用户请根据需求调整网页缩放比例) 一 XML --数据格式的写法 二 Re ...

随机推荐

  1. H3C WAP712C 路由器设置

    0.做完任何设置之后都要执行保存操作,否则断电后设置会丢失! 1.默认登录参数:IP:192.168.0.50ID:adminPD:h3capadmin 2.修改默认IP地址:设备 --> 接口 ...

  2. CC++语法::数组名退化(array decaying)

    参考: CSDN::C/C++中数组名退化为指针的情况 stackoverflow::What is array decaying? 起因 笔者在写memset的时候总想偷一点懒(因为我们一般都是为了 ...

  3. request 获取body内容

    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException ...

  4. atool-build 打包项目报JavaScript heap out of memory 错误

    出现这个问题的原因,是因为NodeJS给JavaScript设置了默认内存使用限制(32位系统下0.7GB,64位系统下1.4GB),就算我们的电脑内存是64GB的,在使用atool-build打包时 ...

  5. 多模块项目Module must not contain source root. The root already belongs to module

    多模块项目Module "*" must not contain source root *. The root already belongs to module "* ...

  6. Spring ElasticsearchTemplate 经纬度按距离排序

    es实体,用 @GeoPointField 注解,值为:中间逗号隔开,如 29.477000,119.278536(经度, 维度) @Document(indexName = "v_inte ...

  7. springboot-文件上传xls及POI操作Excel

    1.pom导入依赖文件 <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-o ...

  8. 415 DOM 查找列表框、下拉菜单控件、对表格元素/表单控件进行增删改操作、创建元素并且复制节点与删除、 对表格操作、通用性和标准的事件监听方法(点击后弹窗效果以及去掉效果)

    DOM访问列表框.下拉菜单的常用属性: form.length.options.selectedindex.type       使用options[index]返回具体选项所对应的常用属性:defa ...

  9. 阿里云服务器+ftp文件操作+基于Centos7的vsftpd配置

    路径问题:一定要注意此位置是否需要加入"/" 文件上传方式:被动模式 vsftp完整配置: # # The default compiled in settings are fai ...

  10. 2018-2019-2 20165234 《网络对抗技术》 Exp5 MSF基础应用

    实验五 MSF基础应用 实验内容 本实践目标是掌握metasploit的基本应用方式,重点常用的三种攻击方式的思路.具体需要完成: 1.一个主动攻击实践,ms08_067(成功) 2. 一个针对浏览器 ...