简体   繁体   English

如何在jstl中调用静态方法?

[英]How can I call a static method in jstl?

I have seen this question already answered here but when I tried the same approach, it did not work. 我已经看到这里已经回答了这个问题,但是当我尝试相同的方法时,它没有用。 Here's my code: 这是我的代码:

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));
        }
}
}

Here is my tld file: 这是我的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>

Then here is my JSP file: 这是我的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>

I just want to invoke the static method 我只想调用静态方法

roundOff(double d, int decPlaces)

and pass the variable 并传递变量

${col} 

as the double parameter - d and then any number can go for decPlaces. 作为双精度参数-d,则任何数字都可以用于decPlaces。 Any help would be highly appreciated thanks. 任何帮助将不胜感激谢谢。

 ${func:roundOff(col, 1)}

should do. 应该做。 But AFAIK, the signature in the taglib file should be 但是AFAIK,taglib文件中的签名应该是

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

If this doesn't work, then mention the exact error message you get, instead of just saying "it doesn't work". 如果这不起作用,请提及您得到的确切错误消息,而不是仅仅说“它不起作用”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM