0

I have problem with CRUD edit operation. When i click Edit which is written in this way in jsp file

 <a href="editMedicines?id=${medicines.id}">Edit</a>

i got error

HTTP Status 404 – Not Found
Type Status Report
Message /Firstaidkit/editMedicines
Description The origin server did not find a current representation for the target resource or is not willing to disclose that one exists.

EditController

@WebServlet(value = "/editMedicines")
    public class MedicinesEditController extends HttpServlet {
        private static final long serialVersionUID = 1L;

    @RequestMapping(method = RequestMethod.GET)
    public ModelAndView editMedicines(HttpServletRequest request) {
        int medicinesId = Integer.parseInt(request.getParameter("id"));
        Medicines medicines = GenericDAO.get(medicinesId);
        ModelAndView model = new ModelAndView("editform");
        model.addObject("medicines", medicines);
        return model;
    }
}

GenericDAO

public interface GenericDAO <T, PK extends Serializable> {
    //CRUD
    T create(T newObject);
    T read(PK primaryKey);
    public void update(Medicines medicines);
    public void delete(T id);
    List<T> getAll();
    public static Medicines get(int medicinesId) {
        return null;
    }
}

MedicinesDAOImpl

 private final static String UPDATE_MEDICINES = 
          "UPDATE medicines SET name=:name, drugform=:drugform, quantity=:quantity, expiration_date=:expiration_date, description=:description WHERE id_medicines=:id_medicines;";

        @Override
        public void update(Medicines medicines) {
                jdbcTemplate.update(UPDATE_MEDICINES, medicines.getName(), medicines.getDrugForm(),
                        medicines.getQuantity(), medicines.getExpirationDate(), medicines.getId());
            } 
        }

editform.jsp

<form class="form-signin" method="post" action="editMedicines">
Pabblo96
  • 29
  • 6

1 Answers1

0

I believe the problem is that the URL your anchor is linked to does not exist. You should have an annotation dictating the path on your MedicinesEditController at a class level. Assuming you want the path of this endpoint to be /Firstaidkit/editMedicines, the following should work:

@Path(value = "/Firstaidkit") // possibly a different but similar annotation
public class MedicinesEditController extends HttpServlet {

    @RequestMapping(value = "/editMedicines", method = RequestMethod.GET)
    public ModelAndView editMedicines(HttpServletRequest request) {
        int medicinesId = Integer.parseInt(request.getParameter("id"));
        Medicines medicines = GenericDAO.get(medicinesId);
        ModelAndView model = new ModelAndView("editform");
        model.addObject("medicines", medicines);
        return model;
    }

}

Otherwise, you need to alter the link of the anchor to reference the root of the application, followed by your endpoint of /editMedicines. This can be accomplished by using the following anchor, as described here:

<a href="/editMedicines?id=${medicines.id}">Edit</a>

Edit: Try the following

MedicinesEditController

@RequestMapping(value = "/editMedicines")
    public class MedicinesEditController extends HttpServlet {
        private static final long serialVersionUID = 1L;

    @GetMapping
    public ModelAndView editMedicines(HttpServletRequest request) {
        int medicinesId = Integer.parseInt(request.getParameter("id"));
        Medicines medicines = GenericDAO.get(medicinesId);
        ModelAndView model = new ModelAndView("editform");
        model.addObject("medicines", medicines);
        return model;
    }
}

editform.jsp

<form class="form-signin" method="GET" action="editMedicines">

anchor

(note: try these variations, as the link changes depending on where you are when you click it. View this stack to help determine the correct link)

 <a href="editMedicines?id=${medicines.id}">Edit</a>
 <a href="/editMedicines?id=${medicines.id}">Edit</a>
 <a href="../editMedicines?id=${medicines.id}">Edit</a>
darkmnemic
  • 272
  • 3
  • 13