我有一个jsp页面,其中有一些从数据库中填充的产品的下拉列表。我想要的是,当用户选择产品时,我希望从数据库中自动填充费率字段。 我的jsp页面:
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html, charset=utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="http://code.jquery.com/jquery-1.8.3.js"></script>
<script src="http://code.jquery.com/ui/1.9.2/jquery-ui.js"></script>
<link rel="stylesheet" href="http://code.jquery.com/ui/1.9.2/themes/base/jquery-ui.css" />
<title>Purchase new product </title>
</head>
<body>
<script>
$(function() {
$('input[name=date]').datepicker();
});
function multiply()
{
var q = document.getElementById('quantity').value;
var r = document.getElementById('rateid').value;
document.getElementById('total').value = q*r;
}
</script>
<div class="container">
<h1>Place your order:</h1><br/>
<form method="post" action="PurchaseController" name="pform">
<table class="table table-striped table-hover table-condensed table-bordered">
<tr>
<td>Product:</td>
<td><select name="selectedProduct">
<option value=0>Select one</option>
<c:forEach items="${product}" var="product">
<option value="${product.pid}"><c:out value="${product.name}" /> </option>
</c:forEach>
</select>
</td>
</tr>
<tr>
<td>Quantity:</td>
<td><input type="number" name="quantity" id="quantity" oninput="multiply()"
onkeypress="return event.charCode >=48" min="1" required>
</td>
</tr>
<tr>
<td>Rate:</td>
<td><input type="text" name="rate" id="rateid" oninput="multiply()" <%-- value="<c:out value="${product.rate}" />" --%> >
</td>
</tr>
<tr>
<td>Total:</td>
<td><input type="number" name="total" id="total" readonly ></td>
</tr>
<tr>
<td>Date:</td>
<td><input type="text" name="date" placeholder="Date" required></td>
</tr>
</table>
<input type="submit" value="Submit" class="btn btn-primary">
<input type="reset" value="Reset" class="btn btn-danger">
</form>
<br/>
<a href="index.jsp">Back to home</a>
</div>
</body>
</html>
当我取消注释&#34;评分&#34;在上面的代码中,我得到NumberFormatException错误如下:
org.apache.jasper.JasperException: An exception occurred processing JSP page [/purchase.jsp] at line [55]
52: <tr>
53: <td>Rate:</td>
54: <td><input type="text" name="rate" id="rateid" oninput="multiply()"
55: value="<c:out value="${product.rate}" />" >
56: </td>
57: </tr>
58: <td>Total:</td>
Stacktrace:
org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:584)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:481)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:386)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:330)
javax.servlet.http.HttpServlet.service(HttpServlet.java:742)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
com.product.controller.ProductController.doGet(ProductController.java:57)
javax.servlet.http.HttpServlet.service(HttpServlet.java:635)
javax.servlet.http.HttpServlet.service(HttpServlet.java:742)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
Root Cause
java.lang.NumberFormatException: For input string: "rate"
java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
java.lang.Integer.parseInt(Integer.java:580)
java.lang.Integer.parseInt(Integer.java:615)
javax.el.ListELResolver.coerce(ListELResolver.java:150)
javax.el.ListELResolver.getValue(ListELResolver.java:67)
org.apache.jasper.el.JasperELResolver.getValue(JasperELResolver.java:110)
org.apache.el.parser.AstValue.getValue(AstValue.java:169)
org.apache.el.ValueExpressionImpl.getValue(ValueExpressionImpl.java:190)
org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate(PageContextImpl.java:944)
org.apache.jsp.purchase_jsp._jspx_meth_c_005fout_005f1(purchase_jsp.java:310)
org.apache.jsp.purchase_jsp._jspService(purchase_jsp.java:178)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
javax.servlet.http.HttpServlet.service(HttpServlet.java:742)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:443)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:386)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:330)
javax.servlet.http.HttpServlet.service(HttpServlet.java:742)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
com.product.controller.ProductController.doGet(ProductController.java:57)
javax.servlet.http.HttpServlet.service(HttpServlet.java:635)
javax.servlet.http.HttpServlet.service(HttpServlet.java:742)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
我尝试将Rate的输入字段类型更改为&#34; number&#34;但是会发生同样的错误。
由于错误显示在value="<c:out value="${product.rate}" />"
行中,当我对此进行评论时没有错误,我认为我是以错误的方式使用EL?
但下拉列表中的产品列表也正确显示我使用EL的位置。
有人能告诉我我做错了吗?
感谢。
修改 产品类如下:
public class Product {
private int pid;
private String name;
private int quantity;
private int rate;
private int total;
private Date date;
public int getPid() {
return pid;
}
public void setPid(int pid) {
this.pid = pid;
}
public Date getDate() {
return date;
}
public String getName() {
return name;
}
public int getQuantity() {
return quantity;
}
public int getRate() {
return rate;
}
public int getTotal() {
return total;
}
public void setDate(Date date) {
this.date = date;
}
public void setName(String name) {
this.name = name;
}
public void setQuantity(int quantity) {
this.quantity = quantity;
}
public void setRate(int rate) {
this.rate = rate;
}
public void setTotal(int total) {
this.total = total;
}
}
答案 0 :(得分:0)
对我而言,一切看起来都很好;你确定你没有插入'rate'作为一个值吗?
尝试输出不带el的速率,看看实际值是多少:
${product.rate}
创建一个javascript函数
function setRate(el) {
document.getElementById("rate").value = el.value;
}
并将其添加到选择:
<select name="selectedProduct" onchange=setRate(this)>