-
-
Save darbyluv2code/debb69b1bf8010d84d50e0542e809ffb to your computer and use it in GitHub Desktop.
<%@ 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; | |
} | |
} |
Would it make any difference if favoriteLanguageOptions were declared and populated static? I'm thinking that it's kind of inefficient to instantiate and populate the favoriteLanguageOptions list every time a student registers.
@Coder-ACJHP facing same problem. Please help
Guys,
refreshing project and re-running worked for me. Check it once.
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!
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 favoriteLanguageOptionsyou 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..
how can we return String in place of list?? its showing compile time error..
Hi Sai,
(snip of student-fom.jsp file)
As you can see the path & items are having different value in student-fom.jsp 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
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 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/questionsthanks for your understanding.
Hi Chad,
Just thought of clearing the confusion of others, I am already cleared of the things what going there.
Thanks!
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.
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!!
@Coder-ACJHP I faced up with the same issue. But in my case it was just a typo because I wrote and changed code from previous example by my hands, the code was not not copied from this source.
So my string looked like <form:radiobutton path="favoriteLang" items="${student.favoriteLanguageOptions}" /> instead of <form:radiobuttons path="favoriteLang" items="${student.favoriteLanguageOptions}" /> Yes I missed that form:radiobuttons tag should be used instead of form:radiobutton.
Hope this help.