我的 Tapestry 应用程序中的登录页面有一个属性,其中存储用户输入的密码,然后将其与数据库中的值进行比较。如果用户输入的密码包含多字节字符,例如:

áéíóú

...检查 getPassword() (相应属性的抽象方法)的返回值给出:

áéíóú

显然,这没有正确编码。然而 Firebug 报告该页面以 UTF-8 提供,因此表单提交请求可能也会以 UTF-8 编码。检查来自数据库的值会生成正确的字符串,因此它不会出现操作系统或 IDE 编码问题。我没有覆盖 .application 文件中 org.apache.tapestry.output-encoding Tapestry 的默认值,以及 Tapestry 4 文档 指示该属性的默认值为 UTF-8。

那么为什么 Tapestry 在设置属性时会出现编码错误呢?

相关代码如下:

登录.html

<html jwcid="@Shell" doctype='html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"' ...>
    <body jwcid="@Body">
        ...
        <form jwcid="@Form" listener="listener:attemptLogin" ...>
            ...
            <input jwcid="password"/>
            ...
        </form>
        ...
     </body>
</html>

登录页面

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE page-specification
    PUBLIC "-//Apache Software Foundation//Tapestry Specification 4.0//EN"
    "http://jakarta.apache.org/tapestry/dtd/Tapestry_4_0.dtd">

<page-specification class="mycode.Login">
    ...
    <property name="password" />
    ...
    <component id="password" type="TextField">
        <binding name="value" value="password"/>
        <binding name="hidden" value="true"/>
        ...
    </component>
    ...
</page-specification>

登录.java

...
public abstract class Login extends BasePage {
    ...
    public abstract String getPassword();
    ...
    public void attemptLogin() {
        // At this point, inspecting getPassword() returns
        // the incorrectly encoded String.
    }
    ...
}

更新

@扬·索尔蒂斯:好吧,如果我检查来自数据库的值,它会显示正确的字符串,因此看起来我的编辑器、操作系统和数据库都正确编码了该值。我还检查了我的 .application 文件;它不包含 org.apache.tapestry.output-encoding 条目,并且 Tapestry 4 文档 指示此属性的默认值为 UTF-8。我更新了上面的描述以反映您问题的答案。

@我:找到解决方案。

有帮助吗?

解决方案 2

我发现了问题。在 Tapestry 或我的页面类出现破解之前,Tomcat 就已经破坏了参数。创建一个强制执行所需字符编码的 Servlet 过滤器可以修复该问题。

字符编码过滤器.java

package mycode;

import java.io.IOException;

import javax.servlet.*;

/**
 * Allows you to enforce a particular character encoding on incoming requests.
 * @author Robert J. Walker
 */
public class CharacterEncodingFilter implements Filter {
    private static final String ENCODINGPARAM = "encoding";

    private String encoding;

    public void init(FilterConfig config) throws ServletException {
        encoding = config.getInitParameter(ENCODINGPARAM);

        if (encoding != null) {
            encoding = encoding.trim();
        }
    }

    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
            throws IOException, ServletException {
        request.setCharacterEncoding(encoding);
        chain.doFilter(request, response);
    }

    public void destroy() {
        // do nothing
    }
}

网络.xml

<web-app>
    ...
    <filter>
        <filter-name>characterEncoding</filter-name>
        <filter-class>mycode.CharacterEncodingFilter</filter-class>
        <init-param>
            <param-name>encoding</param-name>
            <param-value>UTF-8</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>characterEncoding</filter-name>
        <url-pattern>/app/*</url-pattern>
    </filter-mapping>
    ...
</web-app>

其他提示

一切似乎都是正确的。

你是 非常肯定 getPassword() 返回垃圾?难道其他人(你的编辑器、操作系统、数据库……)在向你显示它时不知道它是一个 unicode 字符串,而密码可能完全没问题吗?什么 确切地 让你觉得它是垃圾吗?

我还要确保 .application 配置文件中没有奇怪的编码设置

<meta key="org.apache.tapestry.output-encoding" value="some strange encoding"/>
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top