I'm following the Spring Getting Started tutorials and I'm breaking my brains on how to do something that should be relatively simple like accessing the result of another path in the same Controller.
What I'm trying to do:
- Return a filled Thymeleaf template as HTML to the browser <- this works out of the box
- Return the same page as a pdf
GreetingController:
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.thymeleaf.TemplateEngine;
@Controller
@RequestMapping(path = "/")
public class GreetingController {
@Autowired private TemplateEngine templateEngine;
@RequestMapping(value = "/index", method = RequestMethod.GET, produces = "application/html")
public String html(Model model) {
model.addAttribute("some_data", some_data.getIt());
return "some_template";
}
@RequestMapping(value = "/pdf", method = RequestMethod.GET, produces = "application/pdf")
public String pdf() {
// Option 1: get HTML output from html path
// Option 2: put the same data in some_template via the template engine and get the resulting HTML
// write HTML to a file using FileWriter
// then print the temporary file with HTML to PDF via wkhtml2pdf
return "generated_pdf";
}
}`
Maybe I'm going about this all wrong and there is a much easier way to get the filled HTML, please advise.
EDIT:
Gradle dependencies for people trying to do something similar:
dependencies {
compile("org.springframework.boot:spring-boot-starter-web")
compile("org.springframework.boot:spring-boot-starter-thymeleaf")
compile("org.springframework.boot:spring-boot-devtools")
testCompile("org.springframework.boot:spring-boot-starter-test")
}