First of all, I'd like to say that I've already posted this question in another forum, but as I haven't had any answers until now, and this is an important issue to me, I'm asking it here too.
The开发者_开发百科 HTML <script>
tag has the charset
attribute, but I can't specify this attribute using the <h:outputScript>
tag. I tried to use the preRenderComponent
system event and put the attribute manually but it had no effect; the attribute wasn't rendered. Is there a way to specify the charset
attribute, even programmatically?
Just use UTF-8 all the time. Facelets defaults to UTF-8 already. You should take care that you configure your editor/IDE to save all textbased resources as UTF-8. In Eclipse, you need to go to Window » Preferences and enter filter text encoding. In all of the filtered preferences (Workspace, JSP files, etcetera) you can select the desired encoding from a dropdown.
You might want to make minor edits (add/remove space or something) on existing files to force Eclipse to save them as UTF-8 again.
See also:
- Unicode - How to get the characters right?
If your javascript file resides within another JAR referenced by your webapp and you can't have it converted to another encoding, then you should use a filter to set a content type for that particular file. I noticed that there's no common setting for it in JSF.
Also, I found out that it's possible to override the default renderer for javax.faces.resource.Script
in JSF but I don't think it would be a good strategy, so if someone finds a better solution, please, post it!
public class SetContentEncodingFilter implements Filter {
private final Map<String, String> pathEncodingMap = new HashMap<>();
@Override
public void destroy() {
pathEncodingMap.clear();
}
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
HttpServletRequest req = (HttpServletRequest) request;
String requestURI = req.getRequestURI();
if (requestURI != null && pathEncodingMap.containsKey(requestURI)) {
String encoding = pathEncodingMap.get(requestURI);
response.setCharacterEncoding(encoding);
}
chain.doFilter(request, response);
}
@Override
public void init(FilterConfig config) throws ServletException {
Enumeration<String> parameterNames = config.getInitParameterNames();
while (parameterNames.hasMoreElements()) {
String path = parameterNames.nextElement();
String encoding = config.getInitParameter(path);
pathEncodingMap.put(config.getServletContext().getContextPath() + path, encoding);
}
}
}
Declare the filter in your web.xml:
<filter>
<filter-name>localizedResources</filter-name>
<filter-class>br.mpt.mp.recad.config.SetContentEncodingFilter</filter-class>
<init-param>
<param-name>/javax.faces.resource/jq/ui/i18n/datepicker-pt.js.xhtml</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>localizedResources</filter-name>
<servlet-name>Faces Servlet</servlet-name>
</filter-mapping>
or if you prefer, shall you use org.springframework.web.filter.CharacterEncodingFilter
from Spring
There's also an OmniFaces alternative: CharacterEncodingFilter
精彩评论