Creating XML with JSF

I need to send XML to a browser using my JSF application. This XML is generated by the application. I try to create it, but the JSF application sends HTML every time.

How to change the content type for xml submission?

Thank you for your help.

+5
source share
5 answers

There are several ways to do this. Doing this in JSP is a bit annoying.

As already mentioned, you can use Servlet and insert / load variables there. For example, by accessing the session context:

MyBean myBean = (MyBean)FacesContext.getCurrentInstance()
                         .getExternalContext().getSessionMap().get("myBean");

Or you can output it in HTTP Response from a method in your Bean application. For example:

try {
    String xml = "<person>damian</person>";
    FacesContext ctx = FacesContext.getCurrentInstance();
    final HttpServletResponse resp = (HttpServletResponse)ctx.getExternalContext().getResponse();

    resp.setContentType("text/xml");
    resp.setContentLength(xml.length());
    resp.getOutputStream().write(xml.getBytes());
    resp.getOutputStream().flush();
    resp.getOutputStream().close();

    ctx.responseComplete();

} catch (IOException e) {
    e.printStackTrace();
}

, Facelets, <f:view>.

+7

JSP. , JSP xml- bean? JSP, , XML:

<%@page contentType="text/xml"%><?xml version="1.0" encoding="UTF-8"?>
<portfolio>
  <stock>
    <symbol>SUNW</symbol>
    <name>Sun Microsystems</name>
    <price>17.1</price>
  </stock>
  <stock>
    <symbol>AOL</symbol>
    <name>America Online</name>
    <price>51.05</price>
  </stock>
  <stock>
    <symbol>IBM</symbol>
    <name>International Business
    Machines</name>
    <price>116.10</price>
  </stock>
  <stock>
    <symbol>MOT</symbol>
    <name>MOTOROLA</name>
    <price>15.20</price>
  </stock>
</portfolio>

bean bean , JSP HTML.

+3

, XML, JSF. JSF ( JSP ) "" . XML .

RE: - , JSF, , google : http://blogs.oracle.com/chrisf/entry/retrieving_jsf_session_variables_in

0

content-type ( 14.17) text/xml. text/html.

, , HTTP Content-type.

, JSF, , , -, !

0

, facelet:

<ui:composition xmlns="http://www.w3.org/1999/xhtml"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:ui="http://java.sun.com/jsf/facelets">

    <ui:define name="metadata">
        <f:metadata>
            <f:event type="preRenderView" 
                listener="#{clientManager.initialize}" />
        </f:metadata>
    </ui:define>

    <f:view contentType="text/xml">
        <ui:repeat var="client" value="#{clientManager.list}">
            <client>#{client.name}</client>
        </ui:repeat>
    </f:view>
</ui:composition>

, HTML, JSF xml , .

0

All Articles