File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Struts and the fly likes format attribute in bean:write not working Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "format attribute in bean:write not working" Watch "format attribute in bean:write not working" New topic
Author

format attribute in bean:write not working

Jay Haley
Greenhorn

Joined: May 18, 2004
Posts: 9
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

Joined: Sep 21, 2003
Posts: 415
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

Joined: Dec 19, 2002
Posts: 374
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"


"Write beautiful code; then profile that beautiful code and make little bits of it uglier but faster." --The JavaPerformanceTuning.com team, Newsletter 039.
Jay Haley
Greenhorn

Joined: May 18, 2004
Posts: 9
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

Joined: Sep 21, 2003
Posts: 415
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

Joined: May 18, 2004
Posts: 9
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

Joined: Mar 04, 2004
Posts: 15
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.


SCJP, SCWCD, SCEA
David Hibbs
Ranch Hand

Joined: Dec 19, 2002
Posts: 374
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

Joined: Aug 11, 2003
Posts: 9
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

Joined: May 18, 2004
Posts: 9
Thanks for getting back. I've got several good things to try out after I address a production issue.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: format attribute in bean:write not working
 
Similar Threads
Need an Idea
Problem using html:radio
bean:define not working
Retriving values from html:checkbox in a jsp
Using logic iterate Question