Reputation: 267320
Is it possible to return a string when my action is returning a ModelAndView?
public ModelAndView register(....) {
ModelAndView mav = new ModelAndView(...);
if( // some wierd case) {
// halt and return a string response
}
return mav;
}
This isn't for a normal case, I know I should redirect to another page and display some sort of message, but this is a unusual case where I don't really want to do that or at least I want to know if I have this option to do this.
Upvotes: 2
Views: 1149
Reputation: 16400
I would throw an Exception. You can create a subclass of Exception for this case - a subclass that can hold and answer a string. In this way you can return a String in special situations, but answer a ModelAndView most other times.
public ModelAndView register(....) throws WierdCaseException {
ModelAndView mav = new ModelAndView(...);
if( // some wierd case) {
// halt and return a string response
throw new WierdCaseException("the string to be returned");
}
return mav;
}
Upvotes: 1
Reputation: 284
You can mention return type as String rather than ModelAndView. returned string name should be a name of jsp file.
Upvotes: 0
Reputation: 430
If you are returning a ModelAndView, are you populating your view with values on the server side. Can you return the string as part of the ModelAndView?
mav.addObject("myString", "this is the string for the weird case");
Other option is to get the HttpServletResponse object's PrintWriter or ServletOutputStream and write the string out and then return a null ModelAndView.
Upvotes: 0