如何从百里香中的两个部分创建链接?

时间:2018-08-09 14:26:11

标签: java spring-mvc thymeleaf

我是百里香的新手。 我需要创建一个链接,该链接包括从列表中获取的controller(class PersonController)类的路径和object ID。我希望列表中的每个对象具有自己的链接,例如:href =“ /personData/person.id'。 这是我的代码,给我一个错误org.thymeleaf.exceptions.TemplateInputException:模板解析期间发生错误:

<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
@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";
    }
}

班级人员:

@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;

    }

获取器和设置器... }

2 个答案:

答案 0 :(得分:1)

它的结构应如下:

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

请参见standard url syntax。您可以使用th:with进行相同的表达式,但是除非您重新使用url,否则我看不出您要这样做的任何原因。

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

答案 1 :(得分:0)

您在第一行中使用了错误的百里香语法。这可能有效:

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

对于构建URL,使用@ {...}表达式。使用$ {...},您可以显示模型属性。