如何用 MySQL 数据库中的数据填写 jsp 表格?
How to fill a jsp form by the data from MySQL database?
我有一个 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>
当我取消注释上面代码中 "Rate" 字段中的注释部分时,我得到如下 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 的输入字段类型更改为 "number",但出现相同的错误。
由于错误显示在 value="<c:out value="${product.rate}" />"
行中,并且我在评论时没有错误,我认为我使用错误的方式使用 EL 吗?
但是下拉列表中的产品列表在我使用 EL 的地方显示正确。
有人能告诉我我做错了什么吗?
谢谢。
编辑:
乘积class如下:
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;
}
}
对我来说一切都很好;您确定没有插入 'rate' 作为值吗?
尝试不使用 el 输出 rate,看看实际值是多少:
${product.rate}
创建一个javascript函数
function setRate(el) {
document.getElementById("rate").value = el.value;
}
并将此添加到 select:
<select name="selectedProduct" onchange=setRate(this)>
我有一个 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>
当我取消注释上面代码中 "Rate" 字段中的注释部分时,我得到如下 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 的输入字段类型更改为 "number",但出现相同的错误。
由于错误显示在 value="<c:out value="${product.rate}" />"
行中,并且我在评论时没有错误,我认为我使用错误的方式使用 EL 吗?
但是下拉列表中的产品列表在我使用 EL 的地方显示正确。
有人能告诉我我做错了什么吗?
谢谢。
编辑: 乘积class如下:
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;
}
}
对我来说一切都很好;您确定没有插入 'rate' 作为值吗?
尝试不使用 el 输出 rate,看看实际值是多少:
${product.rate}
创建一个javascript函数
function setRate(el) {
document.getElementById("rate").value = el.value;
}
并将此添加到 select:
<select name="selectedProduct" onchange=setRate(this)>