-
-
Save xmkhatshwa/200af57e3601eab283478a42d5ce9b75 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; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment