• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

format attribute in bean:write not working

 
Jay Haley
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have been unsuccessful in utilizing the format attribute with bean:write. I am looking for some insight on what I am doing wrong. The code clip below displays the items in the ArrayList in the same format that they were stored. I don�t get an error with the code below, but the format attribute does nothing. I am using Struts 1.1

<%@ taglib uri="/tags/struts-logic" prefix="logic" %>
<%@ taglib uri="/tags/struts-html" prefix="html" %>
<%@ taglib uri="/tags/struts-bean" prefix="bean" %>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"%>
<html:html>
<HEAD>
<TITLE></TITLE>
</HEAD>
<BODY>

<% {
java.util.ArrayList list = new java.util.ArrayList();
list.add("55555");
list.add("44444.4");
pageContext.setAttribute("list", list, PageContext.PAGE_SCOPE);
}
%>
<logic:iterate id="item" name="list" indexId="index" >
<bean:write name="item" format="#,000.00" /><br>
</logic:iterate>
 
sreenath reddy
Ranch Hand
Posts: 415
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

Better u go for formatKey attribute of this bean write tag .there in format key u need to give a name which will look up in to resource bundle and display in that format .this is best used for date representation

for eg

org.apache.struts.taglib.bean.format.date = dd/MM/yyyy HH:mm

u make an entry as above in the reosurce bundle and in ur jsp give the key name for formatKey then it will work .......
 
David Hibbs
Ranch Hand
Posts: 374
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The problem is less with your tag than with your format. I'm guessing by your variable names that you're trying to format an index of less than 1,000 to increments of 1,000 -- i.e. 1 to 1,000 2 to 2,000 etc? Likely you need to change it to something like format="{0}000.00"
 
Jay Haley
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm still having the same problem. Here's what I tried..am I missing someting? After changing application.properties I did restart my server.


///////application.properties
display.format.money1=#,000.00
display.format.money2={0}000.00


////////my jsp page
<logic:iterate id="item" name="list" indexId="index" >
<bean:write name="item" format="#,000.00" /><br>
</logic:iterate>
<logic:iterate id="item" name="list" indexId="index" >
<bean:write name="item" format="{0}000.00" /><br>
</logic:iterate>
<logic:iterate id="item" name="list" indexId="index" >
<bean:write name="item" formatKey="display.format.money1" /><br>
</logic:iterate>
<logic:iterate id="item" name="list" indexId="index" >
<bean:write name="item" formatKey="display.format.money2" /><br>
</logic:iterate>
 
sreenath reddy
Ranch Hand
Posts: 415
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
seeem some problem in conversion of the variable to the format u had given .

can u give us a clue whats the error u r getting in ur jsp
 
Jay Haley
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not getting and error. The last example I posted produces the following results.

55555
44444.4
55555
44444.4
55555
44444.4
55555
44444.4
 
Jose Zaleta
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jay,

I've just tried your formatting code in a regular java application. The problems seems to be this: You are trying to format numbers represented as String objects. DecimalFormat does not allow for this input, it requires Number objects.

This means:
DecimalFormat formatter = new DecimalFormat("#,000.00");
System.out.println(formatter.format(new Double("55555")));
will work, and what you are doing is equivalent to:
DecimalFormat formatter = new DecimalFormat("#,000.00");
System.out.println(formatter.format("55555"));
which throws an Exception.

You should then try to fill your ArrayList with Double objects, instead of String objects.

Let me know if this helped you out.
 
David Hibbs
Ranch Hand
Posts: 374
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Jose Zaleta:
Jay,

I've just tried your formatting code in a regular java application. The problems seems to be this: You are trying to format numbers represented as String objects. DecimalFormat does not allow for this input, it requires Number objects.


Jose is on the right track; seeing your output made me suspect something even more, though--that if your object is an instance of String, it would not format it. Rummaging in the source for the bean write tag, I find the following snippet in the code where the format string is applied:



There you have it. You can't alter the format of a String object using the bean:write format options. Of course, you COULD sumbit a patch/Feature request...
 
Shed Hollaway
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jay,

The format function in bean:write I believe is deprecated. Here's a option using JSTLs

<%@ page language="java"%>
<%@ taglib uri="/WEB-INF/c-rt.tld" prefix="c" %>
<%@ taglib uri="/WEB-INF/fmt-rt.tld" prefix="fmt-rt" %>
<html>
<head>
<title>JSP for myformatNoForm form</title>
</head>
<body>
<% {
java.util.ArrayList list = new java.util.ArrayList();
list.add("55555");
list.add("44444.4");
list.add("23342.354");
pageContext.setAttribute("list", list, PageContext.PAGE_SCOPE);
}
%>
<b>This will do it.</b><br>
<c:forEach var="i" items="${list}" ><fmt-rt:formatNumber value="${i}" type="currency" /><br></c:forEach>
</body>
</html>

That was a good mind twister, I celebrate now
 
Jay Haley
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for getting back. I've got several good things to try out after I address a production issue.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic