在spring mvc中获取下拉值的麻烦

时间:2015-05-12 02:38:37

标签: spring-mvc

我在获取下拉值方面遇到了麻烦,我有学生实体和部分实体,他们之间有一个关系,在jsp中它就像com.chan.Eschool.student.model.Section@26552d而不是在jsp中我需要得到特定的bean属性名称,如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;
            }

}

学生模特班

@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;
    }

}

我的Dao实现就像这样

@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喜欢。

<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>

控制器就像

@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";
         }

我对这个问题的最后修改是如何获取指定bean属性名称的下拉值(这里我想得到像sectionName这样的bean属性名称) 我得到了像com.chan.Eschool.student.model.Section@26552d

这样的下拉值

请指导我错误的地方

1 个答案:

答案 0 :(得分:1)

在您的控制器中创建Map<sectionKey, sectionLable>

     @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";
     }

只需通过调用<form:options items="${sections}"/>

将其绑定到jsp即可