2013-04-26 48 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