Character encoding garbled characters in post JSF

2019-05-27 08:06发布

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>
 ....

1条回答
Deceive 欺骗
2楼-- · 2019-05-27 08:43

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:

查看更多
登录 后发表回答