Thymeleaf gets all images from folders - java

is it possible to gets all images from folders?
I mean I've images structure like this
folder1 ---
---image1.jpg
---image2.jpg
---image3.jpg
folder2 ---
---image1.jpg
---image2.jpg
how can I display all images in html?
#edit
only my tries:
<p th:each="i: ${#numbers.sequence(1, 5)}">
<img th:src="#{/folder/001/0.jpg}"/>
</p>

There's nothing built-in to Thymeleaf to read a folder and get a list of files in it. If you're trying to build something dynamically (which I'm not sure from your question if that's what you're trying to do), then you'd need to read it in your controller (whatever Java code is putting together the context variable map or model that you're sending to Thymeleaf).
If you're just trying to do a static list of five numbered images, like your example code, then you need to do concatenation of your "each" variable inside an expression to generate your image URL, like this:
<p th:each="i: ${#numbers.sequence(1, 5)}">
<img th:src="#{${'/folder/001/' + i + '.jpg'}}"/>
</p>
That will generate the following HTML:
<p>
<img src="/context/folder/001/1.jpg" />
</p>
<p>
<img src="/context/folder/001/2.jpg" />
</p>
<p>
<img src="/context/folder/001/3.jpg" />
</p>
<p>
<img src="/context/folder/001/4.jpg" />
</p>
<p>
<img src="/context/folder/001/5.jpg" />
</p>
There are more examples of creating Link URLs in the documentation. These show how you can use an expression to construct the base URL or to construct any query arguments (though usually you don't use query arguments when loading images).

Related

How can I get the URL of a file on the users' computer when using <input type = "file">?

I'm a college student and I'm currently working on a video library in JSP. I'm working on the part where the user inserts their videos, for this I need to save the URL of the video, the path, in the code the, <input type=file>, but when the user would insert a file, I can't get the URL at all, only the name of the file. I know that for security reasons, the browser can't access the users' files, but I'd like to save the URL in the database, not just the file name and type. Is there a work-around to this whole thing?
This is my code for the insert video Java Server Page:
<form method="post" action =<%= string1 %>>
<h1>Insira o vídeo</h1>
<p>
<label>Nome do vídeo</label> <input
id="nome_vid" name="vid_nome" required="required"
type="text" placeholder="Vídeo fixolas" />
</p>
<p>
<label>Vídeo</label><input id="btnExplore" name="btn_file" type="file" />
</p>
<p>
<label>Thumbnail</label><input id="btnExplore2" name="btn_file2" type="file" />
</p>
<p>
<input type="submit" value="Inserir" name="submeter" onclick = "validalogin()"/>
</p>
<p class="link">
<a href=<%= string2 %>>Principal</a>
</a>
</p>
</form>
You can create a folder to save those files in your project folder, so it will not facing security issues. The other way is you hardcoded the front URL + filenames. For example, "C:\Documents" + filenames as you can get the filename.

Thymeleaf Templating th:data=http://www.somesite.com || org.thymeleaf.exceptions.TemplateProcessingException: Could not parse as assignation sequence

Here is the template.
After I run the process function from the TemplateEngine, I receive this error org.thymeleaf.exceptions.TemplateProcessingException: Could not parse as assignation sequence: "data=http://www.somesite.com" (template: "someFile.html" - line 5, col 2)
I've tried doing it a handful of different ways and keep getting the same result.
<div class="someClass" th:id="${divId}">
<object class="someClass" type="application/pdf"
th:data="'|${url}|'">
<p>You don't have a PDF plugin for this browser.
<a th:href="#{|${url}|}">
Click here to download PDF file.
</a>
</p>
</object>
</div>
You're tyring to pass a string to th: when it expect a expression.
Change this:
th:data="'|${url}|'"
To this:
th:data=${url}
Remember to change the other elements that are also as string.
You don't need most of the | characters (as using literal substitutions don't make a difference here. The code you posted should look like this:
<div class="someClass" th:id="${divId}">
<object class="someClass" type="application/pdf" th:data="${url}">
<p>
You don't have a PDF plugin for this browser.
<a th:href="${url}">Click here to download PDF file.</a>
</p>
</object>
</div>
That being said, the error you indicated usually comes with using th:with incorrectly, and since I don't see that anywhere in the code you've given us, I doubt you have located the root cause of the problem.

Thymeleaf / Spring. get property key from ModelMap

I have a basic SpringBoot app. using Spring Initializer, embedded Tomcat, Thymeleaf template engine, and package as an executable JAR file.
in my property file:
signup.form.error.email.already.exists=Email (already taken)
in my controller:
protected static final String ERROR_MESSAGE_KEY = "errorMessageKey";
model.addAttribute(ERROR_MESSAGE_KEY, "signup.form.error.email.already.exists");
in the template:
<div th:if="${errorMessageKey != null}" class="alert alert-danger alert-dismissible" role="alert">
<button type="button" class="close" data-dismiss="alert" aria-label="Close">
<span aria-hidden="true">x</span>
</button>
<p th:text="#{errorMessageKey}" />
</div>
But this is what I see inthe template instead of Email (already taken)
??errorMessageKey_en_US??
Try using
<p th:text="#{signup.form.error.email.already.exists}" />
When you code this:
model.addAttribute(ERROR_MESSAGE_KEY, "signup.form.error.email.already.exists");
Spring is not resolving the message from the message.properties file: you are simply putting the string "signup.form.error.email.already.exists" in the model...
NOTE the expression #{errorMessageKey} is returning the name of the source file containing the specified message.
Putting properties to Model is not how you resolve the properties in Thymeleaf. See the docs where they explain how to do it properly.
It is because of Thymeleaf is trying to fetch locale message. And by default it is trying en_US.
You can look at this answer

Get link from anchor tag using Java

I'm trying to get value of a href attribute from an anchor tag (a tag) using Java, without a third party API. I know the class of the label. The website looks like this:
<html>
<body>
<div id="uix_wrapper">
<div id="button">
<label class="downloadButton">
Button
</label>
</div>
</div>
</body>
</html>
You can use a regular expression if you try to avoid using any third-party libraries. A very basic expression for your example is
<a href="(.*?)">
and should work. You can try out yourself using https://www.debuggex.com/
The result will be in the second group.

How do I fill a PDF with content from an HTML document via JSP

I have the following problem:
I need to write the content of an HTML form into a PDF file when the submit-button is pressed, using JSP.
Now let's say my HTML looks like this:
<html>
<body>
<form action="PDFTest.jsp" method="POST" role="form" name="TestForm">
Name <input name="inp_name" />
First name <input name="inp_firstname" />
Street <input name="inp_street" />
Zip-code <input name="inp_zip" />
<button type="submit">Submit</button>
</form>
</body>
</html>
The PDF file has also declared fields with the identifiers "inp_name, inp_firstname, inp_street, inp_zip" (I created a form in LibreOffice, added Textfields, named them with the identifiers and exported the form to PDF).
Is it possible to take the input elements by their Name-Tags and copy the content of these elements into the fields of the PDF file? And if it is, how do I do it?
I heard, that something like this could be done with iText, but I never used it before.
Additionally, is it possible to create a copy of the original PDF file in the JSP so that the copy will be filled with the content and the original only serves as a template?
Thanks in advance for your answers/criticism.
Flying Saucer can render HTML to PDF with iText. Here's an example showing how that's done.
You will have to replace the strings in the HTML document prior to conversion.

Categories