开发者

Access model objects from view in Spring 2.5

Is there a way to access the objects in the model set by a Controller on a View in Spring 2.5? I'm using a SimpleFormController but instead of using a proper Validator, I perform the checks directly into the onSubmit() method because I'm working with jQuery on the client side and I don't want the whole ViewForm (a complete page) to be shown in case of errors (in which case I send back the HTML block with some error strings).

I'm therefore creating my own map in the onSubmit() but I can't manage to read it from the JSP page. Is there a clean way of doing this in Spring 2.5?

This is the first version of my controller:

protected 开发者_如何学运维ModelAndView onSubmit( Object command,
                                 BindException errors ) throws Exception {

  User user = ( User )command;

  // This is a common validator that I wanted to remain untouched.
  validator.validate( user, errors );

  // In this way I wanted to try to have both errors and the model in the view.
  return new ModelAndView( getSuccessView(), "model", errors.getModel() );
}

As I couldn't access the error list from my jsp, I tried to change the controller by using a class (KeepInformedPageData) that re-maps the model and the error in a new map that I know how to access:

protected ModelAndView onSubmit( HttpServletRequest request,
                                 HttpServletResponse response,
                                 Object command,
                                 BindException errors ) throws Exception {

  KeepInformedPageData pageData = new KeepInformedPageData( messageSource, request.getLocale() );
  User user = ( User )command;

  // This is a common validator that I wanted to remain untouched.
  validator.validate( user, errors );

  // As I couldn't access the model provided by errors.getModel(), I tried to 
  // build up my own model but unfortunately with the same result.
  pageData.addErrors( errors );
  pageData.addCommandModel( errors.getModel() );

  return new ModelAndView( getSuccessView(), "model", pageData.getRebuiltModel() );
}

I tried every possible combination on the jsp side but with no result, these are some of the attempts:

<br />
${model.command.email}
<br />
${model.errors.defaultMessage}
<br />
<form:form name="command" action="index.htm" method="POST">
  <form:errors path="model.invalid.email" />
  <div>Email <input type="text" name="email" value="Your email address"></div>
  <div><input type="submit" value="Submit" /></div>
</form:form>

So the final question is: how do I access the Map received from the controller?


http://static.springsource.org/spring/docs/2.5.x/api/org/springframework/web/servlet/mvc/SimpleFormController.html

Method onBindAndValidate


Ok, maybe it was too simple to be even considered a problem: while trying to work on the "errors" object received as a parameter in my Controller, I realized there was a method "getModel()" and from such a property I was trying to get all the subproperties coming from the command and this was the mistake that pushed me in the wrong direction.

I'm now simply passing the "errors" object from the Controller:

return new ModelAndView( getSuccessView(), "dataBlock", errors );

And I'm using the properties in the "errors" object within the JSP page:

<div>${resultBlock.fieldError.defaultMessage}</div>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜