Mat*_* B. 3 java spring utf-8 character-encoding
{|}~€‚ƒ„…†‡ˆ‰Š‹ŒŽ‘’“”•–—˜™š›œžŸ¡¢£¤¥¦§¨©ª«¬®¯°±²³´µ¶·¸¹º»¼½¾¿ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎ ±ÐÑÒÓÔÕÖרÙÚÛÜÝÞßàáâãäåæçèéêëìíîïðñòóôõö±øùúûüýþŸ?????????????????????€¢£¥¤????????""????????????????†‡??????????×÷+-??¼½¾??????%‰¹²³
{|}~???????????????????????????¡¢£¤¥¦§¨©ª«¬®¯°±²³´µ¶·¸¹º»¼½¾¿ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎ ±ÐÑÒÓÔÕÖרÙÚÛÜÝÞßàáâãäåæçèéêëìíîïðñòóôõö±øùúûüýþ?????¦???????????????¤?¢£¥¤????????""????¦???????????????????????×÷+-Ov¼½¾??????%?¹²³
<#ftl attributes={"content_type":"text/html"} encoding="UTF-8"/>
Run Code Online (Sandbox Code Playgroud)
把它放到我的HttpsCoookieFilter中:
request.setCharacterEncoding("UTF-8");
response.setCharacterEncoding("UTF-8");
response.setContentType("text/html; charset=UTF-8");
Run Code Online (Sandbox Code Playgroud)
很明显,我(ab)使用HttpServlet而不是Freemarker来使用out.write()生成HTML内容,所以我添加了上面的内容.
这是servlet源码.有关如何更改它的任何提示都非常受欢迎:
public class HttpsCookieFilter implements Filter {
private static Logger log = Logger.getLogger(HttpsCookieFilter.class);
@Override
public void destroy() {
}
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
final HttpServletRequest req = (HttpServletRequest) request;
final HttpServletResponse res = (HttpServletResponse) response;
req.setCharacterEncoding("UTF-8");
res.setCharacterEncoding("UTF-8");
res.setContentType("text/html; charset=UTF-8");
final HttpSession session = req.getSession(false);
if (session != null) {
setCookie(req, res);
}
try {
chain.doFilter(req, res);
} catch (IllegalStateException e) {
log.warn("HttpsCookieFilter redirect problem! ", e);
}
}
@Override
public void init(FilterConfig arg0) throws ServletException {
}
private void setCookie(HttpServletRequest request, HttpServletResponse response) {
Cookie cookie = new Cookie("JSESSIONID", request.getSession(false).getId());
cookie.setMaxAge(-1);
cookie.setPath(getCookiePath(request));
cookie.setSecure(false);
response.addCookie(cookie);
}
private String getCookiePath(HttpServletRequest request) {
String contextPath = request.getContextPath();
return contextPath.length() > 0 ? contextPath : "/";
}
}
Run Code Online (Sandbox Code Playgroud)
现在UTF-8到处工作;)谢谢你BalusC !!!
问号通常用于当字符到字节转换器/写入器本身知道字符的字符集的其实在编码和字符集字符必须被解码的.如果解码字符集不支持原始编码中的特定字符,则将其转换为问号.
在具有数据库后端的普通Web应用程序中,有两个地方可能发生这种情况:
在这两种情况下,都使用TCP/IP网络,它只能理解字节,服务器和客户端通常都知道双方使用的字符集.在所有其他情况下,你会看到Mojibake.
要覆盖第一种情况,您需要确保已将DB和表配置为使用UTF-8.您通常在指定期间指定CREATE.这是MySQL方言中的一个例子.
CREATE DATABASE db_name CHARACTER SET utf8;
CREATE TABLE tbl_name (...) CHARACTER SET utf8;
Run Code Online (Sandbox Code Playgroud)
对于某些JDBC驱动程序,例如MySQL驱动程序,您还需要指示驱动程序本身使用UTF-8.
jdbc:mysql://localhost:3306/db_name?useUnicode=yes&characterEncoding=UTF-8
Run Code Online (Sandbox Code Playgroud)
要覆盖第二种情况,您需要确保已指示响应写入程序使用UTF-8将字符解码为字节.当使用JSP作为视图时,只需将以下内容添加到每个 JSP页面(也包括include)的顶部应该足够(它不仅设置响应编码,而且还隐式设置正确的响应头).
<%@ page pageEncoding="UTF-8" %>
Run Code Online (Sandbox Code Playgroud)
对于您当前使用的Spring字符编码过滤器,它仅设置请求编码,以便您可以确保将提交的数据解释为UTF-8.它基本上完成的是以下内容:
request.setCharacterEncoding("UTF-8");
Run Code Online (Sandbox Code Playgroud)
仅此而已.请注意,这仅涵盖POST请求,对于GET请求,您仍需要配置Web服务器以将URL解释为UTF-8.
| 归档时间: |
|
| 查看次数: |
8273 次 |
| 最近记录: |