1.题目
实现表单传值,(书名、单价、数量),通过实体类进行数据绑定及计算,最终显示总价等信息。
要求:总价计算需要用实体类进行计算
2.Books实体类
package com.hjj.book;
import lombok.Data;
import javax.annotation.sql.DataSourceDefinition;
/**
* @author:嘉佳 Date:2023/10/8 15:16
**/
public class Book {
private double price;
private int num;
private String bookName;
private double totalPrice;
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public int getNum() {
return num;
}
public void setNum(int num) {
this.num = num;
}
public String getBookName() {
return bookName;
}
public void setBookName(String bookName) {
this.bookName = bookName;
}
public double getTotalPrice() {
return this.price*this.num;
}
public void setTotalPrice(double totalPrice) {
this.totalPrice = totalPrice;
}
}
3.表单信息录入order.jsp
<%@page contentType="text/html;charset=UTF-8"%>
<%@page pageEncoding="UTF-8"%>
<html>
<head>
<meta charset="UTF-8">
<title>信息录入</title>
</head>
<body>
<h2>请输入购书信息</h2>
<form method="post" action="javabean.jsp" >
<label for="bookName">书名</label>
<input type="text" name="bookName" id="bookName" required><br/>
<label for="price">单价</label>
<input type="text" name="price" id="price" required><br/>
<label for="num">数量</label>
<input type="text" name="num" id="num" required><br/>
<input type="submit" value="提交"/>
</form>
</body>
</html>
实施效果:
4.已录入信息展示javaBean.jsp
<%@page contentType="text/html;charset=UTF-8" %>
<%@page pageEncoding="UTF-8" %>
<%@page import="com.hjj.book.Book" %>
<html>
<head>
<title>信息查看</title>
<meta charset="UTF-8">
<style>
h2 {
text-align: center;
}
table {
/* 合并边框 */
border-collapse: collapse;
height: 80px;
/* 居中 */
margin: 0 auto;
}
th {
/* 内边距 */
padding: 5px 20px;
}
table, th, td {
border: 1px solid #000;
}
</style>
</head>
<body>
<jsp:useBean id="Book" class="com.hjj.book.Book"></jsp:useBean>
<jsp:setProperty name="Book" property="*"/>
<h2>商品总价</h2>
<%
request.setCharacterEncoding("UTF-8");
%>
<table>
<tr>
<th>书名</th>
<th>价格</th>
<th>数量</th>
<th>总价</th>
</tr>
<tr>
<td>
<jsp:getProperty name="Book" property="bookName"/>
</td>
<td>
<jsp:getProperty name="Book" property="price"/>
</td>
<td>
<jsp:getProperty name="Book" property="num"/>
</td>
<td>
<jsp:getProperty name="Book" property="totalPrice"/>
</td>
</tr>
</table>
</body>
</html>
实施效果: