How to generate a dynamic link within a condition in Thymeleaf?

0

I would like to know how I can generate a link within a condition in Thymeleaf. In my situation, if the condition is not met, I wanted to create a link that would lead to a new form, in which the user could register a Director.

I've been able to perform similar tasks in other situations, but only within the cell tags of the tables:

<td><a href="#" th:href="@{'movie' + '/' + ${movie.id} + '/show'}">Click Here</a></td>

I wanted to know how to do the same in the following example, assigning a dynamic link to the 'Click Here to register' text:

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
   <meta charset="UTF-8">
   <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
   <title>Star Wars Movies</title>
</head>
<body>

   <div class="container">
     <h2 th:text="'Episode ' + ${movie.movieName}"></h2>
     <p>You can add other movies through the StarWarsMoviesBootstrap class.</p>
    <table class="table table-bordered">
      <thead>
       <tr>
        <th>Movie</th>
        <th>Box Office</th>
        <th>Release Date</th>
        <th>Director</th>
        <th>Characters</th>
       </tr>
      </thead>

      <tbody>
        <tr>
          <td th:text="${movie.movieName}">The Force Awakens</td>
          <td th:text="${movie.boxOffice}">200000000</td>
          <td th:text="${movie.releaseDate.toString()}">???</td>
          <td>
            <span th:text="${movie.director != null} ? ${movie.director.directorName} : 'Click here to register'">Some value</span>
          </td>
          <td><a th:href="@{/{movieName}/characters(movieName=${movie.getMovieName()},movieId=${movie.id})}">View</a></td>
        </tr>
      </tbody>
</table>

    
asked by anonymous 06.04.2018 / 21:05

1 answer

1

Good evening, you could use the if to check if it exists and put the td accordingly.

For example:

<tbody>
  <tr>
    <td th:text="${movie.movieName}">The Force Awakens</td>
    <td th:text="${movie.boxOffice}">200000000</td>
    <td th:text="${movie.releaseDate.toString()}">???</td>
    <td th:if="${movie.director != null}">
      <a th:text"'View Movie'" th:href="@{/{movieName}/characters(movieName=${movie.getMovieName()},movieId=${movie.id})}">Ver</a>
    </td>
    <td th:if="${movie.director == null}">
      <a th:text"'View Movie'" th:href="@{/register}">Registrar</a>
    </td>
  </tr>
</tbody>
    
10.04.2018 / 00:17