开发者

JSTL fmt tag does not work in IE?

开发者 https://www.devze.com 2022-12-19 10:00 出处:网络
IE seems to ignore the fmt tag in JSTL. I\'ve tried with fmt:formatNumber and fmt:formatDate, and neither work in IE but they work in all other browsers. (I\'m using IE8 and JSTL 1.1)

IE seems to ignore the fmt tag in JSTL. I've tried with fmt:formatNumber and fmt:formatDate, and neither work in IE but they work in all other browsers. (I'm using IE8 and JSTL 1.1)

Is this a known problem?

Here's an example of code that doesn't work:

<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<fmt:formatNumber maxIntegerDigits="2">2003</fmt:format开发者_如何学CNumber>
<fmt:formatNumber maxIntegerDigits="2" value="2003" />


JSP/JSTL is executed server-side, it has nothing to do with what browser you're using.

Something is mis-configured on your server.

What do you mean by "doesn't work"? Does it throw an exception, does it give you a blank page, what?


edit: The fmt tag is influenced by the current Locale of the executing thread, which Spring takes from the http request. if IE is passing a different locale indication than the other browsers (for some reason, I don't know why), then the fmt tag could change its output.

Either you need to make sure all browsers are passing the same locale, or you can override it in the JSP to force it to be the same for everyone:

<fmt:setLocale> - Sets the default locale for the specified scope. This will override the browserbased locale.


I found a solution! If I use the fmt:setLocale tag first, the other tags start working for some reason... Doesn't seem to matter what locale I set it to either.


Any browser that doesn't send an Accept-Language request header will have this issue. The best solution is to set the fallbackLocale property, which is the Locale that will be used when no Accept-Language is specified.

This property can be set at any scope (page, request, application, session). The property that needs to be set is 'javax.servlet.jsp.jstl.fmt.fallbackLocale.scope', where scope is the appropriate value for where you are setting it.

So, to set it for the entire application, do something like:

servletContext.setAttribute("javax.servlet.jsp.jstl.fmt.fallbackLocale.application", java.util.Locale.US);

The best place to put this would be in a ServletContextListener.

If you only need to do this for certain pages, then you could also do the following in a JSP:

<% pageContext.setAttribute("javax.servlet.jsp.jstl.fmt.fallbackLocale.page", java.util.Locale.US); %>

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号