我想添加"编辑"功能。单击“编辑”时,它会添加重复记录而不是编辑

时间:2014-05-17 11:24:55

标签: java

这是我的Operations.java,products.Java类和Products.jsp文件:

我在其中实现了添加和编辑功能。添加按钮工作正常但编辑按钮正在添加重复记录而不是编辑记录。请告诉我如何在程序中实现编辑功能?

package metier;

import java.util.ArrayList;

public class Operation {

private ArrayList<Produit> produits = new ArrayList<Produit>();

public ArrayList<Produit> getProduits() {
    return produits;
}

public void setProduits(ArrayList<Produit> produits) {
    this.produits = produits;
}

public void add(Produit p){
    produits.add(p);
}

public void remove(Long id){
    for(Produit p:produits){
        if(p.getId()==id){ //equals
            produits.remove(p);
            break;
        }
    }
}

public void edit(Long id){
    for(Produit p:produits){
        if(p.getId()==id){
            p.getId();
            p.getName();
            p.getContactno();
            p.getSPS();
            produits.add(p);

            break;
        }
    }
}

public ArrayList getAll(){
    return produits;
}

}

package metier;

public class Produit {

private Long id;
private String name, address, contactNo, SparePartsService;

public Long getId() {
    return id;
}
public void setId(Long id) {
    this.id = id;
}
public String getName() {
    return name;
}
public void setName(String name) {
    this.name = name;
}
public String getAddress() {
    return address;
}
public void setAddress(String address) {
    this.address = address;
}
public String getContactno() {
    return contactNo;
}
public void setContactno(String contactNo) {
    this.contactNo = contactNo;
}
public String getSPS() {
    return SparePartsService;
}
public void setSPS(String SparePartsService) {
    this.SparePartsService = SparePartsService;
}
public Produit() {
    super();
    // TODO Auto-generated constructor stub
}
public Produit(String name, String address, String contactNo, String SparePartsService) {
    super();
    this.name = name;
    this.address = address;
    this.contactNo = contactNo;
    this.SparePartsService = SparePartsService;
}
public Produit(Long id, String name, String address, String contactNo, String SparePartsService) {
    super();
    this.id = id;
    this.name = name;
    this.address = address;
    this.contactNo = contactNo;
    this.SparePartsService = SparePartsService;
}
@Override
public String toString() {
    return id + " - " + name + " - " + address + " - " + contactNo + " - " + SparePartsService + " .";
}

public void Show(){
    System.out.println(toString());
}

}

<%@ page import="web.ProduitBeans"%>
<%@ page import="metier.Produit"%>
<%@ page import="java.util.Iterator"%>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!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>Getiteasy.Net</title>
</head>
<body>
<%
    ProduitBeans produits;
if(request.getAttribute("modele") != null){
    produits = (ProduitBeans) request.getAttribute("modele");
}else{
    produits = new ProduitBeans();
}
%>
<h3>Tutorial MVC(Model, View, Controller)</h3>
<h5>Ajouter un nouveau produit</h5>

<form action="prodserv" method="post">
    <table border="1" width="45%">
        <tr>
            <td>Name</td>
            <td><input type="text" name="name"></td>
        </tr>
        <tr>
            <td>Address</td>
            <td><input type="text" name="address"></td>
        </tr>
        <tr>
            <td>Contact No.</td>
            <td><input type="text" name="contactNo"></td>
        </tr>
        <tr>
            <td>Spare Parts Service(Yes/No)</td>
            <td><input type="text" name="SparePartsService"></td>
        </tr>
        <tr>
            <td colspan="2"><input type="submit" value="Valider"></td>
        </tr>
    </table>
</form>
<table border="1" width="60%">
    <tr>
        <th>ID</th>
        <th>Name</th>
        <th>Address</th>
        <th>Contact No.</th>
        <th>Spare Parts Service</th>
        <th>Option</th>
    </tr>
    <%
        Iterator<Produit> list = produits.getListe().iterator();
        while(list.hasNext()){
            Produit p = list.next();
    %>
    <tr>
        <td><%=p.getId() %></td>
        <td><%=p.getName() %></td>
        <td><%=p.getAddress() %></td>
        <td><%=p.getContactno() %></td>
        <td><%=p.getSPS() %></td>
        <td>
            <form action="prodserv" method="post">
                <input type="hidden" name="id" value="<%=p.getId() %>" >
                <input type="hidden" name="action" value="supprimer" >
                <input type="submit" value="supprimer"/>    
            </form>
                <form action="prodserv" method="post">
                <input type="hidden" name="id" value="<%=p.getId() %>" >
                <input type="hidden" name="editaction" value="Edit" >
                <input type="submit" value="Edit"/>     
            </form>
        </td>
    </tr>
    <%
        }
    %>
</table>

2 个答案:

答案 0 :(得分:1)

您希望编辑方法做什么?看看你的编辑方法。如果您发现具有ID的产品,您只是调用getter方法,而您对数据一无所知。之后,您只需将同一对象再次添加到列表中。您需要定义要编辑的数据,然后使用setter方法覆盖数据。并且不要再将对象添加到列表中。

答案 1 :(得分:0)

为此目的使用ArrayList是非常无效的,您将多次循环列表,随着列表的增长,这将花费更长时间。最好使用Map,它将一个键“映射”到一个值,这样你就可以通过预定义的键直接调用该值,而不必循环整个列表。

在您的Operation类中:

  • ArrayList<Produit> produits ...更改为Map<Long,Produit> = new HashMap<>() (我还建议你让它静止,以确保你保持相同的清单)
  • 添加private static Long idCounter=0L

然后当您添加新的Produit时,请执行以下操作:

public void add(Produit p){
    idCounter++;
    p.setId(idCounter);
    produits.put(idCounter,p);
}

删除

public void remove(Produit p){
    produits.remove(p.getId());
    }

注意:的 这里没有idcounter--;,因为你不知道它是否是你地图中的最后一个,所以如果你删除了很多元素,你将会跳过一些id。 您要删除的Produit的id必须在p参数上设置。

修改

public void edit(Produit p){
   produits.put(p.getId(),p);
}

注意:的 必须在p参数上设置要删除的Produit的ID。

<强> GETALL

public Collection<Produit> getAll(){
    produits.values();
}

在您的JSP页面中:

你必须以某种方式告诉你的控制器它必须采取什么行动。您可以使用Action参数执行此操作。 你已经为一个案例做了这个(删除(= supprimer FR))。

<input type="hidden" name="action" value="supprimer" >

要编辑,您必须再次执行此操作,因此请更改:

<input type="hidden" name="editaction" value="Edit" >

<input type="hidden" name="action" value="Edit" >

注意。“name”-tag是参数的名称,例如number是变量Int number的名称。

在您的Controller(servlet)中:

您:

protected void doPost(HttpServletRequest req, HttpServletResponse resp)             throws ServletException, IOException {
    if (req.getParameter("action") != null) {
        op.remove(Long.parseLong(req.getParameter("id")));
    } 
    else {          // Recuprer les information
        String name = req.getParameter("name");
        String address = req.getParameter("address");
        String contactNo = req.getParameter("contactNo");
        String SparePartsService = req.getParameter("SparePartsService");
        op.add(new Produit(1L, name, address, contactNo,SparePartsService));
    }

在“else” - 你的if-satement中,检查action-parameter是否为null,你只能添加一个Produit。

您必须添加:

 if(req.getParameter("action").equals("Add")){
                    String name = req.getParameter("name");
            String address = req.getParameter("address");
            String contactNo = req.getParameter("contactNo");
            String SparePartsService = req.getParameter("SparePartsService");
            op.add(new Produit(1L, name, address, contactNo,SparePartsService));
}else if (req.getParameter("action").equals("Edit")){
                    String name = req.getParameter("name");
            String address = req.getParameter("address");
            String contactNo = req.getParameter("contactNo");
            String SparePartsService = req.getParameter("SparePartsService");
                    Long id = Long.ParseLong(req.getParameter("id"));
            op.edit(new Produit(id, name, address, contactNo,SparePartsService));
}

<强>结果:

protected void doPost(HttpServletRequest req, HttpServletResponse resp)             throws ServletException, IOException {
    if (req.getParameter("action") != null) {
        op.remove(Long.parseLong(req.getParameter("id")));
    } 
 // Recuprer les information
    else if(req.getParameter("action").equals("Add")){
            String name = req.getParameter("name");
            String address = req.getParameter("address");
            String contactNo = req.getParameter("contactNo");
            String SparePartsService = req.getParameter("SparePartsService");

            op.add(new Produit(1L, name, address, contactNo,SparePartsService));

    }else if (req.getParameter("action").equals("Edit")){
            String name = req.getParameter("name");
            String address = req.getParameter("address");
            String contactNo = req.getParameter("contactNo");
            String SparePartsService = req.getParameter("SparePartsService");
            Long id = Long.ParseLong(req.getParameter("id"));

            op.edit(new Produit(id, name, address, contactNo,SparePartsService));
    }
}

注意。您必须为要实现的每个可能的操作参数值添加新的“else if”-clause。

我建议你将这些if子句中的功能放在一个单独的私有函数中,否则你的doPost(...)函数会变得很长。 或者甚至更好地看看Command-patern。