views:

2808

answers:

6

I am getting the value of amount like 4567.00 , 8976.00 etc. Now while dispalying this value in displaytag i would like to print it as $4567.00 instead of just 4567.00. How can i do that? Provided i just want to use display tag. I can acheive the same thing using core:out tag.

$<core:out value="${variableInMyList}" />


Answer Found [ How i did it ]

Create a new class:

public class NumberFormatDecorator implements DisplaytagColumnDecorator{
    Logger logger = MyLogger.getInstance ( );

    public Object decorate(Object columnValue, PageContext pageContext, MediaTypeEnum media) throws DecoratorException {  
     try
        {
      Object colVal = columnValue;
      if ( columnValue != null ){
       colVal = Double.parseDouble( (String)columnValue );
      }
      return colVal;
        }catch ( Exception nfe ){}
        logger.error( "Unable to convert to Numeric Format");
        return columnValue; // even if there is some exception return the original value
    }
}

now in display tag

<displaytag:column title="Amount" property="amount" decorator="com.rj.blah.utils.decorator.NumberFormatDecorator" format="$ {0,number,0,000.00}"/>

Note: we can use the MessageFormat in format attribute of displaytag:column

A: 

Column decorators - USe this

joe
+3  A: 

DisplayTab is not very JSTL or EL friendly, and doesn't support that style of formatting. Instead, you need to extend the TableDecorator class and put a reference to it using the decorator attribute of the display:table tag.

Your decorator subclass should define a getter method for your formatted currency column, something like:

public class MyTableDecorator extends TableDecorator {
    public String getCurrency() {
        MyRowType row = getCurrentRowObject();
        return row.getCurrency.format();
    }
}

and

<display:table name="myList" decorator="test.MyTableDecorator">
    <display:column property="myProperty" title="My Property"/>
    <display:column property="currency" title="Currency"/>
</display:table>

Alternatively, you can implement the DisplaytagColumnDecorator interface, and reference that decorator from the JSP:

<display:table name="myList">
    <display:column property="myProperty" title="My Property"/>
    <display:column property="currency" title="Currency" decorator="test.MyColumnDecorator"/>
</display:table>

See the documentation for more information

skaffman
+1  A: 

You could use a decorator.

you would have something like

class myDecorator extends TableDecorator{

 public String getCurrency(){
   MyClass myClass  = (MyClass)getCurrentRow();


   return "$"+myClass.getCurrency;

 }
}

Check them out! http://displaytag.sourceforge.net/10/tut_decorators.html

If you don't want to use decorators, you could use the id attribute and JSTL

<display:table htmlId="list" name="mylist" id="row">

   <display:column>
    <%-- row is your current list object. row.currency calls getCurrency()
         $ goes right out to HTML
    --%>
     $ <c:out="${row.currency}"/>
   </display:column>
</display:table>

From display:tag tag reference

id: see uid

uid: Unique id used to identify this table. The object representing the current row is also added to the pageContext under this name and the current row number is added using the key uid_rowNum. Two tables in the same page can't have the same uid (paging and sorting will affect both). If no "htmlId" is specified the same value will be used for the html id of the generated table

Tom
**$ <c:out="${row.currency}"/>** as i said this is not what i want to do. But thanx for the decorator concept
Rakesh Juyal
+1  A: 

What do you need your class for? You could write it as follows:

<displaytag:column property="amount" format="$ {0,number,0,000.00}"/>
A: 

How can I change 0,000.00 by 0.000,00 ? Thanks

Diego
You should better ask this as a separate question, if it has not been asked already. The "Ask Question" button is in the top right.
sth
A: 
<displaytag:column property="amount" format="$ {0,number,0,000.00}"/>

Return $ null for null value of amount. How to skip that?

spsaran