Reputation: 469
I am getting trouble in getting the value of drop down ,I have student entity and section entity there is a relation ship between them and in jsp its coming like com.chan.Eschool.student.model.Section@26552d
instead of this in jsp i need to get the specific bean property name like sectionName
@Entity
@Table(name="section")
public class Section implements Serializable {
private static final long serialVersionUID = 1L;
private Integer id;
private String sectionName;
private School school;
private List<Student> studentList;
public static long getSerialversionuid() {
return serialVersionUID;
}
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getSectionName() {
return sectionName;
}
public void setSectionName(String sectionName) {
this.sectionName = sectionName;
}
@ManyToOne(fetch=FetchType.EAGER)
@JoinColumn(name="school_id")
public School getSchool() {
return school;
}
public void setSchool(School school) {
this.school = school;
}
@OneToMany( mappedBy = "section" )
public List<Student> getStudentList() {
return studentList;
}
public void setStudentList(List<Student> studentList) {
this.studentList = studentList;
}
}
Student Model Class
@Entity
@Table(name="student")
public class Student {
private long id;
private String student_name;
private String roll_no;
private String standard;
private School school;
private Address address;
private StudentPhysicalInfo physicalInfo;
private Section section;
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getStudent_name() {
return student_name;
}
public void setStudent_name(String student_name) {
this.student_name = student_name;
}
public String getRoll_no() {
return roll_no;
}
public void setRoll_no(String roll_no) {
this.roll_no = roll_no;
}
public String getStandard() {
return standard;
}
public void setStandard(String standard) {
this.standard = standard;
}
@ManyToOne
@JoinColumn(name="school_id")
public School getSchool() {
return school;
}
public void setSchool(School school) {
this.school = school;
}
@Embedded
public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}
@OneToOne(mappedBy="student",cascade=CascadeType.ALL)
public StudentPhysicalInfo getPhysicalInfo() {
return physicalInfo;
}
public void setPhysicalInfo(StudentPhysicalInfo physicalInfo) {
this.physicalInfo = physicalInfo;
}
@ManyToOne
@JoinColumn(name =" section_Id")
public Section getSection() {
return section;
}
public void setSection(Section section) {
this.section = section;
}
}
My Dao implementation is like this
@Override
public List<Section> getSections() {
Session session = this.sessionFactory.getCurrentSession();
Query query = session.createQuery( "from Section s" );
List<Section> sectionList = query.list();
return sectionList;
}
JSP like.
<div class="panel-body">
<c:url value="/student/register" var="register" />
<form:form cssClass="form-horizontal" role="form" action="${register}" method="post" modelAttribute="student">
<div class="form-group">
<div class="col-sm-8 col-sm-offset-2">
<form:input path="student_name" class="form-control" id="inputEmail3"
placeholder="student_name" />
</div>
<div class="col-sm-8 col-sm-offset-2">
<form:errors path="student_name" />
</div>
</div>
<div class="form-group">
<div class="col-sm-8 col-sm-offset-2">
<form:input path="roll_no" class="form-control" id="inputEmail3"
placeholder="roll_no" />
</div>
<div class="col-sm-8 col-sm-offset-2">
<form:errors path="roll_no" />
</div>
</div>
<div class="form-group">
<div class="col-sm-8 col-sm-offset-2">
<form:input path="standard" class="form-control" id="inputEmail3"
placeholder="Standard" />
</div>
<div class="col-sm-8 col-sm-offset-2">
<form:errors path="standard" />
</div>
</div>
<h4 class="text-center">Address Details</h4>
<div class="form-group">
<div class="col-sm-8 col-sm-offset-2">
<form:input path="address.area" class="form-control" id="inputEmail3"
placeholder="Area" />
</div>
<div class="col-sm-8 col-sm-offset-2">
<form:errors path="section" />
</div>
</div>
<div class="form-group">
<div class="col-sm-8 col-sm-offset-2">
<form:input path="address.city" class="form-control" id="inputEmail3"
placeholder="City" />
</div>
<div class="col-sm-8 col-sm-offset-2">
<form:errors path="address.city" />
</div>
</div>
<div class="form-group">
<div class="col-sm-8 col-sm-offset-2">
<form:input path="address.country" class="form-control" id="inputEmail3"
placeholder="Country" />
</div>
<div class="col-sm-8 col-sm-offset-2">
<form:errors path="address.country" />
</div>
</div>
<div class="form-group">
<div class="col-sm-8 col-sm-offset-2">
<form:input path="address.pin" class="form-control" id="inputEmail3"
placeholder="pin" />
</div>
<div class="col-sm-8 col-sm-offset-2">
<form:errors path="address.pin" />
</div>
</div>
<div class="form-group">
<div class="col-sm-8 col-sm-offset-2">
<form:input path="address.state" class="form-control" id="inputEmail3"
placeholder="State" />
</div>
<div class="col-sm-8 col-sm-offset-2">
<form:errors path="address.state" />
</div>
</div>
<div class="form-group">
<div class="col-sm-8 col-sm-offset-2">
<form:input path="address.street" class="form-control" id="inputEmail3"
placeholder="Street" />
</div>
<div class="col-sm-8 col-sm-offset-2">
<form:errors path="address.street" />
</div>
</div>
<form:select path="section">
<form:options items="${sectionsList}"/>
</form:select>
<div class="form-group last">
<div class="col-sm-offset-3 col-sm-9">
<button type="submit" class="btn btn-success">Sign Up</button>
<button type="reset" class="btn btn-default">Reset</button>
</div>
</div>
</form:form>
</div>
</div>
</div>
</div>
</div>
</section>
Controller is like
@RequestMapping(value="/student/register",method=RequestMethod.GET)
public String registerStudent(Model model,@ModelAttribute Student student) {
List<Section> sectionsList = (List<Section>) sectionService.getSections();
for(Section section : sectionsList) {
System.out.println("Sections are "+ " "+section.getSectionName());
}
model.addAttribute("sectionsList",sectionsList);
return "student/registration";
}
My final touch up to this question is how to get the dropdown values of specified bean property name (here i want to get the bean property name like sectionName
)
I am getting the drop down value like com.chan.Eschool.student.model.Section@26552d
please guide me where I am wrong
Upvotes: 0
Views: 68
Reputation: 269
Create a Map<sectionKey, sectionLable>
in your controller
@RequestMapping(value="/student/register",method=RequestMethod.GET)
public String registerStudent(Model model,@ModelAttribute Student student) {
List<Section> sectionsList = (List<Section>) sectionService.getSections();
Map<Integer, String> sections = new LinkedHashMap<Integer, String>();
for(Section section : sectionsList) {
sections.put(section.getId(), section.getSectionName());
}
model.addAttribute("sectionsList",sectionsList);
model.addAttribute("sections",sections);
return "student/registration";
}
And simply bind it to jsp by calling <form:options items="${sections}"/>
Upvotes: 1