Created
October 3, 2016 14:32
-
-
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?
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<%@ 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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<%@ 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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
} |
Hi can we use List<> in Select statements ?
Will both value, label will become same in that case ?
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
Do we really need this line in student-confirmation.jsp file?
Why?
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.
Thanks GURU!!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.