利用Javabean+JSP去实现商品进销存中任何一个场景,实现对商品数量的绑定。
先引入几个需要的jar包
add.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ page import="mybean.Goods" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<base href="<%=basePath%>">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3,keyword4">
<meta http-equiv="description" content="my page">
<title>添加商品数量</title>
</head>
<body>
<jsp:useBean id="add" class="mybean.Goods" scope="session" />
<jsp:setProperty name="add" property="*" />
<form action="pay.jsp" method="post">
<h3 align="center">商品一览表</h3>
<table border="1" width="600px" height="200px" cellspacing="0" cellpadding="10px" bgcolor="skyblue" bordercolor="#FFFFFF" align="center">
<tr align="center">
<td>商品类型</td>
<td>单价</td>
<td>添加入库/数量</td>
</tr>
<tr align="center">
<td>衣服</td>
<td>¥200</td>
<td><input required="required" type="text" name="Clothes" style="width:40px;"></td>
</tr>
<tr align="center">
<td>裤子</td>
<td>¥168</td>
<td><input required="required" type="text" name="Trouers" style="width:40px;"> </td>
</tr>
<tr align="center">
<td>鞋子</td>
<td>¥99</td>
<td><input required="required" type="text" name="Shoes" style="width:40px;"></td>
</tr>
<tr align="center">
<td>帽子</td>
<td>¥69</td>
<td><input required="required" type="text" name="Cap" style="width:40px;"></td>
</tr>
<tr align="center">
<td colspan="3" > <input type="submit" value="提交结算"/></td>
</tr>
</table>
</form>
</body>
</html>
pay.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ page import="mybean.Goods" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>提交结算</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3,keyword4">
<meta http-equiv="description" content="This is my page">
</head>
<body>
<jsp:useBean id="buy" class="mybean.Goods" scope="session" />
<jsp:setProperty name="buy" property="*" />
<%
String Count;
Count=request.getParameter("Clothes");
int C = Integer.parseInt(Count);
buy.setClothes(C);
//衣服数量
Count=request.getParameter("Trouers");
int T = Integer.parseInt(Count);
buy.setTrouers(T);
//裤子数量
Count=request.getParameter("Shoes");
int S = Integer.parseInt(Count);
buy.setShoes(S);
//鞋子数量
Count=request.getParameter("Cap");
int Cap = Integer.parseInt(Count);
buy.setCap(Cap);
//鞋子数量
%>
<h2 align="center">商品统计表</h2>
<table border="1" width="600px" height="200px" cellspacing="0" cellpadding="10px" bgcolor="skyblue" bordercolor="#FFFFFF" align="center">
<tr align="center">
<td>商品类型</td>
<td>单价/元</td>
<td>商品数量统计/件</td>
</tr>
<tr align="center">
<td>衣服</td>
<td>¥200</td>
<td><%=buy.Clothes()%></td>
</tr>
<tr align="center">
<td>裤子</td>
<td>¥168</td>
<td><%=buy.Trouers()%></td>
</tr>
<tr align="center">
<td>鞋子</td>
<td>¥99</td>
<td><%=buy.Shoes()%></td>
</tr>
<tr align="center">
<td>帽子</td>
<td>¥69</td>
<td><%=buy.Cap()%></td>
</tr>
<tr>
<th>金额总计/元</th>
<th colspan="2" align="center"> <%=buy.Sum() %>/元</th>
</tr>
</table>
</body>
</html>
javaBean(Goods.java)
package mybean;
public class Goods{
//衣服、鞋子、裤子、和帽子商品
private int Clothes=0;
private int Shoes=0;
private int Trouers=0;
private int Cap=0;
private String Notice;
private int sum;
public int getClothes() {
return Clothes;
}
public void setClothes(int Clothes) {
this.Clothes = Clothes;
}
//衣服
public int getShoes() {
return Shoes;
}
public void setShoes(int Shoes) {
this.Shoes= Shoes;
}
//鞋子
public int getTrouers() {
return Trouers;
}
public void setTrouers(int Trouers) {
this.Trouers = Trouers;
}
//帽子
public int getCap() {
return Cap;
}
public void setCap(int Cap) {
this.Cap = Cap;
}
public String Clothes(){
Notice="";
Notice+=getClothes();
return Notice;
}
public String Trouers(){
Notice="";
Notice+=getTrouers();
return Notice;
}
public String Shoes(){
Notice="";
Notice+=getShoes();
return Notice;
}
public String Cap(){
Notice="";
Notice+=getCap();
return Notice;
}
public int Sum(){
sum=0;
sum+=getClothes()*200;
sum+=getTrouers()*168;
sum+=getShoes()*99;
sum+=getCap()*69;
return sum;
}
}
效果图: