blent 2013-04-08 21:42 采纳率: 25%
浏览 269
已采纳

spring mvc 的问题

spring MVC 里的自定义controller

新的一个方法里给一个ModelAndView里放一个对象
这个方法然后返回一个String

我的问题是是,那个返回的页面是html它是怎么解析一个java对象的?

  • 写回答

3条回答

  • jinnianshilongnian 2013-04-09 08:46
    关注

    举个JstlView的例子(即如jsp)

    JstlView继承了InternalResourceView:

    /**
     * Render the internal resource given the specified model.
     * This includes setting the model as request attributes.
     */
    @Override
    protected void renderMergedOutputModel(
            Map<String, Object> model, HttpServletRequest request, HttpServletResponse response) throws Exception {
    
        // Determine which request handle to expose to the RequestDispatcher.
        HttpServletRequest requestToExpose = getRequestToExpose(request);
    
        [color=red]// Expose the model object as request attributes.
        exposeModelAsRequestAttributes(model, requestToExpose);[/color]
    
        // Expose helpers as request attributes, if any.
        exposeHelpers(requestToExpose);
    
        // Determine the path for the request dispatcher.
        String dispatcherPath = prepareForRendering(requestToExpose, response);
    
        // Obtain a RequestDispatcher for the target resource (typically a JSP).
        RequestDispatcher rd = getRequestDispatcher(requestToExpose, dispatcherPath);
        if (rd == null) {
            throw new ServletException("Could not get RequestDispatcher for [" + getUrl() +
                    "]: Check that the corresponding file exists within your web application archive!");
        }
    
        // If already included or response already committed, perform include, else forward.
        if (useInclude(requestToExpose, response)) {
            response.setContentType(getContentType());
            if (logger.isDebugEnabled()) {
                logger.debug("Including resource [" + getUrl() + "] in InternalResourceView '" + getBeanName() + "'");
            }
            rd.include(requestToExpose, response);
        }
    
        else {
            // Note: The forwarded resource is supposed to determine the content type itself.
            exposeForwardRequestAttributes(requestToExpose);
            if (logger.isDebugEnabled()) {
                logger.debug("Forwarding to resource [" + getUrl() + "] in InternalResourceView '" + getBeanName() + "'");
            }
            rd.forward(requestToExpose, response);
        }
    }
    

    红色部分是把模型数据暴露给view, 其具体实现在AbstractView的

    /**
     * Expose the model objects in the given map as request attributes.
     * Names will be taken from the model Map.
     * This method is suitable for all resources reachable by {@link javax.servlet.RequestDispatcher}.
     * @param model Map of model objects to expose
     * @param request current HTTP request
     */
    protected void exposeModelAsRequestAttributes(Map<String, Object> model, HttpServletRequest request) throws Exception {
        for (Map.Entry<String, Object> entry : model.entrySet()) {
            String modelName = entry.getKey();
            Object modelValue = entry.getValue();
            if (modelValue != null) {
    

    [color=red] request.setAttribute(modelName, modelValue);[/color]
    if (logger.isDebugEnabled()) {
    logger.debug("Added model object '" + modelName + "' of type [" + modelValue.getClass().getName() +
    "] to request in view with name '" + getBeanName() + "'");
    }
    }
    else {
    request.removeAttribute(modelName);
    if (logger.isDebugEnabled()) {
    logger.debug("Removed model object '" + modelName +
    "' from request in view with name '" + getBeanName() + "'");
    }
    }
    }
    }
    可以看到其实它把数据放到request中了

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题