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

标签 java jsp spring-mvc

我有一个表格,可以映射学生信息以供搜索。

我也想添加类(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/

相关文章:

java - 在 apache tomcat 8.0 上部署后的 url 路径问题

java - Spring MVC Controller 中处理不可恢复异常的最佳实践

java - 创建 Map 并将其传递给请求范围的属性

java - Drools:获取 3 个最近的事件

sql - Java (JSP) - 从 'bad inputs' 和 'sql injection/poisoning' 获取表单

java - 是否可以根据用户输入限制查询结果?

java - REST 中通过另一个资源 ID 来命名获取资源的做法是什么

java - 找出我的线程落在 ReentrantLock 中的什么位置

java - IDL to java(Suns idlj)如何编译生成文件?

java - 以编程方式调用自定义 IntelliJ IDEA Annotator