[英]Thymeleaf is unable to access model attribute if springboot application is packaged as a jar
I am building a springboot project with thymeleaf. 我正在与百里香一起建设一个springboot项目。 The project runs as expected when running from within eclipse.
从Eclipse中运行时,项目将按预期运行。 ie 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. 但是,当我将上述项目打包为jar时,thymeleaf无法访问在后端添加的模型属性。
1) Java code on the backend: 1)后端的Java代码:
@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: 编辑1:我将上面的代码更新为:
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 2)Thymeleaf前端代码
<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. 当应用程序作为springboot jar运行时,变量“ headshotsfileNameStrings”具有值“”,而从Eclipse运行时,变量“ headshotsfileNameStrings”具有值“ some_string_here_xyz”。
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?
有任何想法吗?
My first check would be if the file is indeed found, you can put a debug point and see if your resource value indeed holds the file that you expect. 我的第一个检查将是是否确实找到了该文件,您可以放置一个调试点,然后查看您的资源值是否确实包含所需的文件。
Also, add a catch clause and print the stacktrace instead of using throws Exception
, right now if something goes wrong in this method it does so silently. 另外,添加一个catch子句并打印stacktrace而不是使用
throws Exception
,现在,如果此方法出现问题,它会默默地这样做。
What I believe happens at the moment, is that you have an IOException file not found, but you cannot see it because you do not print the stacktrace. 我相信目前发生的是,您没有找到IOException文件,但是您看不到它,因为您没有打印stacktrace。
UPDATE: 更新:
I expect that 'xyz-headshots' is a filename. 我希望“ xyz-headshots”是文件名。 If that is the case, add the file extension and remove the trailing "/".
如果是这种情况,请添加文件扩展名并删除结尾的“ /”。 So if it is a text file for example it should look like this
static/images/xyz-headshots.txt
因此,例如,如果它是文本文件,则其外观应为
static/images/xyz-headshots.txt
This code worked for me in the IDE as well as when the application was run as a JAR: 这段代码在IDE中以及当应用程序作为JAR运行时都对我有用:
@RequestMapping("/")
public String index(Model model) {
String headshotsfileNameStrings = "";
ResourceLoader resourceLoader = new DefaultResourceLoader();
Resource[] resources;
try {
resources = ResourcePatternUtils.getResourcePatternResolver(resourceLoader)
.getResources("classpath:" + "/static/images/xyz-headshots/*.jpg"); //get all files in the directory
for (int i = 0; i < resources.length; i++) {
String fileName = resources[i].getFilename(); //fileName gets populated now.
headshotsfileNameStrings += fileName + ",";
}
} catch (Exception e) {
e.printStackTrace();
}
model.addAttribute("headshotsfileNameStrings", headshotsfileNameStrings);
return "index";
}
I took help from the answers listed here: Classpath resource not found when running as jar 我从这里列出的答案中寻求帮助: 以jar运行时找不到类路径资源
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.