简体   繁体   English

Java 脚轮使用自定义字段处理程序

[英]Java castor using custom field handlers

I have been trying to write a custom field handler which returns the hash code of a java.awt.Image object when writing to XML and retrieves an image based on this hash code when binding the XML to an object. I have been trying to write a custom field handler which returns the hash code of a java.awt.Image object when writing to XML and retrieves an image based on this hash code when binding the XML to an object. For some reason, I can't get this to work;出于某种原因,我无法让它工作; castor, from what I can tell, simply instantiates the field handler and then doesn't call any of its methods.据我所知,castor 只是实例化了字段处理程序,然后不调用它的任何方法。

Can you give me a quick example of how to do this because I must be missing something simple!你能给我一个快速的例子来说明如何做到这一点,因为我一定错过了一些简单的东西!

Cheers,干杯,

Pete皮特

Pasting your code can be a good idea.粘贴代码可能是个好主意。 Anyway following works fine for me无论如何,以下对我来说很好

<m:class name="someClass">
        <m:map-to xml="class"/>
        <m:field name="lineColor" type="java.awt.Color"  handler="ColorFieldHandler">
            <m:bind-xml name="lineColor" node="attribute"/>
        </m:field>
    </m:class>

And the handler itself和处理程序本身

public class ColorFieldHandler extends GeneralizedFieldHandler {
    public Object convertUponGet(Object value) {
        if (value == null) {
            return null;
        }
        Integer colorHash = (Integer) value;
    ...

Hope that helps希望有帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM