1.搭建SpringMVC+spring环境
2.web.xml,Springmvc-config.xml。springMVC提供了处理xml格式请求响应的HttpMessageConverter,springMVC默认使用Jaxb2RootElementHttpMessageConverter,通过
JAXB2读写XML消息,并将请求信息转换到注解XMLRootElement和XmlType作用的类中。
<?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: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-4.2.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd"> <!-- spring可以自动去扫描base-pack下面的包或者子包下面的java文件, 如果扫描到有Spring的相关注解的类,则把这些类注册为Spring的bean --> <context:component-scan base-package="com.moon.controller"/> <!-- 设置配置方案 --> <mvc:annotation-driven/> <!-- 使用默认的Servlet来响应静态文件 --> <mvc:default-servlet-handler/> <!-- 视图解析器 --> <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <!-- 前缀 --> <property name="prefix"> <value>/WEB-INF/content/</value> </property> <!-- 后缀 --> <property name="suffix"> <value>.jsp</value> </property> </bean> </beans>
3.实体类
package com.moon.domain; import javax.xml.bind.annotation.XmlRootElement; import com.sun.xml.internal.txw2.annotation.XmlElement; @XmlRootElement public class Book { private int id; private String author; private String name; public int getId() { return id; } @XmlElement public void setId(int id) { this.id = id; } public String getAuthor() { return author; } @XmlElement public void setAuthor(String author) { this.author = author; } public String getName() { return name; } @XmlElement public void setName(String name) { this.name = name; } }
4.controller层
@Controller public class BookController {//@RequestBody Book book会将传递的xml数据自动绑定到Book对象 @RequestMapping(value="/sendxml",method=RequestMethod.POST) public void sendXml(@RequestBody Book book){ System.out.println(book.getId()); System.out.println(book.getAuthor()); } }
5.view层
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>Insert title here</title> <script type="text/javascript" src="js/jquery-1.11.0.min.js"></script> <script type="text/javascript" src="js/json2.js"></script> <script type="text/javascript"> $(document).ready(function(){ sendXml(); }); function sendXml(){ var xmlData = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><book><id>1</id><name>crazy</name><author>menbbo</author> </book>"; $.ajax( "${pageContext.request.contextPath}/sendxml", { type:"POST", contentType:"application/xml", data:xmlData, async:true } ); } </script> </head> <body> </body> </html>
原文地址:https://www.cnblogs.com/menbo/p/10311791.html
时间: 2024-10-28 15:04:29