gpt4 book ai didi

java - 如何在 jSTL 中调用静态方法?

转载 作者:行者123 更新时间:2023-11-29 08:04:24 25 4
gpt4 key购买 nike

我看到这里已经回答了这个问题,但是当我尝试相同的方法时,它没有用。这是我的代码:

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

这是我的顶级域名文件:

<?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。非常感谢任何帮助。

最佳答案

 ${func:roundOff(col, 1)}

应该做的。但是据我所知,taglib 文件中的签名应该是

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

如果这不起作用,请提及您收到的确切错误消息,而不仅仅是说“它不起作用”。

关于java - 如何在 jSTL 中调用静态方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12297224/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com