上篇文章介绍了程序整合的准备工作、结合MarshallingView视图完成jaxb2转换XML、xStream转换XML工作,这次将介绍castor、jibx转换XML。

还有MappingJacksonView用Jackson转换JSON,自己拓展AbstractView定义Jsonlib的视图完成JSON-lib转换JSON。

上一篇文章:http://www.cnblogs.com/hoojo/archive/2011/04/29/2032571.html

 

四、 Castor转换XML

1、 castor可以通过一个mapping.xml文件对即将转换的Java对象进行描述,然后可以将Java对象按照描述的情况输出XML内容。利用castor转换xml需要添加如下jar包:

SpringMVC 中整合JSON、XML视图二

如果你还不清楚castor,可以阅读:

for csblogs:http://www.cnblogs.com/hoojo/archive/2011/04/25/2026819.html

for csdn:http://blog.csdn.net/IBM_hoojo/archive/2011/04/25/6360916.aspx

2、 你需要在dispatcher.xml中添加castor的相关视图,配置如下:

<-- 
    继承MarshallingView重写locateToBeMarshalled方法
         解决对象添加到ModelAndView中转换后的xml是BindingResult信息的bug 
-->
<bean name="castorMarshallingView" class="com.hoo.veiw.xml.OverrideMarshallingView">
    <property name="marshaller">
        <bean class="org.springframework.oxm.castor.CastorMarshaller">  
            <property name="mappingLocations">
                <array>
                    <value>classpath:mapping.xml</value>
                </array>
            </property>
            <property name="encoding" value="UTF-8"/>
           </bean>  
    </property>
</bean>

相关文章:

  • 2021-07-24
  • 2021-06-07
  • 2021-11-23
  • 2021-12-27
  • 2021-09-23
  • 2021-04-04
  • 2021-10-23
猜你喜欢
  • 2021-10-29
  • 2022-12-23
  • 2021-06-07
  • 2022-12-23
  • 2022-01-17
  • 2021-04-02
相关资源
相似解决方案