Note that there are some explanatory texts on larger screens.

plurals
  1. POValidator + MVC + REST::problems with update
    text
    copied!<p>I'm trying build my application using REST and Spring MVC. For some entities I have special page for update. After data submit it validated. If there are no errors it's redirecting to view of this entity otherway to edit page. haw I must pass data (entity and validator result) between controllers? </p> <p>here implementation with some very bad practice.</p> <pre><code>@RequestMapping(method = RequestMethod.PUT, value = "/workers/{id}") public ModelAndView update(@PathVariable final Long id, @Valid Worker entity, Errors errors, NativeWebRequest request) { ModelAndView model = new ModelAndView(); entity.setId(id); if (errors.hasErrors()) { request.setAttribute("entity", entity, RequestAttributes.SCOPE_SESSION); request.setAttribute("errors", errors.getAllErrors(), RequestAttributes.SCOPE_SESSION); model.setViewName("redirect:/workers/" + entity.getId()+ "/edit"); } else { System.out.println("upd-done"); service.update(entity); model.setViewName("redirect:/workers/" + entity.getId()); model.addObject(entity); } return model; } @RequestMapping(method = RequestMethod.GET, value = "/workers/{id}/edit") public ModelAndView updatePage(@PathVariable Long id, NativeWebRequest request) { ModelAndView model = new ModelAndView(); DomainObject entity = (DomainObject)request.getAttribute("entity", RequestAttributes.SCOPE_SESSION); model.addObject("entity", entity != null ? entity : service.get(id)); model.setViewName(names.provideViewName(Pages.EDIT)); return model; } </code></pre> <p>here form for edit</p> <pre><code>&lt;form id="entity" action="/workers/6" method="post"&gt;&lt;input type="hidden" name="_method" value="PUT"/&gt; foo &lt;input id="foo" name="foo" type="text" /&gt;&lt;/td&gt; &lt;input type="submit" value="Save Changes" /&gt; &lt;/form&gt; </code></pre> <p>Thanks.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload