I have a controller that returns a ReponseEntity<byte[]> image and I can show it with the following tag:
<img th:src="#{'./my-files/main-logo'}"
I can also show an image from the /img/ folder with this tag:
<img th:src="#{/img/default-logo.png}"
I want to show the image from the database when it is present, and the image from the folder as a default if the ResponseEntity is null.
I tried all kind of ternary conditions but none of them worked.
Any suggestions?
Try using the attribute th:if="${...}" in your <img /> tag.
You can try to call the method which returns the image programatically (e.g. nameOfTheImageController.getImage(...)) and add a boolean to your model that represents whether the image is null or not.
Then, you can just add that boolean variable in the <img>'s th:if.
UPDATE
As mentioned in a comment in
HTML, Thymeleaf - displaying an image conditionally, you should wrap your <img> tag inside a <div> and put the th:if attribute in the <div> tag instead.
In my opinion, instead of changing the src of the image, you should change your /my-files/main-logo controller to return img/default-logo.png as a the ResponseEntity<byte[]> if there is no main-logo. That way you can just leave your image tag as is:
<img th:src="#{'./my-files/main-logo'}" />
If you don't want to do that, then yes, you'll have to add something to your controller that tells you whether or not you have a main logo. Something like this for example:
<img th:if="${has_logo}" th:src="#{'./my-files/main-logo'}" />
<img th:unless="${has_logo}" th:src="#{/img/default-logo.png}" />
Please use the following syntax. in your controller add a model attribute hasImageInDatabase when response is null. then from thymeleaf use the below syntax.
<img th:src="${(hasImageInDatabase ?: '/my-files/main-logo': '/img/default-logo.png'}"/>
This will work for you:
<img th:src="./my-files/main-logo" onerror="this.onerror=null;this.src='/img/default-logo.png';">
It means if the first link to the photo didn't work ("./my-files/main-logo" ) or was null, it will show the second photo('/img/default-logo.png').
Related
I want to put one condition in thymeleaf, If my object contain url so i want to print anchor tag with Url so i can open it and if not, then a message should be display.
<span th:utext="${#strings.contains({resultModel.results},'s3')} ? '<a target="_blank" href="${resultModel.results}" >URL</a>' : ${resultModel.results}"></span>
I want to get URL as a java object in href. Please suggest href="${resultModel.results}"
If i use href="http://google.co.in" so it is working but while using href="${resultModel.results}" i am not getting value.
Note: In Above html code else condition is working and getting message as a results.
I wouldn't try and combine that logic... Avoid putting html in html by just splitting the tags out the inner html into its own tags.
<th:block th:with="condition=${#strings.contains(resultModel.results, 's3')}">
<a th:if="${condition}" target="blank" th:href="${resultModel.results}">URL</a>
<span th:unless="${condition}" th:text="${resultModel.results}" />
</th:block>
I have persisted entity with field text. Inside text I have part to replace. myEntity.text:
some text
twoImages[12_v13.PNG, 10_v6.PNG]
text text text
twoImages[12_v13.PNG, 10_v6.PNG]
<h1>And HTML</h1>
So in view I want to use something like th:utext. But with replaced images blocks with th:fragment (~15 lines per fragment).
Output should be like this:
some text
<img .. 12_v13.PNG />
<br />
<img .. 12_v13.PNG />
<additional html />
text text text
<img .. 12_v13.PNG />
<br />
<img .. 12_v13.PNG />
<additional html />
<h1>And HTML</h1>
How to realize this with Thymeleaf?
If the entire text of the field is a single string, you will have to parse it using regex matcher or some generated parser. Say [12_v13.PNG, 10_v6.PNG] has to be parsed e.g. with
\\[([^,]),\\s([^\\]]*)]
The first group will give 12_v13.PNG, the second one - 10_v6.PNG.
You need to provide the path to each of the images in src property of img tag.
You can achieve the result this way:
<img th:src="#{/images/test.png}" />
It is implied, that /images folder is within webapp folder of your project.
<img th:src="#{/resources/images/Picture.png}" />
Output as:
<img src="/resources/image/Picture.png" />
When you hit http://localhost:8080/myapp/resources/images/Picture.png in you browser then you should be able to access the image for the above syntax to work.
This link is useful: Standard URL Syntax
Please have a look at my thymeleaf demo project: demo project (the folder with templates will get opened) You'll find data on project structure and examples of thymeleaf templates.
<a href="printAllocation.action?event=printAllocation&fundAlias=%{fundAlias}&fundName=%{fundName}&lendFromMonth=%{lendFromMonth}&lendFromYear=%{lendFromYear}&partListDate=%{partListDate}&partListType=%{partListType}&secLendIncome=%{secLendIncome}"
onClick="NewWindow(this.href,'Warning','1000','800','Yes');return false;">
<s:submit value="Print" cssClass="btnRedApple">
</s:submit></a>
Hi! in the above code I'm trying to pass the values from the current jsp to new window by using anchor tag for the button "print" but in new window I'm not getting the values.
This is done for the struts2 migration so can any one please help me in resolving this issue. how can I pass the value to the new window
Looks like you have a URL with many parameters. What makes it worse is including the URL in anchor tag. I suggest you use s:url of stuts 2.
Struts 2 “url” tag is used to create an URL and output it as a text
format. It’s never work by itself, but it can provides URL to other
tags like to create a hyperlink or to render an image
<s:url action="printAllocation.action" var="urlTagValue" >
<s:param name="event">printAllocation</s:param>
<s:param name="fundAlias" value="%{fundAlias}"></s:param>
<s:param name="fundName" value="%{fundName}"></s:param>
...//all the other parameters
</s:url>
this essentailly generates output in text format like below.
/theDomainOfYourWebApp/printAllocation.action?event=printAllocation&fundAlias=...
Add then use it your anchor tag
<a href="<s:property value="#urlTagValue" />" onClick="NewWindow(this.href,'Warning','1000','800','Yes');return false;>URL Tag Action (via property)</a>
I have a line of code like this in the jsp:
<button name="CurrentDelete" value="${ra_split}" type="submit">Delete</button>
And in my Controller I use:
#RequestParam String CurrentDelete
I am trying to pass the value of ${ra_split} into the Controller when I hit the Delete button, but all I am getting is the value of the text 'Delete' instead. Why is that?
Here's the explanation
If you use the element in an HTML form, Internet Explorer, prior version 8, will submit the text between the and tags, while the other browsers will submit the content of the value attribute.
By returning to this issue after a few days I figured out a solution.
Just use:
<input type="hidden" value="${ra_split}" name="CurrentDelete">
<input type="submit" value="Delete" />
instead of:
<button name="CurrentDelete" value="${ra_split}" type="submit">Delete</button>
Then the problem will be solved and the String CurrentDelete will contain the value ${ra_split} instead of the text 'Delete'.
Extra information I have got when trying to solve the problem:
The button tag:
<button name="CurrentDelete" value="${ra_split}" type="submit">Delete</button>
Will always pass the value between the button tags to the Controller (in this case the text 'Delete') instead of passing the value="${ra_split}".
Either using
HttpServletRequest req
in the Controller and then do:
String CurrentDelete = req.getParameter("CurrentDelete");
or using
#RequestParam String CurrentDelete
in the Controller,
would both get the same result.
I have started using struts .I have hanged in a place ,Code is bellow
<st:submit src="getText('image.user.login')" type="image" height="21" width="44" </st:submit>
when i run this code , getText('image.user.login') message does return any value , But when i replace src="getText('image.user.login')" with value="getText('image.user.login')" than it returns value of "image.user.login" from property file.
What is reason for it , and how can i solve this issue ?
Thanks in advance
like this example illustrates in this reference submit reference struts
Render an image submit:
<s:submit type="image" value="%{'Submit'}" label="Submit the form" src="submit.gif"/>
src : Supply an image src for image type submit button. Will have no effect for types input and button.
AND
value : String Preset the value of input element.
this should answer your question if you use src you should assign a path and if you use value you can use a preset value
try this:
<st:submit key="image.user.login"/>