我有一个表格,可以映射学生信息以供搜索。
我也想添加类(class)编号作为搜索条件。类(class)编号通过注册与学生相关。
由于 Student 类中不存在类(class)编号,因此仅将学生作为 modelAttribute 发送不会将类(class)编号发送到 Controller 。
所以我想知道将类(class)编号发送到 Controller 的最佳方式是什么。
如有任何建议,我们将不胜感激。
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>
代码片段 Controller :
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;
}
}
最佳答案
尝试使用习惯的模型,我希望您不要将实体用作模型对象,因为这不是一个好的做法:
public class StudentModel{
private String name;
//other attributes
private String courseId;
// GETTERS and SETTERS for all the fields
}
PS:您可以使用 @Max
、 @Min
作为模型属性的验证。
您的 Controller :
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 的值使用 select 和 option
<td><input type="text" id="courseNo" name="courseId" value="${enrolments.courseNo}"/>
关于java - JSP - 添加一个不在 modelAttribute 上的表单参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24769740/