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
classFreeMarkerTemplateUtils.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模板不可能做到这一点。