How to stop propagation of Portlet ImplicitModel into the next handler after request forward?
what should handler return for the model not to be enriched with command object ? ModelAndView - enriched, Model - enriched, Map - entriched ... everything is enriched with the ImplicitModel. Can I somehow stop the propagation of the implicit model to the ajaxResponse View ?
@ActionMapping(params = "javax.portlet.action=sample")
public void response(ActionRequest request, ActionResponse response, Bean bean) {
response.setRenderParameter("javax.portlet.action", "success");
List<MultipartFile> fileList = request.getFiles("file");
}
.....
@RequestMapping(params = "action=success")
public ModelAndView processSuccess(RenderRequest request, Model model) throws IOException {
Map map = new HashMap();
map.put("sucess", "sucess");
return new ModelAndView("ajaxResponse", map);
}
Then the parameters of the "model" argument (implicitModel) goes on to the next handler, because of this condition in Spring's AnnotationMethodHandlerAdapter.
if (returnValue instanceof ModelAndView) {
ModelAndView mav = (ModelAndView) returnValue;
mav.getModelMap().mergeAttributes(implicitModel);
return mav;
}
The View class goes like this:
@Component("someView")
public class SomeVie开发者_StackOverflow中文版w extends AbstractView {
private Logger logger = Logger.getLogger(SomeView.class);
@Override
protected void renderMergedOutputModel(Map map, HttpServletRequest request, HttpServletResponse response)
throws Exception {
logger.info("Resolving ajax request view - " + map);
JSONObject jsonObj = new JSONObject(map);
logger.info("content Type = " + getContentType());
response.setContentType("application/json");
response.setCharacterEncoding("UTF-8");
response.getWriter().write(jsonObj.toString());
response.getWriter().flush();
}
}
It happens even if I remove the "Model model" attribute from processSuccess handler. Simply the implicitModel parameters are propagated into the ajaxResponse view, instead of just a new Map with the parameter I added there
How to stop this propagation ?
It relates to this question, in spring-portlet-mvc this is sometimes needed when request is forwarded to a handler based on some condition and hand it over some parameters that are to be rendered in View, but not the original CommandObject, which has been already processed.
CREATED A JIRA ISSUE - SPR-8267, PLEASE VOTE UP IF YOU HAVE THE SAME PROBLEM.
The answer is : clear ModelMap to prevent it from being stored as an ImplicitModel.
@RequestMapping
public String render(ModelMap modelMap, SessionStatus status, RenderRequest request, RenderResponse response) {
modelMap.clear();
...
}
Notice that if you are using @ModelAttribute at method level, modelMap gets populated with it after you dispatch to different handler within the same controller.
After a successful action method call, you may want to manually clear the model to prevent the action model data from being stored in the ImplicitModel.
First of all, the difference between spring-mvc and spring-portlet-mvc as to handling POST requests is that spring-mvc POST handler dispatches to VIEW directly whereas in spring-portlet-mvc the action phase (POST request handler) is always followed by render phase which is handled by another handler and the entire Model remains in the request (mainly command object and BindingResult) ... Post/Redirect/Get
Anyway after the request is dispatched to a VIEW, there is always a chance to filter the model in there... By declaring which parameters you want only or do not want in the model anymore...
精彩评论