-1

我是Java Web 应用程序的完整初学者,我正在尝试设计一个页面来显示ArrayList 的总费用。我的 Service 类中有一个从 ArrayList 返回双精度的方法:

public double totalPayroll(String name) {
    double total = 0;
    for (int i=0; i < payrollList.size(); i++) {
        total += payrollList.get(i).calWage();
    }
    return total;
}

在我的 servlet 上,我想为此方法在 Servlet 中的 doGet 方法中添加一个请求,以便我可以在我的 jsp 文件中显示总数。在 Servlet 文件上,我尝试过:

request.setAttribute("productList", service.totalPayroll());

并为 jsp 文件尝试了以下操作:

"<h3>Total Payroll to Pay: </h3><c:out value="$${payroll.totalPayroll()}" />"

但我收到一个错误,该方法不适用于该参数。似乎我需要设置属性以匹配方法的返回值,但我无法弄清楚如何在 Servlet 上执行此操作以及如何在 jsp 文件上调用它。我感谢有关如何解决此问题的任何帮助或指导。谢谢!

4

1 回答 1

0

那么问题这个问题还不清楚,但我仍然想根据我对问题的理解给出一个解决方案

试试这个:

在 Servlet 上

         double totalPayroll=00;
        totalPayroll=service.totalPayroll("parameterValue"); //You have to send parameter/argument as your method defination requires 1 parameter/argument 
        request.setAttribute("Demo", totalPayroll);
        RequestDispatcher rd= request.getRequestDispatcher("yourjspfile.jsp"); //Enter the name of your jsp file
        rd.forward(request, response);

在 JSP 上:

<%@ page isELIgnored = "false" %> //top of the page
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>  //top of the page &add jstl jar to your project
  Total Payroll to Pay: <c:out value="${Demo}"/> You can add condition for its visiblity. 
于 2021-04-20T08:32:58.757 回答