1

私はSpringMVCの初心者です。私はそれを行う方法を見つけることができませんでした:

ドメインクラスがあるとします。

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

(チェックしてください)

モデル検証用のコードを追加しました。学生の存在を確認するために、独自のバリデーターを実装することをお勧めします。「JSR303春の検証」のためのGoogleだけで、たくさんのリソースを見つけることができます。

これは、最初から良いバックボーンになるはずです。

そして...コードの正確さを信用しないでください。インテリセンスなしで思い出せるものに基づいて、その場で書きました(神はインテリセンスを祝福します!:-))。

ステファノ

于 2013-03-26T16:23:30.083 に答える