Home > Back-end >  Property not recognized in Thymeleaf
Property not recognized in Thymeleaf

Time:11-03

I want to create a link in Thymeleaf to route the user to an edit page. My Java class looks like this(without getters and setters):


@Entity
@Table(name = "agencies")
public class Agency {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer id;

    @Column(nullable = false)
    private String name;

    @Column(nullable = false, unique = true, length = 50)
    private String email;

    private String address;
}

I also have a controller with this method:

 @Autowired private AgencyService service;

    @GetMapping("/agencies")
    public String showAgencyList(Model model) {
        List<Agency> agencies = service.listAll();
        model.addAttribute("agencies", agencies);

        return "agencies";
    }

And this is a part of the code in Thymeleaf:

 <th:block th:each="agency : ${agencies}">
        <tr>
          <td>[[${agency.name}]]</td>
          <td>[[${agency.email}]]</td>
          <td>[[${agency.address}]]</td>
          <td>
            <a class="h4 mr-3" th:href="@{'/agencies/edit/'   ${agency.id} }">Edit</a>
            <a class="h4" th:href="@{/agents}">Choose</a>
          </td>
        </tr>
      </th:block>

Now everything works fine, except that in the path th:href="@{'/agencies/edit/' ${agency.id} } it says cannot resovle agency, cannot resolve id. Why is that? The table is correctly displayed and I can't figure out why agency seems unknown when providing the path to another view in Thymeleaf.

CodePudding user response:

Whitespace between ${agency.id} } create problem.

Change

th:href="@{'/agencies/edit/'   ${agency.id} }"

To

th:href="@{'/agencies/edit/'   ${agency.id}}"

OR

th:href="@{'/agencies/edit/{id}(id = ${agency.id})'}"
  • Related