有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

Tapestry 4.1.2中的java国际化页面属性

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

áéíóú

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

áéíóú

很明显,这是没有正确编码的。然而Firebug报告说页面是用UTF-8提供的,所以表单提交请求可能也会用UTF-8编码。检查来自数据库的值会生成正确的字符串,这样就不会出现OS或IDE编码问题。我没有覆盖Tapestry对org的默认值。阿帕奇。壁毯在中输出编码。应用程序文件,Tapestry 4documentation指示该属性的默认值为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>

登录。爪哇

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

更新

@Jan Soltis:如果我检查来自数据库的值,它会显示正确的字符串,因此我的编辑器、操作系统和数据库似乎都正确地编码了该值。我还检查了我的手机。申请文件;它不包含组织。阿帕奇。壁毯输出编码项,Tapestry 4documentation指示此属性的默认值为UTF-8。我已经更新了上面的描述,以反映对您问题的回答

@我自己:找到了解决方案


共 (2) 个答案

  1. # 1 楼答案

    我发现了问题。在Tapestry或我的page类出现问题之前,Tomcat已经破坏了参数。创建一个servlet过滤器来执行所需的字符编码,修复了这个问题

    字符编码过滤器。爪哇

    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>
    
  2. # 2 楼答案

    一切似乎都是正确的

    你真的确定getPassword()会返回垃圾吗?不是别人(你的编辑器、操作系统、数据库等)吗当它向您显示它时,它不知道它是一个unicode字符串,而密码可能完全正常?到底是什么让你觉得这是垃圾

    我也会确保在数据库中没有奇怪的编码设置。应用程序配置文件

    <meta key="org.apache.tapestry.output-encoding" value="some strange encoding"/>