【问题标题】:trouble in getting drop down values in spring mvc在spring mvc中获取下拉值的麻烦
【发布时间】:2015-05-12 02:38:36
【问题描述】:

我在获取下拉值时遇到了麻烦,我有学生实体和部分实体,它们之间存在关系,在 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

这样的下拉值

请指点我哪里错了

【问题讨论】:

  • 我对 jsp 不是很熟悉(我使用的是thymeleaf),但我认为您的path 是错误的。而不是&lt;form:select path="section"&gt; 使用这个&lt;form:select path="section.sectionName"&gt;
  • 我试过了,但我得到了与之前相同的结果。感谢您的评论:-)

标签: spring-mvc


【解决方案1】:

在你的控制器中创建一个Map&lt;sectionKey, sectionLable&gt;

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

只需调用&lt;form:options items="${sections}"/&gt;将其绑定到jsp

【讨论】:

    猜你喜欢
    • 2023-03-28
    • 2011-12-05
    • 1970-01-01
    • 2019-08-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多