Spring MVC preview implementation

I have following task: implement "preview" functionality using Spring MVC. And it will be good to know the best way to implement it:

It is straight forward task to 1. Populate new form and save this form in the DB: In order to do this on jsp page we have a spring form, we pass this from to controller and save this from. 2. Edit some form data: So, In order to do that we pass object id to controller, controller reads necessare data from DB -> User has possibility to edit this and save again in the DB.

But It looks like there is no straight forward mechanism for implementation "preview" functionality.

As example: We have a form and user has possibility to add necessary values to the form and then we should display how this data (that user added) will look like on the separate screen (we perform some data manipulation and diplaying how this data will look like for different tool).

In the code we have following situation:

  • In the controller in order to render preview we pass our model:

    @RequestMapping(value="/preview", method = RequestMethod.POST) public String preview(@ModelAttribute("form") SomeForm form){ return "preview"; }


  • and on the preview.jsp we have possibility to render it in the appropriate way:

     <div class="preview">
      <div id="1">${form.field1}</div>
      <div id="2">${form.field2}</div>
     </div>
    

    And on this jsp page we don't have a form. In order to get back to edit page we need to pass a form object because our controller requires it:

    @RequestMapping(value = "/admin/save", method = RequestMethod.POST)
    public String save(@ModelAttribute("form") SomeForm form ){
    

    And the main problem pass back this form to the original jsp page.


    add other method to set a preview

    ...
    @RequestMapping(value = "/admin/preview", method = RequestMethod.POST)
    public ModelAndView preview(@ModelAttribute("form") SomeForm form ){
        model = new ModelAndView();
        model.addObject("form", form);
        model.setViewName("jspPath")
        return model;
    }
    @RequestMapping(value = "/admin/save", method = RequestMethod.POST)
    public String save(@ModelAttribute("form") SomeForm form ){
    ...
    }
    

    in the jsp you have to refill the form with the new parameters, or use javascript ;-)


    In thew preview page along with showing the submitted values, add a form and add all the form attributes in hidden inputs . The submit on the preview page should take to save method instead of preview to save to db.

    链接地址: http://www.djcxy.com/p/92554.html

    上一篇: <form>操作如何工作?

    下一篇: Spring MVC预览实现