如何在 JSP 中格式化值?

发布于 2024-10-25 06:08:58 字数 396 浏览 3 评论 0原文

我正在开发一个使用 Struts (v1) 的 J2EE 应用程序,并且我想格式化在 JSP 中显示的值。要显示的值是一个 7 或 8 位整数,我想在其中插入破折号,如下所示:

1234567 -> 1-234-567
12345678 -> 12-345-678

最好的方法是什么?我的第一个想法是在我的表单 bean 中编写一个特殊的 getter,它将返回特殊格式的字符串,而不是整数。当然,这看起来很臭——我不想只是为了格式化 JSP 中的内容而向我的 bean 添加方法。

我考虑的另一个选择是使用 bean:write 的格式属性。不幸的是,当您尝试格式化日期时,我可以找到很多有关如何使用格式的文档,但我似乎找不到处理任意值的正确语法。

有什么想法吗?

I'm working on a J2EE application which uses Struts (v1) and I would like to format a value being displayed in a JSP. The value to be displayed is a 7 or 8 digit integer and I would like to insert dashes into it, like so:

1234567 -> 1-234-567
12345678 -> 12-345-678

What is the best way to go about this? My first thought was to write a special getter in my form bean which would return the specially formatted String, rather than the Integer. That, of course, seems very smelly - I don't want to add methods to my beans just to format things in my JSP.

Another option I considered was to use bean:write's format attribute. Unfortunately, I can find lots of documentation on how to use format when you're trying to format a date, but I just can't seem to find the correct syntax for working with arbitrary values.

Any thoughts?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

酒绊 2024-11-01 06:08:58

我对 fmt:formatNumber 做了一些研究...我认为使用 - 会导致一些奇怪的问题,因为 , 是唯一安全的分组分隔符。根据文档,您似乎可以使用单引号注释特殊字符,但我认为它不适用于您的情况。

所以,这是我的解决方法:-

<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>

<c:set var="foo" value="12345678"/>
<fmt:formatNumber value="${foo}" pattern="00,00,000" var="result"/>
${fn:replace(result, ",", "-")}

这对我有用。基本上,我使用逗号而不是破折号,然后使用替换函数将其转换回破折号......这不是一个优雅的解决方案。

I did some researching on fmt:formatNumber... I think using - is causing some weird problem because , is the only safe grouping separator. Based on the documentation, it seems like you can comment special character using single quotes, but I don't think it applies in your case.

So, here's my workaround:-

<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>

<c:set var="foo" value="12345678"/>
<fmt:formatNumber value="${foo}" pattern="00,00,000" var="result"/>
${fn:replace(result, ",", "-")}

This works for me. Basically, I use commas instead of dashes, then use the replace function to convert it back to dashes... not quite an elegant solution.

最偏执的依靠 2024-11-01 06:08:58

使用 bean:write,您只能使用 DateFormat 和 DecimalFormat,这不能完成您想要执行的操作(DecimalFormat 不执行完全自由格式的格式化,并保留 - 作为特殊字符)。

Struts 文档建议在 ActionForm 中对其进行格式化: http:// struts.apache.org/1.x/struts-taglib/faq.html#tags

如果您真的非常想在 JSP 中执行此操作,您可能必须编写自己的标记库来处理它。即使 JSTL 也无法真正为您处理此问题: http://download.oracle.com/docs/cd/E17802_01/products/products/jsp/jstl/1.1/docs/tlddocs/fmt/tld-summary.html

With bean:write, you can only use DateFormat and DecimalFormat, which can't do what you're looking to do here (DecimalFormat doesn't do totally free form formatting and reserves the - as a special character).

The Struts docs recommend just formatting it in your ActionForm: http://struts.apache.org/1.x/struts-taglib/faq.html#tags

If you really, really want to do it in your JSP, you'll probably have to write your own tag library to handle it. Not even the JSTL can really even handle this for you: http://download.oracle.com/docs/cd/E17802_01/products/products/jsp/jstl/1.1/docs/tlddocs/fmt/tld-summary.html

但可醉心 2024-11-01 06:08:58

使用类似的东西,它应该对你有帮助!!:

<bean:write name="formBean" property="dashedNum" format="###'-'###'-'###"/>

use something along these lines, it should help you out!!:

<bean:write name="formBean" property="dashedNum" format="###'-'###'-'###"/>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文