How to create a link from two parts in thymeleaf?

旧时模样 提交于 2021-02-11 14:16:49

问题


I am newbie in thymeleaf. I need to create links that consists of the path to controller(class PersonController) and id of object, which I take from list. I want that eachPerson object from list have its own link like this: href=" /personData/person.id'. It's my code, which give me an error org.thymeleaf.exceptions.TemplateInputException: An error happened during template parsing:

<div th:each="person : ${persons}" th:with="hrefToPerson=${/personData/ + ${person.id}}">
    <a th:href=hrefToPerson>Edit</a>
        <p th:text="${person.id} + ' ' + ${person.getLastName()} + ' ' + ${person.getFirstName()} + ' ' + ${person.getPatronymic()}"/>

    <p>Phones:</p>
    <ol>
        <li th:each="phone: ${person.getPhoneNumbers()}" th:text="${phone.getType()} + ' ' + ${phone.getNumber()}"></li>
    </ol>
    <p>Address: </p>
    <ol>
        <li th:each="address:${person.addresses}" th:text="${address.getZipCode()} + ', ' + ${address.getCountry()} +
', ' + ${address.getRegion()} + ', ' + ${address.getCity()} + ', ' + ${address.getAddressLine2()} + ' ' + ${address.getAddressLine1()}"></li>
    </ol>
</div>

Controller:

@Controller
@RequestMapping("/personData")
public class PersonController {
    @Autowired
    private PersonRepository personRepo;


    @GetMapping("{person}")
    public String personEditForm(@PathVariable Person person, Model model) {
        model.addAttribute("person", person);
        return "personEdit";
    }

    @PostMapping
    public String personSave(@RequestParam Person person, Model model) {

        return "personEdit";
    }
}

class Person:

@Entity
@NamedQuery(name = "Person.findByPhone",
        query = "select p from Person as p join p.phoneNumbers as pn where pn.number = :number")
@Table(uniqueConstraints={
        @UniqueConstraint(columnNames = {"lastName", "firstName", "patronymic"})
})
public class Person {

    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    private Integer id;

    private String lastName;

    @NotNull
    private String firstName;

    private String patronymic;

    @ElementCollection(fetch = FetchType.EAGER)
    @CollectionTable(name = "person_phone_numbers", joinColumns = @JoinColumn(name = "person_id"))
    private Set<PhoneNumber> phoneNumbers = new HashSet<>();

    @ElementCollection(fetch = FetchType.EAGER)
    @CollectionTable(name = "person_addresses", joinColumns = @JoinColumn(name = "person_id"))
    @AttributeOverrides({
            @AttributeOverride(name = "addressLine1", column = @Column(name = "house_number")),
            @AttributeOverride(name = "addressLine2", column = @Column(name = "street"))
    })
    private Set<Address> addresses = new HashSet<>();

    public Person() {
    }

    public Person(String lastName, String firstName, String patronymic,
                  Set<PhoneNumber> phoneNumbers, Set<Address> addresses) {
        this.lastName = lastName;
        this.firstName = firstName;
        this.patronymic = patronymic;
        this.phoneNumbers = phoneNumbers;
        this.addresses = addresses;

    }

getters and setters... }


回答1:


It should be structured like this:

<div th:each="person: ${persons}">
    <a th:href="@{/personData/{id}(id=${person.id})}">Edit</a>

See the standard url syntax. You could do the same expression with th:with, but I don't see any reason you'd want to do that unless you are reusing the url.

<div th:each="person: ${persons}" th:with="hrefToPerson=@{/personData/{id}(id=${person.id})}">
    <a th:href="${hrefToPerson}">Edit</a>



回答2:


You are using a wrong thymeleaf syntax in the first line. This might work:

<div th:each="person : ${persons}">
<a th:href="@{/personData/{id}(id=${person.id})}">Edit</a>

For building URLs the @{...} Expression is used. With ${...} you can display model attributes.



来源:https://stackoverflow.com/questions/51769571/how-to-create-a-link-from-two-parts-in-thymeleaf

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!