Setting ISO-8859-1 encoding for a single Tapestry 4 page in application that is otherwise totally UTF-8
Asked Answered
E

4

4

I have a Tapestry application that is serving its page as UTF-8. That is, server responses have header:

Content-type: text/html;charset=UTF-8

Now within this application there is a single page that should be served with ISO-8859-1 encoding. That is, server response should have this header:

Content-type: text/html;charset=ISO-8859-1

How to do this? I don't want to change default encoding for whole application.

Based on google searching I have tried following:

 @Meta({    "org.apache.tapestry.output-encoding=ISO-8859-1", 
    "org.apache.tapestry.response-encoding=ISO-8859-1", 
    "org.apache.tapestry.template-encoding=ISO-8859-1",
    "tapestry.response-encoding=ISO-8859-1"})
 abstract class MyPage extends BasePage {

    @Override
    protected String getOutputEncoding() {
        return "ISO-8859-1";
    }
 }

But neither setting those values with @Meta annotation or overriding getOutputEncoding method works.

I am using Tapestry 4.0.2.

EDIT: I ended up doing this with a Servlet filter with subclassed HttpServletResposeWrapper. The wrapper overrides setContentType() to force required encoding for the response.

Ealing answered 30/9, 2008 at 15:10 Comment(0)
T
3

Have you considered a Filter? Maybe not as elegant as something within Tapestry, but using a plain Filter, that registers the url mapping(s) of interest. One of its init parameters would be the encoding your after. Example:

public class EncodingFilter implements Filter {
private String encoding;
private FilterConfig filterConfig;

/**
* @see javax.servlet.Filter#init(javax.servlet.FilterConfig)
*/
public void init(FilterConfig fc) throws ServletException {
this.filterConfig = fc;
this.encoding = filterConfig.getInitParameter("encoding");
}

/**
* @see javax.servlet.Filter#doFilter(javax.servlet.ServletRequest, javax.servlet.ServletResponse, javax.servlet.FilterChain)
*/
public void doFilter(ServletRequest req, ServletResponse resp,
FilterChain chain) throws IOException, ServletException {
req.setCharacterEncoding(encoding);
chain.doFilter(req, resp);
}

/**
* @see javax.servlet.Filter#destroy()
*/
public void destroy() {
}

}
Tricolor answered 2/10, 2008 at 2:59 Comment(2)
Hmm. This sets encoding for incoming request. I would need to change encoding for outgoing response. My Tapestry application is not the one that handles form submit. My application just serves the form page.Blatt
The response encoding can be set as of 2.4 specification. Are you in a J2EE 1.4 server? java.sun.com/j2ee/1.4/docs/api/javax/servlet/…Tricolor
S
2

You could have done:

    @Override
public ContentType getResponseContentType() {
        return new ContentType("text/html;charset=" + someCharEncoding);
}
Shame answered 27/10, 2010 at 21:53 Comment(0)
U
1

The filter suggestion is good. You can also mix servlets with Tapestry. For instance, we have servlets for serving displaying XML documents and dynamically generated Excel files. Just make sure that correctly set the mappings in web.xml so that that the servlets do not go through Tapestry.

Unifoliolate answered 2/10, 2008 at 15:11 Comment(0)
M
1

Tapestry has the concept of filters that can be applied to the request/response pipeline, but with the advantage that you can access the T5 IoC Container & Services.

http://tapestry.apache.org/tapestry5/tapestry-core/guide/request.html

Marotta answered 31/1, 2009 at 13:22 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.