Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/390.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 用SpringMVC显示XML字符串_Java_Xml_Spring_Spring Mvc_Web - Fatal编程技术网

Java 用SpringMVC显示XML字符串

Java 用SpringMVC显示XML字符串,java,xml,spring,spring-mvc,web,Java,Xml,Spring,Spring Mvc,Web,我试图将XML文档呈现给浏览器,但得到的是一个空白屏幕。但是,当我查看页面的源代码时,我可以看到XML @RequestMapping(value = "view-xml", method = {RequestMethod.GET}) public ResponseEntity<String> viewXmlPayload(@RequestParam ("id") int taskId){ payload = dao.getXmlPayload(taskId);

我试图将XML文档呈现给浏览器,但得到的是一个空白屏幕。但是,当我查看页面的源代码时,我可以看到XML

@RequestMapping(value = "view-xml", method = {RequestMethod.GET})
public ResponseEntity<String> viewXmlPayload(@RequestParam ("id") int taskId){

    payload = dao.getXmlPayload(taskId);

    HttpHeaders responseHeaders = new HttpHeaders();
    responseHeaders.setContentType(MediaType.TEXT_XML);
    return new ResponseEntity<String>(payload, responseHeaders, HttpStatus.OK);
}
使用一些浏览器工具,我可以看到内容类型被正确设置为text/xml,但我仍然没有在页面上看到任何内容。

From

您的代码可能没有问题,某些浏览器根本不呈现XML。您需要使用application/xml,而不是text/xml


为什么要尝试向浏览器呈现XML文档,如果仍然要呈现,请使用XSLT将XML转换为HTML。

您可以使用XML封送器 或者手动使用公共LIB,如下所示:

    @RequestMapping("/downloadXML.do")
public ModelAndView downloadXML(HttpServletResponse  response,@RequestParam("xmlName") String xmlName ) {   

String doc=serviceXml.getXml(xmlName);      

    InputStream in = null;
    try {
        response.setHeader("Content-Disposition", "filename=\"" +doc.getFilename()+ "\"");
        OutputStream out = response.getOutputStream();
        response.setContentType(doc.getContentType());
        in = doc.getInputStream();
        IOUtils.copy(in, out);
        out.flush();
        out.close();


    } catch (IOException e) {
        e.printStackTrace();
    }
    finally 
    {
        if (in != null) {
            try {
                in.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
return null;
}

请尝试以下两项:

将内容类型设置为text/xml。您可以在控制器请求处理程序方法->response.setContentTypetext/xml;或者,如果xml内容位于单独的JSP文件中,请在顶部设置该指令。 确保在applicationContext.xml中,在viewResolver bean配置中添加了以下属性。 .
你的意思是,即使在查看源代码时,响应也是空白的?@Manish页面上什么也不显示,但当我查看源代码时,我可以看到XML。
    @RequestMapping("/downloadXML.do")
public ModelAndView downloadXML(HttpServletResponse  response,@RequestParam("xmlName") String xmlName ) {   

String doc=serviceXml.getXml(xmlName);      

    InputStream in = null;
    try {
        response.setHeader("Content-Disposition", "filename=\"" +doc.getFilename()+ "\"");
        OutputStream out = response.getOutputStream();
        response.setContentType(doc.getContentType());
        in = doc.getInputStream();
        IOUtils.copy(in, out);
        out.flush();
        out.close();


    } catch (IOException e) {
        e.printStackTrace();
    }
    finally 
    {
        if (in != null) {
            try {
                in.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
return null;
}