Jsf pages to plain/text without header html

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-13 04:39:31

问题


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

EDIT: 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


回答1:


I know that I can use a servlet for this but wanted to know if it was possible to use a Bean/JSF instead.

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>

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.
}

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.




回答2:


You can use Java Servlet to output response in plain text.

Example :

protected void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    response.setHeader("Content-Type", "text/plain");
    response.setHeader("success", "yes");
    PrintWriter writer = response.getWriter();
    writer.write("This is plain response\n");
    writer.close();
}



回答3:


JSF only renders HTML if you use components that generate such content. You can produce text/html content like this:

<f:view xmlns:ui="http://xmlns.jcp.org/jsf/facelets"
    xmlns:f="http://xmlns.jcp.org/jsf/core"
    contentType="text/plain"
    encoding="UTF-8">
    <ui:composition>
        Your plain text goes here.
        You can use expressions as usual: #{myBean.value}.
    </ui:composition>
</f:view>

Just plain text will be rendered. With the f:view component attributes you set the response headers.



来源:https://stackoverflow.com/questions/17442888/jsf-pages-to-plain-text-without-header-html

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!