Internationalized Page Properties in Tapestry 4.1.2

The tapestry application login page has a property that stores a password that the user enters, which is then compared to a value from the database. If the user enters a password with multibyte characters, for example:

áéíóú

... checking the return value of getPassword () (an abstract method for the corresponding property) gives:

áéíóú

Clearly this is incorrectly encoded. Nevertheless, Firebug reports that the page is submitted in UTF-8, so, presumably, the request to submit the form will also be encoded in UTF-8. Checking the value coming from the database leads to the correct row, so it will not be a problem encoding the OS or IDE. I did not override the default value for Tapestry for org.apache.tapestry.output-encoding in the .application file, and the Tapestry 4 documentation indicates that the default value for the property is UTF-8.

So why does Tapestry seem like it encodes an encoding when setting a property?

The appropriate code should be:

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

Login.page

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

Login.java

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

Update

@Jan Soltis: , , , , , , . .application; org.apache.tapestry.output-encoding, Tapestry 4 , UTF-8. , .

@myself: .

+3
2

. . , .

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

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

.

, getPassword() ? - ( , , ...), , , , ? , ?

, .application

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

Source: https://habr.com/ru/post/1696668/


All Articles