我想弄清楚这段代码的问题是什么。当我填写表格并按下保存按钮时,页面会像刷新一样闪烁,没有错误和数据插入。
我有一个扩展该表类的工作表。我使用了多对一的关系,所以我不知道错误到底在哪里。
jsp文件输入
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@page import="eshop.model.Category"%>
<%@page import="java.util.List"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<jsp:include page="header.jsp" />
<body>
${msg}
<div>
<div style="float:left;width:200px">
<jsp:include page="menu.jsp" />
</div>
<div style="float:left">
<form action="ProductController" method="post">
<input type="hidden" name="action" value="add"/>
<table>
<tr>
<td>Product ID</td>
<td>
<input type="text" name="pid"/>
</td>
</tr>
<tr>
<td>Product Name</td>
<td><input type="text" name="pname" /></td>
</tr>
<tr>
<td>Category Id</td>
<td>
<%
List<Category> aList = (List) request.getAttribute("clist");
%>
<select name="cat">
<%
for(Category c : aList){
%>
<option value="<%out.println(c.getCatId());%>">
<%out.println(c.getCatName() ); %>
</option>
<% } %>
</select>
</td>
</tr>
<tr>
<td>Supplier ID</td>
<td><input type="text" name="sid" /></td>
</tr>
<tr>
<td>Unit Price</td>
<td><input type="text" name="up" /></td></tr>
<tr>
<td>Quantity</td>
<td><input type="text" name="qua"/></td>
</tr>
<tr>
<td>Units in Stock</td>
<td> <input type="text" name="uis"/></td>
</tr>
<tr>
<td>Units on Order</td>
<td>
<input type="text" name="uoo"/></td>
</tr>
<tr><td>Reorder</td>
<td>
<input type="text" name="rol"/></td></tr>
<tr>
<td><input type="reset" value="Clear" /></td>
<td><input type="submit" value="save" /></td>
</tr>
</table>
</form>
</div>
</div>
</body>
</html>
servlet代码完成
@WebServlet("/ProductController")
public class ProductController extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public ProductController() {
super();
// TODO Auto-generated constructor stub
}
protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException ,IOException {
String action = request.getParameter("action");
ProductServices cs = new ProductServices();
if(action.equalsIgnoreCase("add")){
CategoryServices c=new CategoryServices();
List<Category> aList = c.getAllCategories();
request.setAttribute("clist", aList);
request.getRequestDispatcher("products.jsp").forward(request, response);
}else if(action.equalsIgnoreCase("save")){
Products p = new Products();
Category ct=new Category();
p.setPid(Integer.parseInt(request.getParameter("pid")));
p.setProductName(request.getParameter("pname"));
int cat=Integer.parseInt(request.getParameter("cat"));
ct.setCatid(cat);
p.setCatid(ct);
p.setSupplierId(Integer.parseInt(request.getParameter("sid")));
p.setUnitPrice(Integer.parseInt(request.getParameter("up")));
p.setQuntitypunit(request.getParameter("qua"));
p.setUnitsInStock(Integer.parseInt(request.getParameter("uis")));
p.setUnitsOnOrder(Integer.parseInt(request.getParameter("uoo")));
p.setReorderLevel(Integer.parseInt(request.getParameter("rol")));
cs.addProduct(p);
request.setAttribute("msg", "Record Saved Successfully");
request.getRequestDispatcher("products.jsp").forward(request, response);
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
processRequest(request,response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
processRequest(request,response);
}
这是课程
@Entity
@Table (name="products")
public class Products implements Serializable{
@Id
@Column(name="product_id")
private int Pid;
@Column(name="product_name")
private String productName;
@Column(name="supplierid")
private int supplierId;
@ManyToOne
@JoinColumn(name="categoryid")
private Category catid;
@Column(name="quantity_per_unit")
private String quntitypunit;
@Column(name="unit_price")
private float unitPrice;
@Column(name="unitinstock")
private int unitsInStock;
@Column(name="unitonorder")
private int unitsOnOrder;
@Column(name="reorderlevel")
private int reorderLevel;
public String getProductName() {
return productName;
}
public void setProductName(String productName) {
this.productName = productName;
}
public int getSupplierId() {
return supplierId;
}
public void setSupplierId(int supplierId) {
this.supplierId = supplierId;
}
public Category getCatid() {
return catid;
}
public void setCatid(Category catid) {
this.catid = catid;
}
public String getQuntitypunit() {
return quntitypunit;
}
public void setQuntitypunit(String quntitypunit) {
this.quntitypunit = quntitypunit;
}
public float getUnitPrice() {
return unitPrice;
}
public void setUnitPrice(float unitPrice) {
this.unitPrice = unitPrice;
}
public int getUnitsInStock() {
return unitsInStock;
}
public void setUnitsInStock(int unitsInStock) {
this.unitsInStock = unitsInStock;
}
public int getUnitsOnOrder() {
return unitsOnOrder;
}
public void setUnitsOnOrder(int unitsOnOrder) {
this.unitsOnOrder = unitsOnOrder;
}
public int getReorderLevel() {
return reorderLevel;
}
public void setReorderLevel(int reorderLevel) {
this.reorderLevel = reorderLevel;
}
public int getPid() {
return Pid;
}
public void setPid(int pid) {
Pid = pid;
}
}
这是服务代码,我将其扩展到适用于其他表的基础
public class ProductServices extends Base {
public void addProduct(Products pro){
save(pro);
}
答案 0 :(得分:-1)
您应该使用响应设置响应对象,而不是请求对象。
在ProductController类
中查看ProcessRequest()的最后几行除非这是转发到另一个请求,否则可能是(但我在这里没有看到该代码?)