秀外慧中的springMVC(六)---输入和输出使用xml格式方式

可以在方法上使用@ResponseBody返回单个vo并转换为xml返回数据,当然如果想传入xml转换实体使用@RequestBody和   json的传入传出是一样的.

springmvc配置

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:security="http://www.springframework.org/schema/security"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
	xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:jee="http://www.springframework.org/schema/jee" xmlns:mvc="http://www.springframework.org/schema/mvc"
	xsi:schemaLocation="  
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd  
        http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-3.1.xsd  
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd  
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd  
        http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-3.1.xsd  
        http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.1.xsd  
        http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.1.xsd  
        http://www.springframework.org/schema/jms http://www.springframework.org/schema/jms/spring-jms-3.1.xsd  
        http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang-3.1.xsd  
        http://www.springframework.org/schema/oxm http://www.springframework.org/schema/oxm/spring-oxm-3.1.xsd  
        http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.1.xsd  
        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.1.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd">

<!--
SpringMVC视图选择器 
 -->
	<bean id="viewResolver"	class="org.springframework.web.servlet.view.InternalResourceViewResolver">
	  <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"></property>  
		<property name="prefix" value="/WEB-INF/webPage/" />
		<property name="suffix" value=".jsp" />
	</bean>
	
	
<!--
返回xml
 -->	
	<bean class="org.springframework.web.servlet.view.xml.MarshallingView">
        <constructor-arg>
            <bean class="org.springframework.oxm.jaxb.Jaxb2Marshaller">
                <property name="classesToBeBound">
                    <list>
                        <value>com.wechat.pdcquery.vo.User</value>
                    </list>
                </property>
            </bean>
        </constructor-arg>
    </bean>

<!--
返回json(需要额外的包支持) 
 -->	  
 <!-- 
	<bean id="mappingJacksonHttpMessageConverter" class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">  
	    <property name="supportedMediaTypes">  
	        <list>  
	            <value>text/html;charset=UTF-8</value>
	            <value>application/json;charset=UTF-8</value>
	              
	        </list>  
	    </property>  
	</bean> 
	<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" >  
	    <property name="messageConverters">  
	        <list>  
	            <ref bean="mappingJacksonHttpMessageConverter"/>  
	        </list>  
	    </property>  
	</bean>  
	 -->
</beans>  

实体需要注解以便转换为xml

package com.wechat.pdcquery.vo;

import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement(name="user")
public class User {
    
    private int id;
    private String name;
    private String address;
    
    public int getId() {
        return id;
    }
    @XmlElement
    public void setId(int id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    
    @XmlElement
    public void setName(String name) {
        this.name = name;
    }
    public String getAddress() {
        return address;
    }
    @XmlElement
    public void setAddress(String address) {
        this.address = address;
    }    
}
controller

    @RequestMapping(value="/users", method = RequestMethod.GET)
    public @ResponseBody User getUsers() {    
        //模拟从数据库中获得数据....        
            User user = new User();
            user.setId(1);
            user.setName("Name: aaa");
            user.setAddress("Address: bbb");
            return user;            
     }

转换list

实体bean

package com.wechat.pdcquery.vo;

import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement(name="user")
public class User {
    
    private int id;
    private String name;
    private String address;
    
    public int getId() {
        return id;
    }
    @XmlElement
    public void setId(int id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    
    @XmlElement
    public void setName(String name) {
        this.name = name;
    }
    public String getAddress() {
        return address;
    }
    @XmlElement
    public void setAddress(String address) {
        this.address = address;
    }    
}

package com.wechat.pdcquery.vo;

import java.util.List;

import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlElements;
import javax.xml.bind.annotation.XmlRootElement;

@SuppressWarnings("unchecked")
@XmlRootElement
public class ListBean {
    private String name;
    private List list;

	@XmlElements({ @XmlElement(name = "user", type = User.class) })
    public List getList() {
        return list;
    }
 
    public void setList(List list) {
        this.list = list;
    }
 
    public String getName() {
        return name;
    }
 
    public void setName(String name) {
        this.name = name;
    }
}
controller
@RequestMapping(value = "/querynodata", method = { RequestMethod.GET,
			RequestMethod.POST })
	public @ResponseBody
	ListBean querynodata(HttpServletRequest request, ModelMap map) {

		List<User> list = new ArrayList<>();
		User user = new User();
		user.setId(1);
		user.setName("Name: aaa");
		user.setAddress("Address: bbb");
		list.add(user);
		list.add(user);
		// moView.addObject(list);
		ListBean listBean = new ListBean();
		listBean.setList(list);
		listBean.setName("ffff");
		return listBean;
		
	}

map同理只要xml注解规范就会转换,如果不能转换则会调用下一个转换器进行转换

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值