views:

44

answers:

2

Hi All,

I have this problem in my spring mvc 2.5 apps and I am not sure what should I do.

Kindly look at my code:

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());
 }
}

How is it possible to add some message when I display my formview again so that I could tell the user that adding the station was not successful?

And how to handle that in my jsp?

Hope somebody can help me please..

A: 

There are a couple ways to do that. I prefer not to use the showForm() method b/c I want more control. So I do one of the following, I'm sure there will be several alternative answers given for your question.

If you don't want to fail b/c of a specific field you can just send back an error on the model like this:

ModelAndView mav = new ModelAndView(this.getFormView());
mav.addObject(this.getCommandName(), command);
mav.addObject("errorMessage", "The thing you tried to do failed");
return mav;

Then in your jsp you would do this:

<c:if test="!empty errorMessage">
${errorMessage}
</c:if>

If you have a specific field that has caused the error you can attach an error to the specific field like this (this rejects the length of a field called "alternateid":

errors.rejectValue("alternateId", "longerThan",
new Object[] { Integer.valueOf(2) }, "Please enter at least two characters.");
ModelAndView mav = new ModelAndView(this.getFormView());
mav.addAllObjects(errors.getModel());
mav.addObject(this.getCommandName(), command);
return mav;

Then in your jsp you would use the form tag library and do this:

<form:errors path="alternateId"/>

That's assuming you're using the spring form tag library.

ballmw
+2  A: 

I at first thought you might want to use Validators but instead I think you can do the following:

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());
 }
}

Then in your JSP you can do the following:

<form:errors path="*">

Then any errors you bind will show up there.

tkeE2036