字符编码后JSF中的乱码字符 [英] Character encoding garbled characters in post JSF
问题描述
我在JSF中发布了以下文档。
< h:panelGrid columns =2>
< h:outputText value =Klubbnamn>< / h:outputText>
< h:inputText name =clubnamevalue =#{club.name}>< / h:inputText>
< / h:panelGrid>
< h:commandButton value =Sparaaction =#{serviceHCP.saveClub(club)}>< / h:commandButton>
写作åäö作为发贴的价值将使我重新受到欢迎。在xhtml文件中编写的所有内容看起来应该是只有发布的值才会出现乱码。如果我在java类中硬编码它,它将被正确地保存在数据库中。所以我肯定这个帖子有问题。
我试图将网页切换到is0-8859-1而没有结果。 Tomcat报告默认为utf-8。
<?xml version =1.0encoding =utf-8?>
<!DOCTYPE html>
< html xmlns =http://www.w3.org/1999/xhtml>< html xmlns =http://www.w3.org/1999/xhtml>< head>< link type =text / css =stylesheethref =/ BowlingFacelets / faces / javax.faces.resource / theme.css?ln = primefaces-aristo/>
< meta http-equiv =Content-Typecontent =text / html; charset = utf-8/>
< title>预设标题< / title>
< link rel =stylesheettype =text / csshref =hcp-style.css/>< / head>
< body>
.....
< td> ID< / td>
< td> klubb< / td>
< / tr>
< tr>
< td> 1< / td>
< td>åäö< / td>
....
就像请求编码的问题一样。您应该使用 FireBug 或诸如 Wireshark 。
在请求标题中查找 Content-类型
。它必须包含: charset = utf-8
如果不是这种情况, JSF应该处理请求字符编码。
尽管您可以尝试在自制过滤器中设置字符编码。它可能是这样的:
@WebFilter(/ *)
public class CharEncodingFilter implements Filter {
@Override
public void doFilter(ServletRequest request,ServletResponse response,
FilterChain chain)throws ServletException,IOException {
request.setCharacterEncoding(UTF-8);
chain.doFilter(request,response);
}
}
另请参阅:
类似问题:
I have the following document being posted in JSF.
<h:panelGrid columns="2">
<h:outputText value="Klubbnamn"></h:outputText>
<h:inputText name="clubname" value="#{club.name}"></h:inputText>
</h:panelGrid>
<h:commandButton value="Spara" action="#{serviceHCP.saveClub(club) }"></h:commandButton>
writing åäö as the value for posting will render me åäö back. Everything written in the xhtml file will look like it should it's only the posted value that will be garbled. If I hard code it in the java class it will be correctly saved in the database. So I am sure there is something wrong with the post.
I have tried switching the webpage to is0-8859-1 without result. Tomcat is reporting default as utf-8.
<?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"><html xmlns="http://www.w3.org/1999/xhtml"><head><link type="text/css" rel="stylesheet" href="/BowlingFacelets/faces/javax.faces.resource/theme.css?ln=primefaces-aristo" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Default title</title>
<link rel="stylesheet" type="text/css" href="hcp-style.css" /></head>
<body>
.....
<td>ID</td>
<td>klubb</td>
</tr>
<tr>
<td>1</td>
<td>åäö</td>
....
This definitely looks like a problem with the request encoding. You should check the request character encoding header of the HTTP POST with FireBug or a tool like Wireshark.
In the request headers look for the Content-Type
. It must contain: charset=utf-8
If this is not the case something in your setup is wrong because JSF should take care of the request character encoding.
Despite you can try to set the character encoding in a self-made filter. It could be something like this:
@WebFilter("/*")
public class CharEncodingFilter implements Filter {
@Override
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
chain.doFilter(request, response);
}
}
See also:
Similar questions:
- character encoding UTF-8 in JSF
- JSF 2.0 request.getParameter return a string with wrong encoding
- POST parameters using wrong encoding in JSF 1.2
这篇关于字符编码后JSF中的乱码字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!