0

我想尽可能集中我的JSF页面使用的字符编码。

WEB-INF文件夹下,我有两个项目配置文件,一个用于开发环境(称为projectDEV.properties),另一个用于生产环境(称为projectPRO.properties)。

Spring通过以下方式正确加载了正确的包:

<context:property-placeholder 
        location="WEB-INF/project${my.environment}.properties" />

...其中“ my.environment ”是Tomcat 启动配置参数

-Dmy.environment=PRO   or  -Dmy.environment=DEV

在这两个属性文件中,我有下一个属性声明:

cfg.i18n.encoding1=UTF-8

问题来了:

如何在我的 .xhtml 页面中使用该属性?

<?xml version="1.0" encoding="${cfg.i18n.encoding}" ?>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:ui="http://java.sun.com/jsf/facelets"
    xmlns:p="http://primefaces.org/ui"
    xmlns:c="http://java.sun.com/jsp/jstl/core">

    <f:view contentType="text/html">

        <h:head>
            <meta http-equiv="Content-Type" content="text/html; charset=${cfg.i18n.encoding}" />
            <title>Home</title>
        </h:head>
        <h:body>
            HELLO!
        </h:body>

    </f:view>
</html>

谢谢!


/**
 * Servlet 2.3/2.4 Filter that allows one to specify a character encoding for
 * requests. This is useful because current browsers typically do not set a
 * character encoding even if specified in the HTML page or form.
 *
 * <p>This filter can either apply its encoding if the request does not
 * already specify an encoding, or enforce this filter's encoding in any case
 * ("forceEncoding"="true"). In the latter case, the encoding will also be
 * applied as default response encoding on Servlet 2.4+ containers (although
 * this will usually be overridden by a full content type set in the view).
 *
 * @author Juergen Hoeller
 * @since 15.03.2004
 * @see #setEncoding
 * @see #setForceEncoding
 * @see javax.servlet.http.HttpServletRequest#setCharacterEncoding
 * @see javax.servlet.http.HttpServletResponse#setCharacterEncoding
 */
public class CharacterEncodingFilter extends OncePerRequestFilter {

    private String encoding;

    private boolean forceEncoding = false;


    /**
     * Set the encoding to use for requests. This encoding will be passed into a
     * {@link javax.servlet.http.HttpServletRequest#setCharacterEncoding} call.
     * <p>Whether this encoding will override existing request encodings
     * (and whether it will be applied as default response encoding as well)
     * depends on the {@link #setForceEncoding "forceEncoding"} flag.
     */
    public void setEncoding(String encoding) {
        this.encoding = encoding;
    }

    /**
     * Set whether the configured {@link #setEncoding encoding} of this filter
     * is supposed to override existing request and response encodings.
     * <p>Default is "false", i.e. do not modify the encoding if
     * {@link javax.servlet.http.HttpServletRequest#getCharacterEncoding()}
     * returns a non-null value. Switch this to "true" to enforce the specified
     * encoding in any case, applying it as default response encoding as well.
     * <p>Note that the response encoding will only be set on Servlet 2.4+
     * containers, since Servlet 2.3 did not provide a facility for setting
     * a default response encoding.
     */
    public void setForceEncoding(boolean forceEncoding) {
        this.forceEncoding = forceEncoding;
    }


    @Override
    protected void doFilterInternal(
            HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)
            throws ServletException, IOException {

        if (this.encoding != null && (this.forceEncoding || request.getCharacterEncoding() == null)) {
            request.setCharacterEncoding(this.encoding);
            if (this.forceEncoding) {
                response.setCharacterEncoding(this.encoding);
            }
        }
        filterChain.doFilter(request, response);
    }

}
4

2 回答 2

1

我认为没有办法直接引用 JSF 页面中的配置属性,因为PropertyPlaceholderConfigurer它没有为此公开任何 API。

你可以试试这个技巧。像这样创建一个常规的 Java bean:

public class Config {

  private String charset;

  /* getters + setters */

}

然后将其添加到您的 Spring 配置中,如下所示:

<bean id="config" class="com.example.myapp.Config">
  <property name="charset"><value>${cfg.i18n.encoding1}</value></property>
</bean>

然后你应该能够像这样读取属性:

<meta http-equiv="Content-Type" content="text/html; charset=#{config.charset}" />
于 2012-06-07T15:21:06.807 回答
1

您可以使用 SpringFramework 提供的过滤器来集中 html 文件的编码。您可以在 web.xml 文件中定义此过滤器,如下所示。

    <filter>
        <filter-name>encodingFilter</filter-name>
        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
        <init-param>
            <param-name>encoding</param-name>
            <param-value>UTF-8</param-value>
        </init-param>
        <init-param>
            <param-name>forceEncoding</param-name>
            <param-value>true</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>encodingFilter</filter-name>
        <url-pattern>/</url-pattern>
    </filter-mapping>

希望这对您有所帮助。干杯。

于 2012-06-08T06:04:18.303 回答