jsf页面转换为纯文本/无标题html的文本 [英] Jsf pages to plain/text without header html

查看:65
本文介绍了jsf页面转换为纯文本/无标题html的文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在一个jsf页面中显示输出,该页面的格式不是html(没有标题,没有html标签),而是一个简单的文本文件.使用JSF 2.0可以做到这一点,或者我是否需要一个servlet?谢谢

I need to show output in a jsf page that is not formatted as html (without header and without html tags), but as a simple text file. This is possible with JSF 2.0 or I necessarily need a servlet? Thanks

客户端通过url(带有参数)发出请求,而我必须给它一个响应.我知道我可以为此使用servlet,但想知道是否可以使用Bean/JSF.问题是我必须给出的响应不能是html文件,而应该是文本文件(用于简单解析),但是不应下载而是直接在浏览器中显示.我希望我很清楚

a client makes a request through url (with parameters) and I have to give it a response. I know that I can use a servlet for this but wanted to know if it was possible to use a Bean/JSF instead. the problem is that I have to give response that can not be an html file but a text file (for simple parsing), but that should not be downloaded but displayed directly in the browser. I hope I was clear

推荐答案

我知道我可以为此使用servlet,但想知道是否可以使用Bean/JSF.

是的,JSF也很有可能.整个Facelet页面如下所示:

Yes, it's quite possible with JSF as well. The entire Facelet page can look like this:

<ui:composition
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:ui="http://java.sun.com/jsf/facelets">
    <f:event type="preRenderView" listener="#{bean.renderText}" />
</ui:composition>

bean的相关方法如下所示:

And the relevant method of the bean can look like this:

public void rendertext() throws IOException {
    FacesContext fc = FacesContext.getCurrentInstance();
    ExternalContext ec = fc.getExternalContext();
    Map<String, String> params = ec.getRequestParameterMap();
    String foo = params.get("foo"); // Returns request parameter with name "foo".
    // ...

    ec.setResponseContentType("text/plain");
    ec.setResponseCharacterEncoding("UTF-8");
    ec.getResponseOutputWriter().write("Some text content");
    // ...

    fc.responseComplete(); // Important! Prevents JSF from proceeding to render HTML.
}

但是,实际上,您实际上滥用 JSF是用于此目的的错误工具.在这种特定情况下,JSF会增加过多的开销,您根本不需要这些开销.这样,servlet就更好了.您可以使用@WebServlet批注进行注册,而无需进行XML配置.您也不再需要Facelet文件.

However, you're then essentially abusing JSF as wrong tool for the purpose. JSF adds too much overhead in this specific case which you don't need at all. A servlet is then much better. You can use the @WebServlet annotation to register it without any need for XML configuration. You also don't need a Facelet file anymore.

这篇关于jsf页面转换为纯文本/无标题html的文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆