zoukankan      html  css  js  c++  java
  • SpringMVC处理XML格式的数据

    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.book.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <book>
        <id>1</id>
        <name>balckmaba</name>
        <author>menbbo</author>
    </book>

    5.controller层@Controllerpublic 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());
        }
       

    @RequestMapping(value="/readxml") //@ResponseBody会自动将Book对象转换成xml数据返回。
    public @ResponseBody Book readXml() throws Exception{
    //通过JAXBContext的newinstance方法,传入一个class就可以获得一个上下文
    JAXBContext context = JAXBContext.newInstance(Book.class);
    //创建一个unmarshaller对象
    Unmarshaller unmarshaller = context.createUnmarshaller();
    InputStream in = this.getClass().getResourceAsStream("/Book.xml");
    //Unmarshaller对象的unmarshal方法可以将xml转换成java对象
    Book book = (Book) unmarshaller.unmarshal(in);
    return book;
    }

     

      

    
    }

    6.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();
          readXml(); });
    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 } ); }

    function readXml(){
    $.ajax("${pageContext.request.contextPath}/readxml",
    {
    //dataType:"text",

    type:"POST",
    contentType:"application/xml",
    success:function(data){
    var id = $("id",data).text();
    var name = $("name",data).text();
    var author = $("author",data).text();
    $("#id").html(id);
    $("#name").html(name);
    $("#author").html(author);
    },
    error:function(XMLHttpRequest, textStatus, errorThrown){
    alert(XMLHttpRequest.status);
    alert(XMLHttpRequest.readyState);
    alert(textStatus);
    }
    }
    );
    }

    </script>
    </head>
    <body>
    
    </body>
    </html>
  • 相关阅读:
    hive报错 java.sql.SQLException: No suitable driver found for jdbc:hive://localhost:10000/default
    使用Beeline连接Hive
    hive报错 root is not allowed to impersonate root (state=08S01,code=0)
    hive报错 Could not open client transport with JDBC Uri: jdbc:hive2://node01:10000/default:java.net.ConnectException refused
    excel快速删除空值单元格,数据上移
    FineBI 图表选择
    数据库连接池大小设置?
    工作中有待留❤️积累的一些经验
    内存包括随机存储器(RAM),只读存储器(ROM),以及高速缓存(CACHE)。RAM最重要
    我自己ood的复习思路:抄
  • 原文地址:https://www.cnblogs.com/menbo/p/10311791.html
Copyright © 2011-2022 走看看