Как установить поле ввода для агрегированного объекта в thymleaf с помощью весенней загрузки - PullRequest
0 голосов
/ 18 марта 2019

Я пытаюсь установить поля ввода для агрегированного объекта в thymleaf с помощью Spring Boot.Моя модель

public class DearHelpUsers {

    private String id;
    private String username;
    private String email;
    private String password;
    private String cnfPassword;
    private String role;
    private String phoneNumber;
    private UserAddress address;

    //setters getters
}

Я объединяю класс UserAddress в классе DearHelpUsers

class UserAddress{

    private String streetName;
    private String city;
    private String zipCode;
    private String state;
    private float lattitude;
    private float longitude;

    //setters and getters
}

Моя форма из листьев тимьяна - "regfuser.html"

<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <title>Getting Started: Handling Form Submission</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
	<h1>Form</h1>
    <form action="#" th:action="@{/register}" th:object="${user}" method="post">
    	<p>Username: <input type="text" th:field="*{userName}" /></p>
        <p>Password: <input type="text" th:field="*{password}" /></p>
        <p>Confirm Password: <input type="text" th:field="*{cnfPassword}" /></p>
        <!-- other fields-->
        <p>Street: <input type="text" th:field="*{}" /></p>
        <p>City: <input type="text" th:field="*{}" /></p>
        <p>Zip: <input type="text" th:field="*{}" /></p>
        <p>State: <input type="text" th:field="*{}" /></p>
        <p><input type="submit" value="Submit" /> <input type="reset" value="Reset" /></p>
    </form>
</body>
</html>

Мой контроллер -

@Controller
public class DearHelpUsersController {

@Autowired
private DearHelpUsersRepo userRepo;

@GetMapping("/regfuser")
public String regUserForm( Model model) {
    model.addAttribute("user" new DearHelpUsers());
    return "regfuser";
}

Я передал объект " user " из моего контроллера в эту форму.Как установить поля для атрибутов UserAddress в моей форме?Надеюсь, кто-нибудь поможет мне в ближайшее время.

1 Ответ

0 голосов
/ 18 марта 2019

Вы можете использовать:

<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <title>Getting Started: Handling Form Submission</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
    <h1>Form</h1>
    <form action="#" th:action="@{/register}" th:object="${user}" method="post">
        <p>Username: <input type="text" th:field="*{userName}" /></p>
        <p>Password: <input type="text" th:field="*{password}" /></p>
        <p>Confirm Password: <input type="text" th:field="*{cnfPassword}" /></p>
        <!-- other fields-->
        <p>Street: <input type="text" th:field="*{address.streetName}" /></p>
        <p>City: <input type="text" th:field="*{address.city}" /></p>
        <p>Zip: <input type="text" th:field="*{address.zipCode}" /></p>
        <p>State: <input type="text" th:field="*{address.state}" /></p>
        <p><input type="submit" value="Submit" /> <input type="reset" value="Reset" /></p>
    </form>
</body>
</html>
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...