FileDocCategorySizeDatePackage
PlainTextResult.javaAPI DocExample5463Mon Jul 23 13:26:38 BST 2007org.apache.struts2.dispatcher

PlainTextResult

public class PlainTextResult extends StrutsResultSupport
A result that send the content out as plain text. Usefull typically when needed to display the raw content of a JSP or Html file for example.
  • location (default) = location of the file (jsp/html) to be displayed as plain text.
  • charSet (optional) = character set to be used. This character set will be used to set the response type (eg. Content-Type=text/plain; charset=UTF-8) and when reading using a Reader. Some example of charSet would be UTF-8, ISO-8859-1 etc.


<action name="displayJspRawContent" >
<result type="plaintext">/myJspFile.jsp</result>
</action>


<action name="displayJspRawContent" >
<result type="plaintext">
<param name="location">/myJspFile.jsp</param>
<param name="charSet">UTF-8</param>
</result>
</action>


Fields Summary
public static final int
BUFFER_SIZE
private static final Log
_log
private static final long
serialVersionUID
private String
charSet
Constructors Summary
public PlainTextResult()


      
        super();
    
public PlainTextResult(String location)

        super(location);
    
Methods Summary
protected voiddoExecute(java.lang.String finalLocation, com.opensymphony.xwork2.ActionInvocation invocation)


        // verify charset
        Charset charset = null;
        if (charSet != null) {
            if (Charset.isSupported(charSet)) {
                charset = Charset.forName(charSet);
            }
            else {
                _log.warn("charset ["+charSet+"] is not recognized ");
                charset = null;
            }
        }

        HttpServletResponse response = (HttpServletResponse) invocation.getInvocationContext().get(HTTP_RESPONSE);
        ServletContext servletContext = (ServletContext) invocation.getInvocationContext().get(SERVLET_CONTEXT);


        if (charset != null) {
            response.setContentType("text/plain; charset="+charSet);
        }
        else {
            response.setContentType("text/plain");
        }
        response.setHeader("Content-Disposition", "inline");


        PrintWriter writer = response.getWriter();
        InputStreamReader reader = null;
        try {
            if (charset != null) {
                reader = new InputStreamReader(servletContext.getResourceAsStream(finalLocation), charset);
            }
            else {
                reader = new InputStreamReader(servletContext.getResourceAsStream(finalLocation));
            }
            if (reader == null) {
                _log.warn("resource at location ["+finalLocation+"] cannot be obtained (return null) from ServletContext !!! ");
            }
            else {
                char[] buffer = new char[BUFFER_SIZE];
                int charRead = 0;
                while((charRead = reader.read(buffer)) != -1) {
                    writer.write(buffer, 0, charRead);
                }
            }
        }
        finally {
            if (reader != null)
                reader.close();
            if (writer != null) {
                writer.flush();
                writer.close();
            }
        }
    
public java.lang.StringgetCharSet()
Set the character set

return
The character set

        return charSet;
    
public voidsetCharSet(java.lang.String charSet)
Set the character set

param
charSet The character set

        this.charSet = charSet;