简体   繁体   English

Spring MVC预览实现

[英]Spring MVC preview implementation

I have following task: implement "preview" functionality using Spring MVC. 我有以下任务:使用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. 这是直截了当的任务1.填充新表单并将此表单保存在DB中:为了在jsp页面上执行此操作,我们有一个弹簧形式,我们将其传递给控制器​​并保存。 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. 2.编辑一些表单数据:因此,为了做到这一点,我们将对象id传递给控制器​​,控制器从DB读取必要的数据 - >用户有可能编辑它并再次保存在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: 在代码中我们有以下情况:

  1. 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"; @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: 在preview.jsp上我们有可能以适当的方式呈现它:

 <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. 在这个jsp页面上,我们没有表格。 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. 主要问题是将此表单传回原始jsp页面。

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 ;-) 在jsp中你必须用新参数重新填充表单,或者使用javascript ;-)

In thew preview page along with showing the submitted values, add a form and add all the form attributes in hidden inputs . 在w预览页面中显示提交的值,添加表单并在hidden inputs添加所有表单属性。 The submit on the preview page should take to save method instead of preview to save to db. 预览页面上的submit应该采用save方法而不是preview来保存到数据库。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM