spring mvc 的问题

spring MVC 里的自定义controller

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

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

3个回答

举个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中了

blent
blent 嗯,谢谢了。现在理解了。对象是放在了模型里,最后view解释器和jsp还是要对模型进行处理,最后只是在html里组装简单数据类型而已
接近 7 年之前 回复

ModelAndView view = new ModelAndView();
view.setView(new InternalResourceView("/error.jsp"));
view.addObject("REQUEST_ID", MDC.get(LoggingProcessFilter.REQUEST_ID));
view.addObject("EXCEPTION", "

"+s.getString()+"
");

在页面中使用${"REQUEST_ID"}就能输出

如果你要说原理 那么就是jsp在服务器时其实也是一段字符串 一行行解析看看有没jsp标签等一大堆标签然后进行处理 ModelAndView的这个标签也是现在处理的 替换一下就行了 理解jsp的本质你就明白了

看错了 html? 其实也是个文件 发送出去的时候替换里面的内容就行了 服务器和浏览器交互的时候 其实就是服务器把这个文件发送给浏览器 读文件的时候替换一下里面内容 不是很简单吗?

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问