在应用程序中为单个Tapestry 4页面设置ISO-8859-1编码,否则完全为UTF-8
-
03-07-2019 - |
题
我有一个Tapestry应用程序,它的页面为UTF-8。也就是说,服务器响应有标题:
Content-type: text/html;charset=UTF-8
现在,在此应用程序中,有一个页面应该使用ISO-8859-1编码。也就是说,服务器响应应具有此标头:
Content-type: text/html;charset=ISO-8859-1
怎么做?我不想更改整个应用程序的默认编码。
根据Google搜索,我试过以下内容:
@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方法设置这些值都不起作用。
我正在使用Tapestry 4.0.2。
编辑:我最后使用带有子类HttpServletResposeWrapper的Servlet过滤器来完成此操作。包装器覆盖setContentType()以强制响应所需的编码。解决方案
您考虑过滤器吗?也许不像Tapestry中的内容那么优雅,但使用普通的Filter,它会记录感兴趣的url映射。它的一个init参数就是你的后续编码。例如:
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() {
}
}
其他提示
你可以做到:
@Override
public ContentType getResponseContentType() {
return new ContentType("text/html;charset=" + someCharEncoding);
}
过滤器建议很好。您还可以将Servlet与Tapestry混合使用。例如,我们有servlet用于显示XML文档和动态生成的Excel文件。只需确保在web.xml中正确设置映射,以便servlet不通过Tapestry。
Tapestry具有可应用于请求/响应管道的过滤器概念,但其优点是可以访问T5 IoC Container&服务。
http://tapestry.apache.org/tapestry5/tapestry -core /导向/ request.html
不隶属于 StackOverflow