admin管理员组文章数量:1410724
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 there any way to return plain text ("application/json") using an request from something like a markup-less facelet?
The only solution I came up with was using an HttpServlet and registering it to a service URL in web.xml. That approach works and really returns an file without any markup, but I'd rather use my Spring-injected ManagedProperties than being restricted to WebApplicationContextUtils.
Did I miss something or is that the remended way?
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 there any way to return plain text ("application/json") using an request from something like a markup-less facelet?
The only solution I came up with was using an HttpServlet and registering it to a service URL in web.xml. That approach works and really returns an file without any markup, but I'd rather use my Spring-injected ManagedProperties than being restricted to WebApplicationContextUtils.
Did I miss something or is that the remended way?
Share Improve this question edited Dec 2, 2011 at 15:03 BalusC 1.1m376 gold badges3.7k silver badges3.6k bronze badges asked Dec 2, 2011 at 14:45 AlexAlex 4721 gold badge8 silver badges17 bronze badges2 Answers
Reset to default 5There 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:position
xmlns:f="http://java.sun./jsf/core"
xmlns:ui="http://java.sun./jsf/facelets">
<f:event type="preRenderView" listener="#{bean.renderJson}" />
</ui:position>
with
public void renderJson() throws IOException {
FacesContext facesContext = FacesContext.getCurrentInstance();
ExternalContext externalContext = 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:
- Servlet vs RESTful
- How to use Servlets and Ajax?
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./jsf/core" >
{ test : 'value' ,
some : #{someBean.someValue} }
</f:view>
本文标签: javascriptJSFJSON Output quotplainquot text in servletStack Overflow
版权声明:本文标题:javascript - JSF + JSON: Output "plain" text in servlet? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744788460a2625143.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论