如何为 returns servlet 中的数字并将其传递给 jsp 文件的方法设置参数?
How to set a parameter for a method that returns a number in a servlet and pass it to jsp file?
我是 java 网络应用程序的完全初学者,我正在尝试设计一个页面来显示 ArrayList 的总费用。我的服务 class 中有一个方法 return 是来自 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()}" />"
但我收到一条错误消息,指出该方法不适用于该参数。似乎我需要设置属性以匹配方法的 return 值,但我不知道如何在 Servlet 上执行此操作以及如何在 jsp 文件上调用它。我感谢有关如何解决此问题的任何帮助或指导。谢谢!
好吧,问题不清楚,但我仍然想根据我对问题的理解给出解决方案
试试这个:
在 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.
我是 java 网络应用程序的完全初学者,我正在尝试设计一个页面来显示 ArrayList 的总费用。我的服务 class 中有一个方法 return 是来自 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()}" />"
但我收到一条错误消息,指出该方法不适用于该参数。似乎我需要设置属性以匹配方法的 return 值,但我不知道如何在 Servlet 上执行此操作以及如何在 jsp 文件上调用它。我感谢有关如何解决此问题的任何帮助或指导。谢谢!
好吧,问题不清楚,但我仍然想根据我对问题的理解给出解决方案
试试这个:
在 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.