1

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」に設定されていることがわかりますが、ページにはまだ何も表示されません。

4

3 に答える 3

0

RFC 3023から

If an XML document -- that is, the unprocessed, source XML document -- is readable by
casual users, text/xml is preferable to application/xml. MIME user agents (and web user
agents) that do not have explicit support for text/xml will treat it as text/plain, for 
example, by displaying the XML MIME entity as plain text. Application/xml is preferable when the XML MIME entity is unreadable by casual users.

あなたのコードでは問題にならないかもしれませんが、browserXML をまったくレンダリングしないものもあります。の代わりにtext/xml、使用する必要がありますapplication/xml

XML ドキュメントをブラウザーにレンダリングしようとしている理由。それでもレンダリングしたい場合は、を使用XSLTして を に変換XMLHTMLます。

于 2013-09-13T11:40:32.833 に答える
0

次の 2 つの項目を試してください。

  1. コンテンツ タイプを「text/xml」に設定します。コントローラーのリクエスト ハンドラー メソッドで実行できます -->response.setContentType("text/xml");または、xml コンテンツが別の JSP ファイルにある場合は、<%@ page contentType="text/xml" %>ディレクティブを先頭に設定します。
  2. applicationContext.xml で、次のプロパティが viewResolver Bean 構成に追加されていることを確認します。 <property name="alwaysInclude" value="true" />.
于 2016-09-01T19:02:13.383 に答える
0

XML Marshaller http://docs.spring.io/spring-ws/site/reference/html/oxm.html を使用するか、手動で次のような一般的なライブラリを使用できます。

    @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;
}
于 2013-09-13T12:06:52.480 に答える