【问题标题】:Thymeleaf date field is passed as null to controllerThymeleaf 日期字段作为 null 传递给控制器
【发布时间】:2021-06-25 18:14:39
【问题描述】:

我在将输入的日期值从表单传递到控制器时遇到问题。所有值都正确传递并存储在数据库中,但日期为空。创建一个 Java 日期对象并手动设置它是可行的,所以我认为问题出在前端格式上。

下面是 thymeleaf 语法​​的 HTML 代码:

    <form th:action="@{/register}" method="post" th:object="${user}" id="userInfo">
    ...
        <h4>Enter Payment Information</h4>
                <div th:object="${card}">
                    <div class="col-12">
                        <label for="cardType" id="cardType"><strong>Card
                            type:</strong></label> <select class="form-select" th:field="*{cardType}">
                        <option th:value="None">Select one:</option>
                        <option th:value="Visa" th:text="Visa"></option>
                        <option th:value="Mastercard" th:text="Mastercard"></option>
                        <option th:value="Discover" th:text="Discover"></option>
                        <option th:value="AmericanExpress" th:text="AmericanExpress"></option>
                    </select>
                    </div>
                    <div class="col-12">
                        <label for="card-number" id="cardNumberLabel"><strong>Card
                            number:</strong></label> <input type="text" th:field="*{cardNumber}" class="form-control"
                                                            id="card-number" name="card-number"
                                                            pattern="^\d{16}$"
                                                            title="Card number must be 16 digits long."
                                                            value="">
                    </div>
                    <div class="col-12 mb-2">
                        <label for="exp" id="expLabel"><strong>Expiration
                            date:</strong></label> <input type="date" th:value="*{expirationDate}" class="form-control" id="exp"
                                                          name="exp"
                                                          value="">
                    </div>
                </div>
    ...
    </form>

这是控制器(为简洁起见,我省略了一些细节,但保留了相关部分):

@GetMapping(value={"/register", "register.html"})
public String displayForm(Model model) {
    // process form data and add user to db
    User user = new User();
    PaymentCard card = new PaymentCard();
    model.addAttribute("user", user);
    model.addAttribute("card", card);
    return "register";
}

@PostMapping(value={"/register", "register.html"})
public String createAccount(@ModelAttribute("user") User user, @ModelAttribute("card") PaymentCard card) {
    User tempUser = new User();
    PaymentCard tempCard = new PaymentCard();
    tempCard.setUser(tempUser);
    tempCard.setPaymentCardId(card.getPaymentCardId());
    tempCard.setCardType(card.getCardType());
    tempCard.setCardNumber(card.getCardNumber());
    //tempCard.setExpirationDate(card.getExpirationDate()); <--- this returns null so I get a NPE
    Date tempDate = new Date(2020, 11, 12); <--- dummy date that works and is saved to db correctly
    tempCard.setExpirationDate(tempDate);
    cardRepo.save(tempCard);

    return "confirmregistration";
}

PaymentCard POJO 的开始:

@Entity
@Table(name = "payment_card", catalog = "bookstore")
public class PaymentCard implements java.io.Serializable {

@Id
@Column(name = "PaymentCardID", unique = true, nullable = false)
@GeneratedValue(strategy=GenerationType.IDENTITY)
private int paymentCardId;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "UserID")
private User user;

@Column(name = "cardNumber", nullable = false)
private String cardNumber; // int and long aren't big enough to hold 16 digits

@Column(name = "cardType", nullable = false, length = 20)
private String cardType;

@Temporal(TemporalType.DATE)
@Column(name = "expirationDate", nullable = false, length = 10)
private Date expirationDate;
...
}

【问题讨论】:

  • 您正在使用 th:value,而您应该在日期字段中使用 th:field
  • 尝试将此card.getExpirationDate() 转换为日期,然后将其传递给您的对象
  • @elgarnaoui 我不确定你的意思。你的意思是在 PostMapping 方法中做类似 tempCard.setExpirationDate((Date) card.getExpirationDate()); 的事情吗?问题是 card.getExpirationDate() 为空。

标签: java html spring spring-mvc thymeleaf


【解决方案1】:

试试这个:

<input type="date" th:field="*{expirationDate}" class="form-control" id="exp" name="exp" value="">

如果这不起作用,请尝试像这样获取单独的字段:

<label for="exp">Expiration date:</label>
<input type="date" id="exp" name="exp" value="2021-02-01" min="2018-01-01" max="2021-12-31">

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-12-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-02
    • 2021-11-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多