如何从 thymeleaf 的两部分创建链接? [英] How to create a link from two parts in thymeleaf?

查看:23
本文介绍了如何从 thymeleaf 的两部分创建链接?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 thymeleaf 的新手.我需要创建由控制器路径(PersonController 类)和对象 id 组成的链接,我从列表中获取.我希望列表中的每个人对象都有自己的链接,如下所示:href="/personData/person.id'.这是我的代码,它给了我一个错误 org.thymeleaf.exceptions.TemplateInputException: An error occurred during template parsing:

<div th:each="person : ${persons}" th:with="hrefToPerson=${/personData/+ ${person.id}}"><a th:href=hrefToPerson>编辑</a><p th:text="${person.id} + ' ' + ${person.getLastName()} + ' ' + ${person.getFirstName()} + ' ' + ${person.getPatronymic()}"/><p>电话:</p><ol><li th:each="phone: ${person.getPhoneNumbers()}" th:text="${phone.getType()} + ' ' + ${phone.getNumber()}"></里></ol><p>地址:</p><ol><li th:each="address:${person.addresses}" th:text="${address.getZipCode()} + ', ' + ${address.getCountry()} +', ' + ${address.getRegion()} + ', ' + ${address.getCity()} + ', ' + ${address.getAddressLine2()} + ' ' + ${address.getAddressLine1()}"></li></ol>

控制器:

@Controller@RequestMapping("/personData")公共类 PersonController {@自动连线私有 PersonRepository personRepo;@GetMapping("{person}")public String personEditForm(@PathVariable Person person, Model model) {model.addAttribute("person", person);return "personEdit";}@PostMappingpublic String personSave(@RequestParam Person person, Model model) {return "personEdit";}}

<小时>

类人:

@Entity@NamedQuery(name = "Person.findByPhone",query = "select p from Person as p join p.phoneNumbers as pn where pn.number = :number")@Table(唯一约束={@UniqueConstraint(columnNames = {"lastName", "firstName", "patronymic"})})公共类人{@ID@GeneratedValue(strategy=GenerationType.AUTO)私有整数 ID;私人字符串姓氏;@NotNull私人字符串名字;private String 父名;@ElementCollection(fetch = FetchType.EAGER)@CollectionTable(name = "person_phone_numbers", joinColumns = @JoinColumn(name = "person_id"))私人设置<电话号码>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"))})私有集<地址>地址 = 新的 HashSet<>();公共人(){}公共人(字符串姓氏,字符串名字,字符串父名,设置<电话号码>电话号码,设置<地址>地址){this.lastName = 姓氏;this.firstName = firstName;this.patronymic = 父名;this.phoneNumbers = phoneNumbers;this.addresses = 地址;}

getter 和 setter...}

解决方案

它的结构应该是这样的:

<a th:href="@{/personData/{id}(id=${person.id})}">编辑</a>

请参阅标准网址语法.你可以用 th:with 做同样的表达,但我不认为你有任何理由想要这样做,除非你重复使用 url.

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

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... }

解决方案

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>

这篇关于如何从 thymeleaf 的两部分创建链接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆