-1

How to iterate a Array and build a url ?

I have a Array like below ticketList and need a ResultList as below with a hyperlink attached

ticketList = [JR8908,JR7676,JR7687,JR8798]

I want the result ResultList : [ JR8908,JR7676,JR7687,JR8798]

Hyperlink would be - http://localhost:8080/browse/ticketID

    <ul th:each="ticket: ${ticketList}">
    <li th:href="http://localhost:8080/browse/${ticket}" th:text="${ticket}"></li>
    </ul>
pruthvi
  • 43
  • 5
  • It looks like this is a question about how to build URLs and not a question about Thymeleaf iteration. Take a look at the [Standard URL Syntax](https://www.thymeleaf.org/doc/articles/standardurlsyntax.html) guide - especially section 5 _Adding parameters_. There are also many questions and answers on SO which may help, with some research. For example: [Thymeleaf construct URL with variable](https://stackoverflow.com/questions/14938344/thymeleaf-construct-url-with-variable) may be helpful. (You could even just use string concatenation, in this specific case, if you wanted.) – andrewJames Aug 26 '22 at 18:23
  • @andrewJames It is both iterate over the list and add link to those – pruthvi Aug 29 '22 at 05:43
  • @andrewJames Can you check now ? – pruthvi Aug 29 '22 at 11:51
  • Your iteration works correctly for me. That is not the problem. So this is not a question about how to iterate. Your URL syntax is incorrect. That is why I pointed you at the two links I gave in my first comment. Those links will solve the problem you are showing us in your code. – andrewJames Aug 29 '22 at 12:33
  • Apart from that, when you say "_I have a list..._", what you are showing us is actually an array not a list, but that does not matter in this case. As long as you are handling that array (or list) correctly in your Java code and passing that array (or list) to the Thymeleaf renderer correctly. Unfortunately, you have not shown us that part of your code. You have also not shown us what actually happens when you run your code, and you have not shown us what errors (if any) you are getting. – andrewJames Aug 29 '22 at 12:33
  • @andrewJames It is not working for me when I give href, Thanks I will correct the naming – pruthvi Aug 29 '22 at 12:35
  • "_It is not working for me when I give href_" - yes, you are correct. Your code is broken. You are using the wrong syntax. So, again, that is why I gave you two links in my first comment. Those links show you how you need to fix your code. – andrewJames Aug 29 '22 at 12:38

1 Answers1

0

Below code worked for me, hope this helps someone :)

<ul>
<li th:each="ticket : ${ticketList}">
<a th:text="${ticket}" th:href="@{'http://localhost:8080'+${ticket}}"></a>
</li>
</ul>
pruthvi
  • 43
  • 5