Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/spring-mvc/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
SpringMVC。如何将JSP处理为以模型作为参数的字符串_Jsp_Spring Mvc_Freemarker - Fatal编程技术网

SpringMVC。如何将JSP处理为以模型作为参数的字符串

SpringMVC。如何将JSP处理为以模型作为参数的字符串,jsp,spring-mvc,freemarker,Jsp,Spring Mvc,Freemarker,与接受两个参数(模板模板、对象模型)的FreeMarkerclassFreeMarkerTemplateUtils.processTemplateIntoString等价的JSP是什么 使用FreeMarker,代码如下所示: public String getReport(ModelMap model) throws Exception { model.addAttribute("name", "tim"); String template = "testing.ftl";

与接受两个参数(模板模板、对象模型)的
FreeMarker
class
FreeMarkerTemplateUtils.processTemplateIntoString
等价的JSP是什么

使用FreeMarker,代码如下所示:

public String getReport(ModelMap model) throws Exception {
    model.addAttribute("name",  "tim");
    String template = "testing.ftl";
    String htmlStr = FreeMarkerTemplateUtils.processTemplateIntoString(
            freemarkerConfig.getConfiguration().getTemplate(template),
            model);
    // ...
}

如何使用JSP实现上述目标?

使用JSP,您有两种选择:

1) 填充模型并返回视图的逻辑名称

2) 创建并返回
ModelAndView
对象

控制器-选项1

public String getReport(Model model) throws Exception {

    model.addAttribute("name",  "tim");

    //you should have testing.jsp
    return "testing";
}
控制器-选项2

public ModelAndView getReport(HttpServletRequest arg0,
        HttpServletResponse arg1) throws Exception {

    ModelAndView modelAndView = new ModelAndView("testing");
    modelAndView.addObject("name", "tim");

    return modelAndView;
}
在这两种方式中,视图(JSP页面)都可以访问模型数据,如下所示

testing.jsp

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<body>
    <p>${name}</p>
</body>
</html>

${name}


Hi。字符串转换的主要原因是从它生成一个pdf文档,而不是显示它自己的页面。现在我了解了您的要求。我认为JSP模板不可能做到这一点。