0

Whats JSPs equivalent of FreeMarker class FreeMarkerTemplateUtils.processTemplateIntoString which accepts two arguments (Template template, Object model).

Using FreeMarker, the code looks like:

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

How can I achieve above using JSP?

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Kelvin Muia
  • 336
  • 1
  • 3
  • 15

1 Answers1

0

Using JSPs you have two options:

1) Fill the model and return the logical name of the view

2) Create and return a ModelAndView object

Controller - Option 1

public String getReport(Model model) throws Exception {

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

    //you should have testing.jsp
    return "testing";
}

Controller - Option 2

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

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

    return modelAndView;
}

In both ways, the view (JSP page) can access the model data as shown bellow.

testing.jsp

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