• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to create forms in a loop in Spring MVC

 
Honey Agarwal
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I have a list of data picked from mysql and I am presenting it in a loop in jsp using Spring MVC framework. Each loop has its own form (like in facebook posts).



Now the question is how to handle each form? For the page carrying above code, the controller is:



If there would be only one form, addFeed would be called. But how to manage here? Can anyone suggest me anything?
 
Nam Ha Minh
Ranch Hand
Posts: 502
Eclipse IDE Firefox Browser Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't understand why you need that? I think there is only a form submitted at a time so there is no need to check which form.
In case you have a good reason to use, I suggest you to use a hidden field in the form and in the controller you can check value of that field.
 
Honey Agarwal
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nam Ha Minh wrote:I don't understand why you need that? I think there is only a form submitted at a time so there is no need to check which form.
In case you have a good reason to use, I suggest you to use a hidden field in the form and in the controller you can check value of that field.


Thanks...that worked but I have to use new form for every loop for that to work otherwise I will get id or any hidden value of the first post.
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry, but that is not the correct way to display a list of Links or buttons where each is for an "id" that you want to pass. You definitely do not want a form tag for each row in that list.

Personally, I like just having them as links where the id is added to the URL either as a path variable or as a request parameter. Then I use CSS to make the link look like a button. The link would then do a get instead of a post.

If you still want buttons, I would still set the button's action to include the id of that row as a request parameter.

Then your controller method is mapped to one single method which takes in the @RequestParam("nameOfParamHere") or @PathVariable.

Mark
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic