如何在请求转发后停止将 Portlet ImplicitModel 传播到下一个处理程序?

发布于 2024-11-02 20:01:14 字数 2253 浏览 0 评论 0原文

对于不使用命令对象丰富的模型,处理程序应该返回什么? ModelAndView - 丰富,Model - 丰富,Map - 丰富......一切都通过 ImplicitModel 丰富。我可以以某种方式停止将隐式模型传播到 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);
    }

然后“模型”参数(implicitModel)的参数继续到下一个处理程序,因为 Spring 的 AnnotationMethodHandlerAdapter 中存在这种情况。

if (returnValue instanceof ModelAndView) {
    ModelAndView mav = (ModelAndView) returnValue;
    mav.getModelMap().mergeAttributes(implicitModel);
    return mav;
}

View 类如下所示:

    @Component("someView")
    public class SomeView 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();
    }
}

即使我从 processSuccess 处理程序中删除“Model model”属性,也会发生这种情况。只是将implicitModel 参数传播到ajaxResponse 视图中,而不仅仅是一个带有我在其中添加的参数的新Map

如何停止此传播?

它涉及这个问题< /a>,在 spring-portlet-mvc 中,当请求根据某些条件转发到处理程序并传递一些要在 View 中渲染的参数,而不是已经处理过的原始 CommandObject 时,有时需要这样做。

创建了 JIRA 问题 - SPR-8267,如果您遇到相同问题,请投票。

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 SomeView 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

沒落の蓅哖 2024-11-09 20:01:14

答案是:清除 ModelMap 以防止将其存储为 ImplicitModel。

@RequestMapping
public String render(ModelMap modelMap, SessionStatus status, RenderRequest request, RenderResponse response) {
        modelMap.clear();
...
}

请注意,如果您在方法级别使用 @ModelAttribute,则在您分派到同一控制器内的不同处理程序后,modelMap 会填充它。

成功调用操作方法后,您可能需要手动清除模型以防止操作模型数据存储在 ImplicitModel 中。

首先,spring-mvc 和 spring-portlet-mvc 在处理 POST 请求方面的区别在于,spring-mvc POST 处理程序直接分派到 VIEW,而在 spring-portlet-mvc 中,始终遵循操作阶段(POST 请求处理程序)通过由另一个处理程序处理的渲染阶段,整个模型保留在请求中(主要是命令对象和 BindingResult)... Post/Redirect/Get

无论如何,在将请求分派到 VIEW 后,总是有机会过滤其中的模型...通过声明您只想要或不想要哪些参数不再在模型中...

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...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文