如何在一个表单中的两个Dropbox中传递两个列表

时间:2017-05-01 02:27:21

标签: java html jsp spring-mvc

我正在尝试将两个列表(学生和课程)从Controller传递给jsp文件。它应该是两个dropbox,一个表单和一个提交按钮。现在我只能通过一个列表。想法是从一个Dropbox中选择学生,从另一个Dropbox中选择几个课程并提交注册。这是我的Controller,两个类:Student,Course和jsp文件。谢谢!

班级学生

@Entity
@Table(name = "students")
public class Student {

@Id
@GeneratedValue
@Column(name = "STUDENT_ID")
private int studentId;

@Column(name = "name")
private String name;

@ManyToMany(cascade = CascadeType.ALL )
@JoinTable(name="STUDENT_COURSE", joinColumns={ @JoinColumn(name="STUDENT_ID")},
        inverseJoinColumns = { @JoinColumn(name = "COURSE_ID") })
private Set<Course> courses = new HashSet<Course>();

public Student() {
}

public Student(String name) {
    this.name = name;
}

public Student(String name, Set<Course> courses) {
    this.name = name;
    this.courses = courses;
}


public int getStudentId() {
    return studentId;
}

public void setStudentId(int studentId) {
    this.studentId = studentId;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

@Override
public String toString() {
    return "Student{" +
            "name='" + name + '\'' +
            '}';
}

}

课程

@Entity
@Table(name = "courses")
public class Course {

@Id
@GeneratedValue
@Column(name = "COURSE_ID")
private int courseId;

@Column(name = "name")
private String courseName;

public Course() {
}

public Course(String courseName) {
    this.courseName = courseName;
}

public String getCourseName() {
    return courseName;
}

public void setCourseName(String courseName) {
    this.courseName = courseName;
}

public int getCourseId() {
    return courseId;
}

public void setCourseId(int courseId) {
    this.courseId = courseId;
}

@Override
public String toString() {
    return "Course{" +
            "courseName='" + courseName + '\'' +
            '}';
}

}

这个RegistrationController

@Controller
public class RegistrationController {

@Autowired
private StudentService studentService;
@Autowired
private CourseService courseService;


@RequestMapping(value = "/register")
public String showRegister(Model model){

    Student student = new Student();
    Course course = new Course();
    model.addAttribute("student", student);
    model.addAttribute("course", course);
    List<Student> students = studentService.showAllStudents();
    model.addAttribute("students", students);
    List<Course> courses = courseService.showAllCourses();
    model.addAttribute("courses", courses);

    return "registration";
} .   

registration.jsp

<%@ taglib prefix="form" 
uri="http://www.springframework.org/tags/form"%>
<html>

<title>Spring MVC dropdown box</title>

<h2>Select student</h2>



<form:form  method="POST" action ="register" commandName="student">

    <table>

        <tr>

            <td>Please select:</td>

            <td><form:select path="name" >

                  <form:option value="" label="student" />

                  <form:options items="${students}" />

                   </form:select>

                          </td>

            <td><form:errors path="name" cssStyle="color: #ff0000;" /></td>

        </tr>

        <tr>

            <td><input type="submit" name="submit" value="Submit"></td>

        </tr>



    </table>

</form:form>

  。

1 个答案:

答案 0 :(得分:0)

您可以为课程创建另一个<form:select并删除commandName,因为您可以将studentscourses作为HTTP参数发送,然后可以从{{1控制器中的对象。

更新:最好创建一个简单的HTTPSerlvetRequest而不是Spring <form>,因为这只适用于将表单绑定到一个POJO时。