Creating POST request from JSP without input fields for several different actions

Viewed 200

I have an object - Event. There is JSP card of Event. I want to have button on the card to delete event, so I have to create POST request to server for it, but I don't know how to do it.

I tried: pass to the View special boolean false modelAttribute "wasDeleted", change it when button is pressed via script and pass the model to the PostMapping method in Controller. There are the parts of code:

Passing special model attribute:

    @GetMapping("/event{Id}")
    public String getCurrentEvent(@PathVariable String Id, Model model) {
        //...
        if (user.getEvents().contains(event)) {
            model.addAttribute("wasDeleted", false);
            return "myEvent";
        }
        //...


JSP view of event card:

<%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<c:set var="contextPath" value="${pageContext.request.contextPath}"/>

<!DOCTYPE html>
<html lang="en">
  <head>
      <meta charset="utf-8">
      <title>Event card</title>

      <link href="${contextPath}/resources/css/bootstrap.min.css" rel="stylesheet">
      <link href="${contextPath}/resources/css/common.css" rel="stylesheet">
  </head>

  <body>

<form:form method="POST" modelAttribute="wasDeleted" class="form-signin">

          <script>
              function delete(){
                  wasDeleted = true;
                     }
          </script>

          <button class="btn btn-lg btn-primary btn-block" type="submit" onClick='delete()'>Delete event</button>
      </form:form>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
    <script src="${contextPath}/resources/js/bootstrap.min.js"></script>
  </body>
</html>

Deleting method in controller's method:

    @PostMapping("/event{Id}")
    public String changeEvent(@PathVariable String Id, Model model) { //I don't use @ModelAttribute here because Model can have another attributes (the set of attributes may be different depends of event)
        //...

        if (user.getEvents().contains(event)) {
            boolean wasDeleted = (boolean)model.getAttribute("wasDeleted");
            if (wasDeleted) {
                user.getEvents().remove(event);
                eventRepository.delete(event);
            }
            //...
        }
    //...

The error: model is empty and doesn't contain attribute "wasDeleted"

Help, please, why is model empty? How can I change model attribute in JSP and pass it to controller? Maybe there are other ways to pass to the server the information that deleting button was pressed?

Deleting - is an example of actions with event. There can be another action buttons on the card, so it would be great to have general solution of problem that lets to have different controller reaction for different buttons

0 Answers
Related