Skip to content

Instantly share code, notes, and snippets.

@darbyluv2code
Created October 3, 2016 14:32
Show Gist options
  • Save darbyluv2code/debb69b1bf8010d84d50e0542e809ffb to your computer and use it in GitHub Desktop.
Save darbyluv2code/debb69b1bf8010d84d50e0542e809ffb to your computer and use it in GitHub Desktop.
FAQ: How to populate radiobuttons with items from Java class like we did with selectlist?
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html>
<html>
<head>
<title>Student Confirmation</title>
</head>
<body>
The student is confirmed: ${student.firstName} ${student.lastName}
<br><br>
Favorite Language: ${student.favoriteLanguage}
</body>
</html>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<!DOCTYPE html>
<html>
<head>
<title>Student Registration Form</title>
</head>
<body>
<form:form action="processForm" modelAttribute="student">
First name: <form:input path="firstName" />
<br><br>
Last name: <form:input path="lastName" />
<br><br>
Favorite Language:
<form:radiobuttons path="favoriteLanguage" items="${student.favoriteLanguageOptions}" />
<br><br>
<input type="submit" value="Submit" />
</form:form>
</body>
</html>
package com.luv2code.springdemo.mvc;
import java.util.LinkedHashMap;
public class Student {
private String firstName;
private String lastName;
private String favoriteLanguage;
private LinkedHashMap<String, String> favoriteLanguageOptions;
// create no-arg constructor
public Student() {
// populate favorite language options
favoriteLanguageOptions = new LinkedHashMap<>();
// parameter order: value, display label
//
favoriteLanguageOptions.put("Java", "Java");
favoriteLanguageOptions.put("C#", "C#");
favoriteLanguageOptions.put("PHP", "PHP");
favoriteLanguageOptions.put("Ruby", "Ruby");
}
public String getFavoriteLanguage() {
return favoriteLanguage;
}
public void setFavoriteLanguage(String favoriteLanguage) {
this.favoriteLanguage = favoriteLanguage;
}
public LinkedHashMap<String, String> getFavoriteLanguageOptions() {
return favoriteLanguageOptions;
}
// define getter/setter methods
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
}
@DumitruTeodorMadalin
Copy link

Seems like alot of people fell into this "trap". There is something missing in the FAQ code in Udemy but in this code here it is present.
There are the following references:

String favoriteLanguage
and
LinkedHashMap favoriteLanguageOptions

you return just the value that you select from the LinkedHashMap by simply returning the String not the LinkedHashMap

Keep up the good work!

@Saikumar544
Copy link

Seems like alot of people fell into this "trap". There is something missing in the FAQ code in Udemy but in this code here it is present.
There are the following references:

String favoriteLanguage
and
LinkedHashMap favoriteLanguageOptions

you return just the value that you select from the LinkedHashMap by simply returning the String not the LinkedHashMap

Keep up the good work!

how can we return String in place of list?? its showing compile time error..

@paulranjan694
Copy link

paulranjan694 commented Jun 19, 2020

how can we return String in place of list?? its showing compile time error..

Hi Sai,

image
(snip of student-fom.jsp file)

As you can see the path & items are having different value in student-fom.jsp file

image
snip of Student.java file

also, you can find that both path="favoriteLanguage" items="${student.favoriteLanguageOptions}" are declared in Student.java file . So, through path, spring is doing the things for favoriteLanguage and rendering to the view page i.e., student-confirmation.jsp

image
(snap of student-confirmation.java)

@darbyluv2code
Copy link
Author

darbyluv2code commented Jun 19, 2020

Hi Paul,

Can you repost the question to the Udemy classroom discussion Q&A? We can provide tech support on the Udemy platform. Here's the link to the classroom discussion Q&A:
https://www.udemy.com/spring-hibernate-tutorial/learn/v4/questions

thanks for your understanding.

@paulranjan694
Copy link

paulranjan694 commented Jun 20, 2020

Hi Paul,

Can you repost the question to the Udemy classroom discussion Q&A? We can provide tech support on the Udemy platform. Here's the link to the classroom discussion Q&A:
https://www.udemy.com/spring-hibernate-tutorial/learn/v4/questions

thanks for your understanding.

Hi Chad,
Just thought of clearing the confusion of others, I am already cleared of the things what going there.
Thanks!

@sonalika-git
Copy link

I am trying to use radio button by using LinkedHashMap but in my jsp page i am unable to see multiple radio buttons with the label.
It is showing only one radio button with no labels.

@InclinedScorpio
Copy link

Hi can we use List<> in Select statements ?
Will both value, label will become same in that case ?

@InclinedScorpio
Copy link

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
Do we really need this line in student-confirmation.jsp file?
Why?

@darbyluv2code
Copy link
Author

Hi Ashutosh,

Can you repost the question to the Udemy classroom discussion Q&A? We can provide tech support on the Udemy platform. Here's the link to the classroom discussion Q&A:
https://www.udemy.com/spring-hibernate-tutorial/learn/v4/questions

thanks for your understanding.

@RajanXVII
Copy link

Thanks GURU!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment