本文介绍了在应用程序中为单个Tapestry 4页面设置ISO-8859-1编码,否则完全为UTF-8的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Tapestry应用程序以UTF-8的形式提供其页面。也就是说,服务器响应有标题:

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

现在在这个应用程序中,有一个页面应该使用ISO-8859-1编码。也就是说,服务器响应应该有这个标题:

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";
    }
 }

但是,不能使用@Meta注释或覆盖设置这些值getOutputEncoding方法工作。

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

我正在使用Tapestry 4.0.2。

I am using Tapestry 4.0.2.

编辑:我结束了一个带有子类HttpServletResposeWrapper的Servlet过滤器。包装器覆盖setContentType()以强制响应的所需编码。

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

推荐答案

您是否考虑过过滤器?可能不像Tapestry中的东西那么优雅,但是使用一个简单的Filter来注册感兴趣的url映射。其中一个init参数将是您之后的编码。示例:

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() {
}

}

这篇关于在应用程序中为单个Tapestry 4页面设置ISO-8859-1编码,否则完全为UTF-8的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-01 18:13