2013-04-26 69 views
1

<--> JS我已经给出生成的类的非常复杂的堆栈和需要在服务器,并使用DWR客户端之间发送。其中一个类使用jax.xml.datatype.Duration。这里是我创建一个bean使用的持续时间的测试数据中的Java代码:如何马歇尔javax.xml.datatype.Duration中的Java使用Spring/DWR

DatatypeFactory df = DatatypeFactory.newInstance(); 
Duration duration = df.newDuration(RANDOM.nextLong()); 
testObject.setDuration(duration); 

DWR不喜欢在对象持续时间:

ERROR [org.directwebremoting.dwrp.DefaultConverterManager] - 没有为'com.sun.org.apache.xerces.internal.jaxp.datatype.DurationImpl'找到的转换器'

偶尔我看到相同的消息(我认为是从JavaScript返回到Java时)类com.sun.org.apache.xerces.internal.jaxp.datatype.DurationImpl

定义常规转换器没有帮助:

<dwr:convert type="bean" match="com.sun.org.apache.xerces.internal.jaxp.datatype.DurationImpl"/> 
<dwr:convert type="bean" match="javax.xml.datatype.Duration"/> 

我敢肯定,上述不起作用,因为他们并不是真正的豆子。但是我不知道如何让它发挥作用。我已经在几个地方看过,你可以定义一个自定义转换器,但细节看起来非常模糊,在大多数情况下过时。 DWR网站上说有一个链接可以解释这一切,但它只是将您链接到javadoc,它并没有帮助我实际执行。

任何人可以帮助我弄清楚如何处理时间?

回答

2

我无法找到所提供的答案一个地方,所以我必须的信息一起从不同的地方一片位 - 弹簧DWR架构,扩展提供的类实例等

对于初学者来说,这是配置spring.xml的正确方法:

<dwr:configuration> 
    <dwr:init> 
     <dwr:converter id="durationConverter" class="com.foo.DurationConverter"/> 
    </dwr:init> 

    <dwr:convert class="com.sun.org.apache.xerces.internal.jaxp.datatype.DurationImpl" type="durationConverter"/> 
    <dwr:convert class="javax.xml.datatype.Duration" type="durationConverter"/> 
    ... 
    </dwr:configuration> 

我真的不知道,如果我需要两个DurationImpl和时间转换的声明,但我看到的消息对他们俩的,所以我把它们都放进去了。

他重新是我如何建立我的转换器:

public class DurationConverter extends BeanConverter { 

    @Override 
    public Duration convertInbound(Class<?> type, InboundVariable iv, InboundContext ic) throws MarshallException { 
     String value = iv.getValue(); 

     // If the text is null then the whole bean is null 
     if (value == null) { 
      return null; 
     } 

     Duration duration = null; 
     try { 
      DatatypeFactory df = DatatypeFactory.newInstance(); 
      duration = df.newDuration(Long.valueOf(value)); 

     } catch (DatatypeConfigurationException ex) { 
      Logger.getLogger(DurationConverter.class.getName()).log(Level.SEVERE, null, ex); 
     } 

     return duration; 
    } 

    @Override 
    public OutboundVariable convertOutbound(Object o, OutboundContext oc) throws MarshallException { 
     Duration duration = (Duration) o; 
     String varname = oc.getNextVariableName(); 
     Map<String, OutboundVariable> ovs = new TreeMap<>(); 
     OutboundVariable durationOV = getConverterManager().convertOutbound(duration.getSeconds(), oc); 

     ovs.put("duration", durationOV); 

     ObjectJsonOutboundVariable oj = new ObjectJsonOutboundVariable(); 
     oj.setChildren(ovs); 

     return oj; 
    } 
} 

希望这可以帮助别人挣扎着同样的事情。

1

我不知道DWR,但在查看com.sun.org.apache.xerces.internal.jaxp.datatype.DurationImpl时,没有公共的默认构造函数,并且我在JAXB之前看到了这种类型的问题类似的情况。在JAXB中,我在包中使用了一个package-info.java文件,该文件包含您的案例持续时间中包含的对象定义(在我的情况下是java.util.Locale)。我的对象存储在com.foo.domain中。现在

@XmlJavaTypeAdapter(type = java.util.Locale.class, value=com.foo.util.LocaleXmlAdapter.class) 
package com.foo.domain; 
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter; 

我LocaleXmlAdapter.class定义

public class LocaleXmlAdapter extends XmlAdapter<String, Locale> { 
    @Override 
    public Locale unmarshal(String localeString) throws Exception { 
     {insert your impl} 
    } 

    @Override 
    public String marshal(Locale locale) throws Exception { 
     {insert your impl} 
    } 
} 

这使得JAXB解决缺少默认构造函数的。

+0

良好的建议和解决方案,一个非常类似的问题。不幸的是,它解决了Java和XML之间的编组问题。我需要告诉DWR如何处理Java和JavaScript之间的编组。 – Bal 2013-04-29 15:04:43

+0

是的,我明白了。快速查看源代码,您可能需要做点修改才能使您的东西起作用,从源代码中检查此包,它看起来很有前途,因为它们具有用于其他没有默认构造函数的类的转换器。 http://svn.directwebremoting.org/dwr/trunk/core/impl/main/java/org/directwebremoting/convert/ – fpmoles 2013-04-29 19:28:45