I am building a springboot project with thymeleaf. The project runs as expected when running from within eclipse. i.e I am able to access model attributes in thymeleaf.
However, when I package the above project as a jar, thymeleaf is unable to access model attributes added on the backend.
1) Java code on the backend:
@RequestMapping("/")
public String index(Model model) throws Exception {
String headshotsfileNameStrings = "";
InputStream resource = new ClassPathResource("static/images/xyz-headshots/").getInputStream();
try (BufferedReader reader = new BufferedReader(new InputStreamReader(resource))) {
headshotsfileNameStrings = reader.lines().collect(Collectors.joining(","));
}
model.addAttribute("headshotsfileNameStrings", headshotsfileNameStrings);
System.out.println(model);
return "index";
}
EDIT 1: I updated above code to:
try (BufferedReader reader = new BufferedReader(new InputStreamReader(resource))) {
System.out.println(reader.readLine()); //This prints 'null' when running application as a Jar
headshotsfileNameStrings = reader.lines().collect(Collectors.joining(","));
}
2) Thymeleaf frontend code
<script>
var headshotsfileNameStrings = "[[${headshotsfileNameStrings}]]";
</script>
The variable 'headshotsfileNameStrings' has value "" when the application is run as a springboot jar and has value "some_string_here_xyz" when the application is run from Eclipse.
What could be going on here? I don't see any errors and I don't know where to start debugging this from. Any ideas?