自定义Spring MVC中的数据绑定

2023-05-16

默认情况下,spring mvc的数据映射的实现是自动查找请求中的key为参数名的parameter的值。比如有以下方法:

 

	@RequestMapping(value="/xml", method=RequestMethod.POST)
	public String xmlData(String name, Integer age){
		System.out.println(name+" "+age);
		return "/index";
	}
	

则Spring MVC会使用如下的方法给参数注入值:

 

 

name = request.getParameter("name");
age= new Integer(request.getParameter("age"));

如果想要实现一些自定义的解析方式,又该如何做呢?

 

 

Spring提供了一个annotation org.springframework.web.bind.annotation.InitBinder 可以用来做一些简单的、自定义的解析的实现。

 

例如请求内容如下:

 


<a>
	<name>sss</name>
	<age>21</age>
</a>  

这里希望解析完以后,方法中的参数name和age就是对应的xml内容中的name和age的元素的值。

 

 

首先定义一个添加了  InitBinder 注解的方法:

	@InitBinder
	public void addBinder(WebDataBinder webDataBinder, WebRequest webRequest){
		。。。
	}

根据InitBinder的文档说明,以它注解的方法上可以添加很多参数(同RequestMapping) ,典型的参数就是上面看到的WebDataBinder和WebRequest。

 

由于需要的参数类型分别是String和Integer,所以这里添加对于String和Integer类型的自定义的解析的支持,如下:

	@InitBinder
	public void addBinder(WebDataBinder webDataBinder, WebRequest webRequest){
		webDataBinder.registerCustomEditor(String.class, new CustomPropertyEditor(webDataBinder.getObjectName(), webRequest));
		webDataBinder.registerCustomEditor(Integer.class, new CustomPropertyEditor(webDataBinder.getObjectName(), webRequest));
	}

这里 webDataBinder.getObjectName() 里取出来的值就分别是name和age,根据参数的个数,这个方法会被调用多次。

 

下面就是CustomPropertyEditor的实现了:

public class CustomPropertyEditor extends  PropertyEditorSupport {

	private WebRequest WebRequest;
	private String objectName;
	private String value;

	public CustomPropertyEditor(String objectName, WebRequest webRequest) {
		super();
		WebRequest = webRequest;
		this.objectName = objectName;
		extractXML();
	}

	private void extractXML() {
		String xmlContent = WebRequest.getParameter("xml");
		if(xmlContent != null){
			try {
				DocumentBuilder documentBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
				ByteArrayInputStream inputStream = new ByteArrayInputStream(xmlContent.getBytes());
				
				Document document = documentBuilder.parse(inputStream);
				NodeList elementsByTagName = document.getElementsByTagName(objectName);
				if(elementsByTagName != null && elementsByTagName.getLength() > 0){
					value = elementsByTagName.item(0).getTextContent();
				}
			} catch (ParserConfigurationException | SAXException | IOException e) {
				e.printStackTrace();
			}
		}
	}

	@Override
	public Object getValue() {
		return value;
	}

}

  

这里就是简单的提取xml中的内容,然后把提供的内容作为getValue()方法的返回值输出就可以了。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

自定义Spring MVC中的数据绑定 的相关文章

随机推荐

  • Markdown学习记录

    Markdown 1 代码块 xff1a 代码块语法 xff1a 96 96 96 python 96 96 96 shell 1 python代码 print 34 Hello World 34 2 shell脚本 linux下重启的命令
  • MAC终端代理设置

    移动开发有时需要设置代理 xff0c 不然太慢 在终端中输代码即可显示隐藏文件 defaults write com apple finder AppleShowAllFiles boolean true killall Finder 再次
  • C#:如何查看.net core版本?

    C xff1a 如何查看 net core版本 xff1f 打开控制面板 xff0c 选择 程序和功能 xff0c 找到下图选项 xff0c 即可查看 net core版本 检查是否已正确安装所有内容 xff1a 安装完成后 xff0c 打
  • Qt信号槽如何传递参数

    Qt信号槽如何传递参数 利用 Qt 进行程序开发时 xff0c 有时需要信号 槽来完成参数传递 带参数的信号 槽在使用时 xff0c 有几点需要注意的地方 xff0c 下面结合实例进行介绍 1 当信号与槽函数的参数数量相同时 xff0c 它
  • 解决 zsh: command not found 报错

    问题描述 最近在开发 Go 项目 xff0c 使用 go get u xxxx 成功下载安装包后 xff0c 在终端执行新下载包的命令 xff0c 一直报 zsh command not found 的错误 一开始以为是包没安装成功 xff
  • Ubuntu 安装scipy错误解决办法

    在ubuntu 14 04使用pip3 install scipy时报错 xff1a numpy distutils system info NotFoundError no lapack blas resources found 百度了一
  • LA5016-IIC EEPROM协议解析

    写入 LA5016 解析协议设置 xff1a 波形 读取 波形 xff1a
  • Linux下安装oracle数据库提示DISPLAY not set. Please set the DISPLAY and try again.解决方法

    问题描述 xff1a Linux下安装oracle数据库提示DISPLAY not set Please set the DISPLAY and try again 如下图所示 xff1a 解决办法 xff1a 切换到root 用户 xff
  • html 清除缓存样式

    autocomplete 61 off
  • ArcGIS Server for linux 服务无法启动解决简记

    今天在一台Linux虚拟机上安装了一个ArcGIS Server For Linux 只ArcSOC 组件 xff0c 一切正常 xff0c 但是启动服务的时候报一下的错误 xff1a root 64 rhsde scripts start
  • Java中swap()方法的实现

    为了能更多的掌握C C 43 43 xff0c 时不时的就会拿起一本什么书看看 昨天又看到了请指针和引用的部分 xff0c 又会有经典的swap 方法的实现 几乎所有人都知道了 xff0c 要实现一个正确的swap 方法需要以指针或引用为参
  • 渐变色原理

    引用 http www islandcn com post 311 html 在图象图形的编程中 经常会见到渐变色以及各种图片的叠加等效果 这篇文章就是要对这些效果的原理加以分析 并在Elastos 操作系统 Mobile Edition
  • JAX-WS 学习二:创建客户端

    上一节中介绍了怎么基于JAX WS的API创建服务端 xff0c 这一节介绍一下创建一个客户端调用WebService服务 要创建一个Client端也相当简单 xff0c 不过需要知道几个东西 xff1a 1 wsdl文件路径 需要读取服务
  • 使用JDI监听Java程序运行

    Java虚拟机提供了一套用于调试 xff08 JVMDI xff09 和监视 xff08 JVMPI xff09 的接口 xff0c Java5之后统一为JVMTI xff1a http docs oracle com javase 1 5
  • 使用CXF和camel-cxf调用webservice

    CXF是什么 Apache CXF 是一个开源的 全功能的WebService框架 xff0c 它提供了一套工具和API来帮助开发和构建WebService xff0c 像 JAX WS 和 JAX RS 它也支持许多WebService标
  • Java国际化:BreakIterator

    译自 xff1a http tutorials jenkov com java internationalization breakiterator html xff0c 不准确别怪我 java text BreakIterator 类用来
  • build-helper-maven-plugin的使用

    build helper maven plugin 插件主要的作用就是用来指定额外的源码路径和资源路径 它总共有15个goal xff1a build helper add source Add more source directorie
  • tkinter进阶版——ttk

    很长的一段时间里 xff0c 我都是用tkinter进行GUI设计的 xff0c 还写过一篇 tkinter模块常用参数 但后来慢慢地觉得 xff0c 这个tkinter真的是有点丑啊 于是 xff0c 找到了现在的ttk ttk是什么呢
  • Spring 参考资料

    拦截器顺序参考 xff1a http www cnblogs com yjmyzz p how to custom filter provider and token in spring security3 html Spring框架详细参
  • 自定义Spring MVC中的数据绑定

    默认情况下 xff0c spring mvc的数据映射的实现是自动查找请求中的key为参数名的parameter的值 比如有以下方法 xff1a 64 RequestMapping value 61 34 xml 34 method 61