0

I am using SimpleFormController with a result page looking like this:

<tr>
 <td>Name: </td>
 <td>${product.name}</td>
</tr>
<tr>
 <td>Text: </td>
 <td>${product.text}</td>
</tr>

A user can enter a name and some text. I'm trying to implement a delete functionality for each entry (there should be a link next to each entry). I'm having trouble with understanding, if it can be done in the same Controller as for the input or not (am new to Spring) and how. The onSubmit method helps to display data that was added, do I need to implement an extra delete method? If yes, how can I "map" it to my delete link in my jsp?

1 Answers1

1

I suppose you are not wanting to put a delete link even when the user is just entering the name!

Delete links should normally appear when you are displaying data, not creating them.

Here is how you can create a delete link according to associated ids.

<tr>
 <td>Name: </td>
 <td>${product.name}</td>
 <td><a href="delete/${product.id}">delete</a></td>
</tr>

and this should be in your controller:

@Controller
public class ProductController{
     @RequestMapping("/delete/{id}")
     public String deleteProduct(@PathVariable("id")Integer id) {
            // get product by id
            // delete that product
            // save database
            // or do as you wish
return "redirect:/index";
    }
}

Hope that helps :)

QuestionEverything
  • 4,809
  • 7
  • 42
  • 61
  • I am not using a database, just storing all entries in a list for now. So it is enough if I insert a link with the same name as the method name? Why is the return type void? Should it not return a new ModelandView Object ? –  Jun 08 '13 at 11:53
  • Well, the answer is just a brief idea about what you can do. However, I'm extending my answer as per your inquiry. Happy springing.. :) – QuestionEverything Jun 08 '13 at 12:05
  • Is it possible without annotations as well :(? Can I do it inside my class that extends the SimpleFormController? –  Jun 08 '13 at 12:05
  • Frankly Mysia, I started learning spring when the days of boring xml configuration was at an end. Annotations are on the rise now-a-days. So, briefly, I don't have much idea about on xml configs. However, you shouldn't hesitate to use annotations. You would find that it's easily configurable, requires less effort and the thing of modern web programming paradigms. :) – QuestionEverything Jun 08 '13 at 12:13
  • Ok. But that also means that I cannot use SimpleFormController anymore right? –  Jun 08 '13 at 12:17
  • That's right. check this link [link]http://stackoverflow.com/questions/4734259/spring-simpleformcontroller-in-3-0[/link] – QuestionEverything Jun 08 '13 at 12:19
  • Can you explain the "redirect:/index" in the return statement? –  Jun 08 '13 at 12:25
  • it's very well explained here: http://static.springsource.org/spring/docs/current/spring-framework-reference/html/mvc.html#mvc-redirecting-redirect-prefix – QuestionEverything Jun 08 '13 at 12:29
  • I've tried this solution now, but I'm getting a 404 error when clicking the link :(. Any idea why? –  Jun 08 '13 at 19:05