使用JSF创建XML [英] Create XML with JSF
问题描述
我需要使用我的JSF应用程序将XML发送到浏览器。此XML由应用程序生成。
我尝试创建它但我的JSF应用程序每次都发送HTML。
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.
如何更改内容类型以发送xml?
How can I change the content-type to send xml ?
感谢您的帮助。
推荐答案
有几种方法可以做到这一点。在JSP中执行它有点讨厌。
There are several ways to do this. Doing it in JSP is a bit nasty.
如前所述,您可以使用 Servlet 并在其中注入/加载变量。例如,通过访问会话上下文:
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");
或者您可以从您的方法中将其输出到 HTTP响应支持Bean。例如:
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();
}
或者如果您使用 Facelets ,您可以设置< f:view>
代码中的回复类型。
Or if you are using Facelets you can set the response type in the <f:view>
tag.
这篇关于使用JSF创建XML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!