【问题标题】:Thymeleaf form for class like this:像这样的类的百里香形式:
【发布时间】:2017-04-19 16:04:14
【问题描述】:

我有类客户

    public class Customer {

        private long id;
        private String firstName;
        private String lastName;
        private String pesel;
        private Address address;

        public Customer() {}

        public Customer(String firstName, String lastName, String pesel, Address address) {
            this.firstName = firstName;
            this.lastName = lastName;
            this.address = address;
            this.pesel = pesel;
        }
}

和班级地址

public class Address {

    private String country;
    private String city;
    private String street;
    private String streetNumber;
    private String zipCode;
}

如何从百里香形式输入?我的意思是对于基本类型这已经足够了,但是我怎样才能读取地址类呢?

    <form th:action="@{/addCustomer}" method="post" class="col m8 s12 offset-m2">
        <div class="row">
            <div class="input-field col s6">
                <input id="firstName" type="text"/>
                <label for="firstName">Imię</label>
            </div>

            <div class="input-field col s6">
                <input id="lastName" type="text"/>
                <label for="firstName">Nazwisko</label>
            </div>

            <div class="row">
                <div class="input-field col s6">
                    <input id="pesel" type="text">
                    <label for="pesel">Pesel</label>
                </div>
            </div>
        </div>
    </form>

当然每个类之间的关系都是,基本上类代码是例如我有什么字段

【问题讨论】:

  • 您是否尝试从已填充的类实例中填充 Thymeleaf 页面?或者您是否尝试在控制器的“请求映射”方法中读取表单数据以填充类实例?
  • 嗯,我认为这是第二种选择

标签: java spring thymeleaf


【解决方案1】:

一种方法是将表单的元素声明为请求映射方法的输入参数。比如:

@RequestMapping(value = '/trainInfo/trainDetails', method = RequestMethod.POST, produces='text/html')
public ModelAndView getTrainDetails(HttpServletRequest request, HttpServletResponse response,
@RequestParam(name='username') String username) {

其中'username' 是您要在方法内访问的 HTML 输入元素的名称。

【讨论】:

    猜你喜欢
    • 2018-11-08
    • 1970-01-01
    • 2018-07-08
    • 2021-01-09
    • 2020-12-30
    • 2018-09-07
    • 2013-09-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多