How to read processed view jsp in spring?

0

Is there a way to get a view as String already processed?

I need to send emails and I want to avoid creating the html views in a String and better create a JSP view and be able to call it as a String .

I've dealt with TemplateEngine like this:

import org.springframework.restdocs.templates.Template;
import org.springframework.restdocs.templates.TemplateEngine;

@Service
public class LectorVistas
{
    @Autowired
    private TemplateEngine templateEngine;

    public String obtenerVista(Model model, String vistaNombre) throws 
    IOException {
        Template vista = templateEngine.compileTemplate(vistaNombre);

        String view = vista.render(model.asMap());

        return view;
   }
}

But it generates an exception:

  

No qualifying bean of type   [org.springframework.restdocs.templates.LectorVista] found for   dependency: expected at least 1 bean which qualifies as autowire   candidate for this dependence

Is there a better way to do it? What is the error in my code?

    
asked by Eddy 31.08.2017 в 01:36
source

1 answer

1

The first thing you have to take into account is that a JSP is a servlet . You must go through the entire process to be rendered as HTML.

That is, and depending on the optimizations of the "translation" engine of JSPs, if it is the first request to that JSP, then there is no class file. Then the JSP is translated to a Java file ( servlet ). In some application servers it is possible to take a look at those generated files. The file is compiled and the class is loaded into memory. An instance of the servlet is created. The container dispatches a thread to handle the HTTP request with this instance. The response is generated and sent to the browser. The browser interprets the response. 1

Although it is possible to use a servlets engine embedded as Jetty to get the resulting HTML, perhaps what is more convenient in this case is to use a template engine such as Velocity 2 or FreeMarker 3 .

References

  • Mahesh P. Matha. (2013). JSP and Servlets: A Comprehensive Study . India: Prentice-Hall. p. 17.
  • There is an example in Spring Email Velocity Template Example .
  • There is an example in Sending email with freemarker template .
  • answered by 01.09.2017 в 00:31