springmvc controller中返回已经生成的xml数据
2018-03-29 11:05
417 查看
1.上代码
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<context:component-scan base-package="com.yitong.controller"/>
<mvc:annotation-driven/>
<!-- 通过mvc:resources设置静态资源,这样servlet就会处理这些静态资源,而不通过控制器 -->
<!-- 设置不过滤内容,比如:css,jquery,img 等资源文件 -->
<mvc:resources location="/*.html" mapping="/**.html" />
<mvc:resources location="/css/*" mapping="/css/**" />
<mvc:resources location="/js/*" mapping="/js/**" />
<mvc:resources location="/images/*" mapping="/images/**" />
<!--
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
-->
</beans> 3.访问:
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.ResponseBody; @Controller public class XmlController { @RequestMapping(value = "/xml", produces={"application/xml; charset=UTF-8"}, method = { RequestMethod.GET, RequestMethod.POST }) @ResponseBody public String xml(HttpServletRequest request, HttpServletResponse response) throws Exception { String xmlData = "<person>" + "<name>tom</name>" + "<age>17</age>" + "</person>"; return xmlData; } }2.springmvc配置文件如下:<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<context:component-scan base-package="com.yitong.controller"/>
<mvc:annotation-driven/>
<!-- 通过mvc:resources设置静态资源,这样servlet就会处理这些静态资源,而不通过控制器 -->
<!-- 设置不过滤内容,比如:css,jquery,img 等资源文件 -->
<mvc:resources location="/*.html" mapping="/**.html" />
<mvc:resources location="/css/*" mapping="/css/**" />
<mvc:resources location="/js/*" mapping="/js/**" />
<mvc:resources location="/images/*" mapping="/images/**" />
<!--
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
-->
</beans> 3.访问:
相关文章推荐
- SpringMVC -Controller返回xml数据
- springmvc controller中返回已生成的json数据
- SpringMVC常用配置-Controller返回格式化数据如JSON、XML的配置方式和机制
- springmvc 返回xml 数据配置
- springmvc接口返回xml数据出现中文乱码的问题
- springMVC Controller返回json数据中中文是问号解决
- SpringMVC 中 jsp 页面对 Controller 返回数据的使用
- springmvc 解析url 返回的xml数据
- SpringMvc中,Controller方法的多种实现方式(指定返回到哪个页面,指定返回到页面的数据)
- springmvc 返回xml数据
- js获取springmvc的controller返回的数据,可以是对象,也可以处理成字符串返回给js
- 通过gradle生成SpringMVC实现RESTful返回JSON格式数据
- SpringBoot RestController 同时支持返回xml和json格式数据
- 基于xml的springMVC请求与数据返回
- SpringMVC返回客户端Json/XML数据
- spring mvc 在同一个controller 中同时返回多种格式的数据 (xml json atom)
- springmvc 用fasterxml.jackson返回son数据
- Asp.net mvc返回Xml结果,扩展Controller实现XmlResult以返回XML格式数据
- SpringMVC返回XML或者JSON格式的数据
- SpringMVC返回XML或者JSON格式的数据