Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
198 views
in Technique[技术] by (71.8m points)

java - Create XML with JSF

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

How can I change the content-type to send xml ?

Thanks for your help.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

As already mentioned you can use a Servlet and inject/load your variables in there. Eg by accessing the session context:

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

Or you can output it to the HTTP Response from a method in your Backing Bean. Eg:

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

Or if you are using Facelets you can set the response type in the <f:view> tag.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...