1

我是 Spring MVC 的新手。我找不到这样做的方法:

假设,我有一个域类。

class CourseRegistration {
  Student student; // Have id and name
  Course course; // Have id and name
  String semester;
}

我想创建一个弹簧表单来创建课程注册。

课程由控制器填充的组合框选择。

学生姓名将被写入文本字段,但有一项服务可以通过姓名查找用户。

如何编写这样的控制器和视图?

  • 您可以假设我为学生、课程和课程注册提供了适当的商业服务。
4

1 回答 1

2

首先,定义表单的模型。必须是 POJO。

注册将保留:用户名和课程(添加您想要的任何内容)。

class RegistrationModel{
    @NotNull
    private String username;

    @NotNull
    private Course course;

    // getters and setters
}

控制器

@RequestMapping("/registration/**")
@Scope("session")
@Controller
class RegistrationController{
    @Autowired  // or inject manually in app context
    private courseService;

    @Autowired  // or inject manually in app context
    private studentService;

    // No need for getter and setter. However is a best practice to write them
    // Storing the mode in the controller will allow you to reuse the same
    // model between multiple posts (for example, validation that fails)
    private RegistrationModel registrationModel; 

    // getters and setters as usual

    // inject into the view the course list
    // there are other ways to do that, 
    // i'll get my preferred one. 
    // refer to Spring online documentation
    @ModelAttribute("courses")
    public List<Course> injectCourses(){
        return courseService.findAll();
    }

    // create and inject the registration model
    @ModelAttribute("registration")
    public RegistrationModel injectRegistration(){
        if(registrationModel==null)
            registrationModel = new RegistrationModel();

        return registrationModel;
    }

    // handles the post
    @RequestMapping(method=RequestMethod.POST)
    public ModelAndView doRegistration(@Valid @ModelAttribute("registration") registration, BindingResult bindingResult){
        if(bindingResult.hasError())
            return new ModelAndView("registration/index"); // redraw the form

        CourseRegistration cr = new CourseRegistration(); // or get it as a bean from the context
        cr.setStudent(studentService.findByUsername(registrationModel.getUsername()));
        cr.setCourse(registrationModel.getCourse());

        courseService.save(cr);

        return new ModelAndView("registration/confirm"); // imaginary path... 
    }

    // "draws" the form
    @RequestMapping
    public ModelAndView registration(){
        return new ModelAndView("registration/index"); // the path is hypothetic, change it
    }

}

JSPX(表格摘录)

<form:form modelAttribute="registration" method="POST">
    <form:input path="username" />
    <form:errors path="username" />
    <form:select path="course">
        <c:foreach items="${courses}" var="course">
            <form:option value="${course}" label="${course.name [the property to be shown in the drop down]}" />
        </c:foreach>
    </form:select>
    <form:errors path="course" />
</form:form>

您需要导入以下命名空间:

http://www.springframework.org/tags/form

http://java.sun.com/jsp/jstl/core

(检查他们)

我添加了用于模型验证的代码。实施您自己的验证器来检查学生的存在应该是一个好主意。只需 Google 搜索“JSR 303 春季验证”,您就会找到大量资源。

这应该是一个很好的开始。

而且...不要相信代码的正确性。我根据我在没有智能感知的情况下能回忆起的内容即时编写它(上帝保佑智能感知!:-))。

斯特凡诺

于 2013-03-26T16:23:30.083 回答