如何在Spring MVC simpleformcontroller上添加错误?

Mar*_*ada 3 spring spring-mvc

我的Spring MVC 2.5应用程序中存在这个问题,我不知道该怎么做.

这是我的代码:

public class AddStationController extends SimpleFormController {
 private SimpleStationManager stationManager;

 protected ModelAndView onSubmit(HttpServletRequest request,
   HttpServletResponse response, Object command, BindException errors)
   throws Exception {
  StationDetails detail = (StationDetails) command;
  //add to DB
  int return = stationManager.addStation(detail);

  //return value: 1 = successful, 
  //    if not = unsuccessful

  if(return != 1){
   //how can I add error so that when I display my formview ,
   //I could notify the user that saving to the db is not successful?
   showform();
  }
  return new ModelAndView("redirect:" + getSuccessView());
 }
}
Run Code Online (Sandbox Code Playgroud)

当我再次显示我的formview时,如何添加一些消息以便我可以告诉用户添加该站不成功?

以及如何在我的jsp中处理它?

Ian*_*las 6

我起初以为你可能想要使用Validators,但我认为你可以做以下事情:

public class AddStationController extends SimpleFormController {
 private SimpleStationManager stationManager;

 protected ModelAndView onSubmit(HttpServletRequest request,
   HttpServletResponse response, Object command, BindException errors)
   throws Exception {
  StationDetails detail = (StationDetails) command;
  //add to DB
  int return = stationManager.addStation(detail);

  //return value: 1 = successful, 
  //    if not = unsuccessful

  if(return != 1){
   //Account for failure in adding station
   errors.reject("exception.station.submitFailure", "Adding the station was not successful");
   showform(request, response, errors);
  }
  return new ModelAndView("redirect:" + getSuccessView());
 }
}
Run Code Online (Sandbox Code Playgroud)

然后在JSP中,您可以执行以下操作:

<form:errors path="*">
Run Code Online (Sandbox Code Playgroud)

然后你绑定的任何错误都会显示在那里.