それ以外の場合は完全にUTF-8であるアプリケーションの単一のTapestry 4ページのISO-8859-1エンコードの設定
-
03-07-2019 - |
質問
ページをUTF-8で提供しているTapestryアプリケーションがあります。つまり、サーバー応答にはヘッダーがあります:
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を使用して、サーブレットフィルターでこれを実行しました。ラッパーはsetContentType()をオーバーライドして、応答に必要なエンコードを強制します。
解決
フィルターを検討しましたか?たぶんTapestry内の何かほどエレガントではないかもしれませんが、興味のあるURLマッピングを登録する単純なフィルターを使用しています。その初期化パラメータの1つは、エンコーディングの後になります。例:
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);
}
フィルターの提案は良好です。サーブレットとTapestryを混在させることもできます。たとえば、XMLドキュメントと動的に生成されたExcelファイルを表示するためのサーブレットがあります。サーブレットがTapestryを通過しないように、web.xmlでマッピングが正しく設定されていることを確認してください。
Tapestryには、要求/応答パイプラインに適用できるフィルターの概念がありますが、T5 IoCコンテナー&にアクセスできるという利点があります。サービス。
http://tapestry.apache.org/tapestry5/tapestry -core / guide / request.html