trouble in getting drop down values in spring mvc -
i getting trouble in getting value of drop down ,i have student entity , section entity there relation ship between them , in jsp coming com.chan.eschool.student.model.section@26552d
instead of in jsp need specific bean property name 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 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 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 "+ " "+section.getsectionname()); } model.addattribute("sectionslist",sectionslist); return "student/registration"; }
my final touch question how dropdown values of specified bean property name (here want bean property name sectionname
) getting drop down value com.chan.eschool.student.model.section@26552d
please guide me wrong
create map<sectionkey, sectionlable>
in 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 bind jsp calling <form:options items="${sections}"/>
Comments
Post a Comment