Jsf + Json: Output "plain" Text In Servlet?
I'm trying to use Mootools (Request.JSON) together with JSF - mainly because I wrote a similar application in CakePHP some time ago and would like to reuse most of the JS part. Is
Solution 1:
There is a way. But it's ugly and essentially abuse of JSF/Facelets as in using the wrong tool for the job.
E.g.
<ui:compositionxmlns:f="http://java.sun.com/jsf/core"xmlns:ui="http://java.sun.com/jsf/facelets"><f:eventtype="preRenderView"listener="#{bean.renderJson}" /></ui:composition>
with
publicvoidrenderJson()throws IOException {
FacesContextfacesContext= FacesContext.getCurrentInstance();
ExternalContextexternalContext= facesContext.getExternalContext();
externalContext.setResponseContentType("application/json");
externalContext.setResponseCharacterEncoding("UTF-8");
externalContext.getResponseOutputWriter().write(someJsonString);
facesContext.responseComplete();
}
Much better is to use a JAX-RS web service. I'm not sure if Spring managed beans are injectable in there, but the new Java EE 6 CDI allows you to inject @Named
beans everywhere by @Inject
, even in a simple @WebServlet
.
See also:
Solution 2:
If you want to use facelets you can do it like this. (I don't know if spring injected beans work, but if you add @named or @managedBean then it should be accessible in the facelet)
<f:view contentType="application/json" xmlns:f="http://java.sun.com/jsf/core" >
{ test : 'value' ,
some : #{someBean.someValue} }
</f:view>
Post a Comment for "Jsf + Json: Output "plain" Text In Servlet?"