我希望集中尽可能使用 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);
}
}
答案 0 :(得分: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}" />
答案 1 :(得分: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>
希望这会对你有所帮助。欢呼声。