Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to delete a record using Spring MVC and annotations
    primarykey
    data
    text
    <p>I am relatively new to Spring and I am trying to accomplish the following:</p> <p>Let's say I have a list of fruits in a database table with 2 columns "id" and "name". I would like to have a page displaying the list and have a "delete" button next to each fruit. I thought that would be easy to accomplish but I can't figure it out and can't find any tutorial explaining this.</p> <p>This is what I have for now:</p> <p>View:</p> <pre><code>&lt;c:forEach items="${fruits}" var="fruit"&gt; &lt;tr&gt; &lt;td&gt;&lt;c:out value="${fruit.name}" /&gt;&lt;/td&gt; &lt;sec:authorize access="hasRole('admin')"&gt; &lt;td&gt; &lt;a href="&lt;c:url var="deleteUrl" value="/fruit/remove.html"/&gt;"&gt;DELETE&lt;/a&gt; &lt;form id="${fruitFormId}" method="POST"&gt; &lt;input id="fruit" name="fruit" type="hidden" value="${fruit.id}"/&gt; &lt;/form&gt; &lt;/td&gt; &lt;/sec:authorize&gt; &lt;/tr&gt; &lt;/c:forEach&gt; </code></pre> <p>Then the controller:</p> <pre><code>@Controller public class FruitController{ @Autowired private FruitManager fruitManager; @RequestMapping(value = "/list", method = RequestMethod.GET) public String getFruits(ModelMap model) { model.addAttribute("fruits", this.fruitManager.getFruits()); return "list"; } @RequestMapping(value = "/fruit/remove", method = RequestMethod.POST) public String removeAd(Fruit fruit) { fruitManager.removeFruit(fruit); return "/list"; } </code></pre> <p>I am not even sure I am using the "good and clean" way right now and would like to accomplish this "the right way", following conventions if any.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload