JSP - 添加一个不在modelAttribute上的表单参数

时间:2014-07-15 23:20:45

标签: java jsp spring-mvc

我有一张表格,用于映射学生信息以进行搜索。

我想将课程编号添加为搜索条件。课程编号与学生通过报名有关。

由于课程编号在Student课程中不存在,因此只发送学生作为modelAttribute,不会将课程编号发送给控制器。

所以我想知道将课程编号发送给控制器的最佳方法是什么。

任何建议都表示赞赏。

代码段JSP文件:

<form:form method="POST" action="/tar/sisStudentSearch"
    commandName="student">
    <table class ="layoutBodyTable border">
        //...
                            <td class="label_r">First Name:</td>
                            <td><input type="text" name="firstName" maxlength="100" size="50" /></td>
                            <td class="label_r">Surname:</td>
                            <td><input type="text"  name="familyName" maxlength="100" size="50" /></td>

                        </tr>
                        <tr>
                            <td class="label_r"  alt="Gender">Gender:</td>
                            <td><input type="text" name="sexCode" maxlength="1" size="1" /></td>
                            <td class="label_r"  alt="Date of birth">Date of Birth:</td>
                            <td><input type="text" maxlength="10" size="10" /></td>
                        </tr>
                        <tr>
                            <td class="label_r">College No:</td>
                            <td><input type="text" maxlength="3" size="3" /></td>
                            <td class="label_r">Course No:</td>
                            <td><input type="text" id="courseNo" name="enrolments.courseNo" maxlength="5" size="5" />
                            //...
</form:form>

代码段控制器:

public class StudentSearchController {

    @Autowired
    StudentService studentService;


    @RequestMapping(value = "/sisStudentSearch", method = RequestMethod.POST)
    public ModelAndView searchStudents(@ModelAttribute("student") Student student,
            Map<String, Object> map, HttpServletRequest request) {

        List<Student> students = new ArrayList<Student>();
        // StudentService userService = new StudentService();
        Logger.getLogger(StudentSearchController.class).info("Student id received as parameter: " + student.getStudentNo());


            students = studentService.findStudents(student);        
            if (students!= null && !students.isEmpty()){
                Logger.getLogger(StudentSearchController.class).info("Students found ! First name: " + students.get(0).getFirstName());
            }
        }
        ModelAndView modelAndView = new ModelAndView("sisStudent");
        modelAndView.addObject("students", students);
        return modelAndView;

    }   

}

1 个答案:

答案 0 :(得分:1)

尝试使用习惯模型,我希望您不使用您的实体作为模型对象,因为这不是一个好习惯:

public class StudentModel{
    private String name;
    //other attributes
    private String courseId;
    // GETTERS and SETTERS for all the fields
}

PS:您可以使用@Max@Min作为模型属性的验证。

你的控制器:

public class StudentSearchController {

     @Autowired
     StudentService studentService;


     // added this assuming that you need courses from database
     @Autowired
     CourseService courseService;
     StudentModel studentmodel;

      @RequestMapping(method = RequestMethod.GET)
      public ModelAndView init() {
               studentmodel = new StudentModel();
               ModelAndView modelAndView = new ModelAndView();
               //assuming that your seach page is named studentpage
               List<Course> courses  = courseService.findCourses(..);
               modelAndView.addObject("student", studentmodel );  
               modelAndView.addObject("courses", courses);  
               //OR
               modelAndView.addObject("enrolments",courseService.findTheCourseYouNeed(..));
               // any other logic
       return modelAndView;
       }

      @RequestMapping(value = "/sisStudentSearch", method = RequestMethod.POST)
     public ModelAndView searchStudents(@ModelAttribute("student") StudentModel student,
                    Map<String, Object> map, HttpServletRequest request) {
                //here you can acess your courseNumber from StudentModel student
               // Apply you logic by calling the studentService filter method that gets attributs from the model
                ModelAndView modelAndView = new ModelAndView("sisStudent");
                modelAndView.addObject("students", students);
                return modelAndView;

     }   
}
JSP中的

将您输入的值分配给courseNo,或者使用courseId的值选择和选项

<td><input type="text" id="courseNo" name="courseId" value="${enrolments.courseNo}"/>