SetLocale在jsp网站中无法使用德语格式化货币

con*_*nce 7 jstl jsp-tags spring-mvc currency-formatting

我对jstl标签库感到困惑:

我想将数字格式化为具有德国风格的货币...但我尝试过的所有内容都没有...

我找到了以下示例,但输出是相同的 - .-

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core"
      prefix="c" %>
<%@taglib uri="http://java.sun.com/jsp/jstl/fmt"
      prefix="fmt" %>

<html>
<head>
    <title>format number</title>
</head>
<body>
    <c:set var="val" value="40.52" />
    <p> Currency in USA
    <fmt:setLocale value="en_US"/>
    <fmt:formatNumber value="${val}"
              type="currency" />
    </p>

    <p>Currency in Germany
    <fmt:setLocale value="de_DE"/>
    <fmt:formatNumber value="${val}"
              type="currency"/>
    </p>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)

这就是输出:

Currency in USA $40.52

Currency in Germany $40.52
Run Code Online (Sandbox Code Playgroud)

那里出了什么问题?

谢谢你的帮助.

con*_*nce 16

太棒了,我偶然得到了解决问题的方法:

诀窍是将setLocale标签的scope-parameter设置为session:D然后它工作^^甜蜜:)

所以正确的代码片段如下所示:

<c:set var="val" value="40.52" />
<p> Currency in USA
<fmt:setLocale value="en_US" scope="session"/>
<fmt:formatNumber value="${val}"
          type="currency" />
</p>

<p>Currency in Germany
<fmt:setLocale value="de_DE" scope="session"/>
<fmt:formatNumber value="${val}"
          type="currency"/>
Run Code Online (Sandbox Code Playgroud)

好的,我真的不知道它为什么会起作用,但这里有一些关于我的项目设置的更多信息:

  • Spring 3 Framework(MVC,Security usw.)
  • 和标准的应用程序和servlet安装程序
  • 每件事都在tomcat 7中运行,所以我使用JSP 2.2

希望能帮助到你.

  • 我有同样的问题(我在tomcat中使用Spring MVC 3.1.4)。当我在&lt;fmt:setLocale&gt;中将范围设置为“ page”或“ request”时,它不适用于&lt;fmt:formatNumber&gt;,这是下面的一行。 (2认同)