• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Knute Snortum
  • Bear Bibeault
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Tim Holloway

How to post multiple checkboxes value in Spring Boot web app?

 
Ranch Hand
Posts: 523
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi expert,

I am stuck with this checkboxes multiple values which I need to post it together with another entity.

Most of the tutorials I found are using List<String> but my 2nd entity is using List<Subject>, I wonder if it is not possible to use List<Subject> at all ?

Here's one such tutorial I have found :

https://examples.javacodegeeks.com/enterprise-java/spring/mvc/spring-mvc-checkbox-and-checkboxes-example/

Hope someone can tell me how do I do it using List<Subject>.

Tks.
 
Master Rancher
Posts: 451
6
IntelliJ IDE Spring Fedora
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you tried making the checkboxes have the same name?  
 
tangara goh
Ranch Hand
Posts: 523
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Al Hobbs wrote:Have you tried making the checkboxes have the same name?  



What do you mean by the above ?

Anyway, my problem is that I am using List<Object> so I think I may need a converter as per this stackover flow suggestion :-

https://stackoverflow.com/questions/6989711/spring-mvc-pre-populate-checkboxes/6996350#6996350

I wish there is an easier way to do things.

Notice that alot of users are using a wrapper class for their backing object like this...

Not sure if this is the only way to make things work.

Hope to hear some replies.

Tks.
 
Al Hobbs
Master Rancher
Posts: 451
6
IntelliJ IDE Spring Fedora
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So instead of having commandname  blah blah you can just use a regular html form and have each input tag have the same name="" and spring should load it automatically as a list
 
tangara goh
Ranch Hand
Posts: 523
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Al Hobbs wrote:So instead of having commandname  blah blah you can just use a regular html form and have each input tag have the same name="" and spring should load it automatically as a list



I think CommandName is old way of doing things.  Now, people are using ModelAndView Attributes, right ?

Could you show me an example using the same name = "" combined with Controller how to  ?
 
Al Hobbs
Master Rancher
Posts: 451
6
IntelliJ IDE Spring Fedora
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hmm most tutorials online use the model attribute.  So I'll just write a basic one.
<Input name="nameyouwanttouse"........
<Input name="nameyouwanttouse"........



Controller method:
....mapping...
Public blah methionine(List<type> nameyouwanttouse){}
 
tangara goh
Ranch Hand
Posts: 523
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Al Hobbs wrote:Hmm most tutorials online use the model attribute.  So I'll just write a basic one.
<Input name="nameyouwanttouse"........
<Input name="nameyouwanttouse"........



Controller method:
....mapping...
Public blah methionine(List<type> nameyouwanttouse){}



Hi,

The thing is that if I used your method, it is going to be very long and chunky.

What I am looking for is something like what this gal did in this tutorial :

https://examples.javacodegeeks.com/enterprise-java/spring/mvc/spring-mvc-checkbox-and-checkboxes-example/

However, she is using List<String> and I am using List<Object> so my question is how to do


List<String> courses = new ArrayList<String>();
       courses.add("Yoga");
       courses.add("Stretching");
       courses.add("Pilates");
       courses.add("Aerobic");
       courses.add("Oriental");



and then in the form she just use one line :


<td>Choose the courses you like:</td>
               <td><form:checkboxes path="courses" items="${courses}" />

 
Al Hobbs
Master Rancher
Posts: 451
6
IntelliJ IDE Spring Fedora
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's actually not clunky. You don't need to send an object with the jsp for the model attribute Or use spring form tag Or setup up command name.   For what you asked you can use just a basic for each tag using the collection from the par
 
tangara goh
Ranch Hand
Posts: 523
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Al Hobbs wrote:It's actually not clunky. You don't need to send an object with the jsp for the model attribute Or use spring form tag Or setup up command name.   For what you asked you can use just a basic for each tag using the collection from the par



<qutoe>
<input type="checkbox" name="language" value="English">English</input><br />
<input type="checkbox" name="language" value="English">English</input><br />
<input type="checkbox" name="language" value="English">English</input><br />
<input type="checkbox" name="language" value="English">English</input><br />
<input type="checkbox" name="language" value="English">English</input><br />
<input type="checkbox" name="language" value="English">English</input><br />
<input type="checkbox" name="language" value="English">English</input><br />
<input type="checkbox" name="language" value="English">English</input><br />
<input type="checkbox" name="language" value="English">English</input><br />
<input type="checkbox" name="language" value="English">English</input><br />
<input type="checkbox" name="language" value="English">English</input><br />
<input type="checkbox" name="language" value="English">English</input><br />
<input type="checkbox" name="language" value="English">English</input><br />
<input type="checkbox" name="language" value="English">English</input><br />
<input type="checkbox" name="language" value="English">English</input><br />
<input type="checkbox" name="language" value="English">English</input><br />
<input type="checkbox" name="language" value="English">English</input><br />
<input type="checkbox" name="language" value="English">English</input><br />
<input type="checkbox" name="language" value="English">English</input><br />
<input type="checkbox" name="language" value="English">English</input><br />
<input type="checkbox" name="language" value="English">English</input><br />
<input type="checkbox" name="language" value="English">English</input><br />
<input type="checkbox" name="language" value="English">English</input><br /><input type="checkbox" name="language" value="English">English</input><br /><input type="checkbox" name="language" value="English">English</input><br />
<input type="checkbox" name="language" value="English">English</input><br />vv
<input type="checkbox" name="language" value="English">English</input><br /><input type="checkbox" name="language" value="English">English</input><br /><input type="checkbox" name="language" value="English">English</input><br />v<input type="checkbox" name="language" value="English">English</input><br />
<input type="checkbox" name="language" value="English">English</input><br /><input type="checkbox" name="language" value="English">English</input><br /><input type="checkbox" name="language" value="English">English</input><br />
v
<input type="checkbox" name="language" value="English">English</input><br />
<input type="checkbox" name="language" value="English">English</input><br />
v
<input type="checkbox" name="language" value="English">English</input><br />
v
v
v
<input type="checkbox" name="language" value="English">English</input><br />v<input type="checkbox" name="language" value="English">English</input><br /></quote>

Chunky.  I hope you can get my point.

There got to be an easier way to do things.

And I think a Converter is needed for my caes.

I hope someone can correct me if I am wrong.

Tks!
 
Al Hobbs
Master Rancher
Posts: 451
6
IntelliJ IDE Spring Fedora
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can use jsp tags for that so it wouldn't look like that.  I literally told you can use a for each tag for your example.
 
tangara goh
Ranch Hand
Posts: 523
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Al Hobbs wrote:You can use jsp tags for that so it wouldn't look like that.  I literally told you can use a for each tag for your example.



I do not think so but I may be wrong.

Could you kindly show me what you meant by that ?

Cos how will you make the form render all the checkboxes for user to tick, without explicitly telling Spring what are the subjects ?

How do you bind a method to the checkboxes with all the subjects names etc. ?
 
Al Hobbs
Master Rancher
Posts: 451
6
IntelliJ IDE Spring Fedora
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You could do it manually with a foreach tag, but you could probably use the form:checkboxes like that girl did.  I haven't used that tag before, but I am assuming it probably works like that.  The foreach would have maybe one more line telling it to iterate through the list.  

That brings me to the question of what are you trying to do specifically, because the example you gave seems like it works.
 
Al Hobbs
Master Rancher
Posts: 451
6
IntelliJ IDE Spring Fedora
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You post multiple objects as a list to a controller, right?  So instead of getting a list of strings you want to get a list of more complex objects?

If that's what you're talking about then I haven't done that but, maybe it would work if you just have the properties multiple times and then spring might handle it.  I kinda want to know now so I think i'll check it out for myself.
 
tangara goh
Ranch Hand
Posts: 523
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Al Hobbs wrote:You post multiple objects as a list to a controller, right?  So instead of getting a list of strings you want to get a list of more complex objects?

If that's what you're talking about then I haven't done that but, maybe it would work if you just have the properties multiple times and then spring might handle it.  I kinda want to know now so I think i'll check it out for myself.



Hi Al Hobbs,

It seems that everyone is using List<String> so I may change my entities....if it will help.

Do let me know if you manage to find out how to use the object instead...

I am quite confused really.  So, why Java come up with something so troublesome as in Type Generic ?  I thought Generic Type is the way to go ?

 
Al Hobbs
Master Rancher
Posts: 451
6
IntelliJ IDE Spring Fedora
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok I looked into it.  Since you are trying to get a list of a complex object , you could send a list to the JSP and then have the form fill it out using indexes.  Or you could probably use javascript to post the data as a json to an endpoint.
 
Al Hobbs
Master Rancher
Posts: 451
6
IntelliJ IDE Spring Fedora
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's a link that shows how to do it using modelattribute:
https://stackoverflow.com/questions/15480397/how-to-send-list-of-objects-to-view-and-back-to-post-method-in-controller
 
Ranch Hand
Posts: 214
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not a big problem.  
You will definitely give names to your checkboexes at JSP side:
and .


Similarly receive them at controller method respectively:


I hope it is works.
 
tangara goh
Ranch Hand
Posts: 523
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Arun Singh Raaj wrote:Not a big problem.  
You will definitely give names to your checkboexes at JSP side:
and .


Similarly receive them at controller method respectively:


I hope it is works.



Hi both,

Still, I can't escape from repeating 40 over lines of input type="checkbox".

I need a clean method to tie the values of the checkbox which users tick and then bind them together with first entity into the form

How do I make that happen ?
 
Arun Singh Raaj
Ranch Hand
Posts: 214
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
in jsp:
 
Al Hobbs
Master Rancher
Posts: 451
6
IntelliJ IDE Spring Fedora
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh no..  Did you just tell somebody to use scriptures.....   You should use tags.
 
Al Hobbs
Master Rancher
Posts: 451
6
IntelliJ IDE Spring Fedora
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Scriptlets
 
Arun Singh Raaj
Ranch Hand
Posts: 214
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually he needs the code, not the advice. You could have written, given a reference link or copy-pasted two lines of code in 5 days to help him.
I dont have experience of front-end programming.

By the way this is how you can display checkboxes using forEach of JSTL:
 
Al Hobbs
Master Rancher
Posts: 451
6
IntelliJ IDE Spring Fedora
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I gave him a link with code showing exactly how to solve his problem.
 
tangara goh
Ranch Hand
Posts: 523
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Arun Singh Raaj wrote:Actually he needs the code, not the advice. You could have written, given a reference link or copy-pasted two lines of code in 5 days to help him.
I dont have experience of front-end programming.

By the way this is how you can display checkboxes using forEach of JSTL:



I am looking into doing something like this below:
<code>
<div class="checkbox_list">
<c:forEach var="i" varStatus="statusSubject" items="${Tutor.subjectListWrapper }">
<input type="checkbox" path="subjectListWrapper" name="subjectListWrapper" value="${i.subjectName }">

</c:forEach>
</code>

My problem is to understand how the SpringController is able to take in the values that are being ticked ?

Also, I am not looking at nested object because i think it won't help me create the join table .........

Kindly correct me if I am wrong....

I am curious if you don't do the front-end, how do you make the user enter the data into your database ?
 
Arun Singh Raaj
Ranch Hand
Posts: 214
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As the JSP code you provided, my answer is still same:




As i understood your code, all the checkbox values are of "Tutor" type, so all can be saved into tutor object at controller.
don't know why you aren't getting it.
Firstly you should apply this method then see what next.
 
Arun Singh Raaj
Ranch Hand
Posts: 214
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

tangara goh wrote:Hi expert,
I am stuck with this checkboxes multiple values which I need to post it together with another entity.
Most of the tutorials I found are using List<String> but my 2nd entity is using List<Subject>, I wonder if it is not possible to use List<Subject> at all ?
Here's one such tutorial I have found :
https://examples.javacodegeeks.com/enterprise-java/spring/mvc/spring-mvc-checkbox-and-checkboxes-example/
Hope someone can tell me how do I do it using List<Subject>.
Tks.



Hi, tangara goh.
I would like to start over.

Your question is actually about "association mapping." I assume it is many-to-many relationship between Student and Subject.
If i got it right then you don't have to pass the list of subjects as a separate object. If you have a registration form for a  student where he has to choose multiple subjects for himself then the subjects will be mapped with the student form itself, it happens by @ModelAttribute("student") at Controller class.

Your Student class could be like:

So my point is, when you hit submit button after filling up the details in jsp page, the data is passed to the Controller as Student object and it is accepted by
So basically you can view all the passed subjects at Controller class by:


you can understand this concept more after trying this Example
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!