2012-09-06 33 views
1

我已經看到這個問題已經在這裏回答,但是當我嘗試相同的方法時,它不起作用。這裏是我的代碼:如何在jstl中調用靜態方法?

package linear_programming.matrix; 

import java.lang.reflect.Array; 
import java.text.DecimalFormat; 
import java.text.NumberFormat; 

/** 
* 
* @author Jevison7x 
*/ 
public final class MatrixOperations<T extends Number> 
{ 
    /** 
* This method round's off decimal numbers to two decimal places. 
* @param d The decimal number to be rounded off. 
    * @param decPlaces The number of decimal places to round off. 
* @return the rounded off decimal number. 
*/ 
public static double roundOff(double d, int decPlaces) 
{ 
     if(decPlaces < 0) 
      throw new IllegalArgumentException("The number of decimal places cannot be less than 0."); 
     else 
     { 
      String places = ""; 
      for(int i = 0; i < decPlaces; i++) 
      places += "0"; 
      NumberFormat nf = new DecimalFormat("0."+places); 
      return Double.parseDouble(nf.format(d)); 
     } 
} 
} 

這裏是我的TLD文件:

<?xml version="1.0" encoding="UTF-8"?> 
<taglib 
    version="2.1" 
    xmlns="http://java.sun.com/xml/ns/javaee" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-jsptaglibrary_2_1.xsd"> 

    <display-name>Custom Functions</display-name> 
    <tlib-version>1.0</tlib-version> 
    <short-name>func</short-name> 
    <uri>/WEB-INF/tlds/Functions</uri> 

    <function> 
     <name>roundOff</name> 
     <function-class>linear_programming.matrix.MatrixOperations</function-class> 
     <function-signature>double roundOff(double int)</function-signature> 
    </function> 
</taglib> 

那麼這裏就是我的JSP文件:

<%@page contentType="text/html" pageEncoding="UTF-8"%> 
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> 
<%@taglib uri="/WEB-INF/tlds/Functions" prefix="func"%> 
<table> 
    <c:forEach var="Msegment" items="${multipliedSegments}" varStatus="segmentCount"> 
    <tr> 
     <td>X<sub>${segmentCount.count}</sub><sup>T</sup>X<sub>${segmentCount.count}</sub>=</td> 
     <td> 
      <table border="1"> 
     <c:forEach var="row" items="${Msegment}"> 
       <tr> 
      <c:forEach var="col" items="${row}"> 
       <td>${col}</td><!-- Iwant to invoke the static method here --> 
      </c:forEach> 
       </tr> 
     </c:forEach> 
      </table> 
     </td> 
    </tr> 
    <tr><td>&nbsp;</td><td>&nbsp;</td></tr> 
    </c:forEach> 
</table> 

我只是想調用靜態方法

roundOff(double d, int decPlaces) 

並通過變量

${col} 

作爲雙參數 - d然後任何數字都可以用於decPlaces。 任何幫助將不勝感激謝謝。

回答

5
${func:roundOff(col, 1)} 

應該做的。但據我所知,在標籤庫文件中的簽名應該是

double roundOff(double, int) 
         ^--- comma here. 

如果這不起作用,再一提,而不是隻說:「它不工作」你得到確切的錯誤信息。

+0

謝謝。我只是沒有看到這個逗號。你救了我的一天! – Jevison7x