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 Qt下使用tcp获取数据的详细操作
  • ¥15 idea右下角设置编码是灰色的
  • ¥15 全志H618ROM新增分区
  • ¥15 在grasshopper里DrawViewportWires更改预览后,禁用电池仍然显示
  • ¥15 NAO机器人的录音程序保存问题
  • ¥15 C#读写EXCEL文件,不同编译
  • ¥15 MapReduce结果输出到HBase,一直连接不上MySQL
  • ¥15 扩散模型sd.webui使用时报错“Nonetype”
  • ¥15 stm32流水灯+呼吸灯+外部中断按键
  • ¥15 将二维数组,按照假设的规定,如0/1/0 == "4",把对应列位置写成一个字符并打印输出该字符