微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

在春季MVC中,双日期绑定没有发生

如何解决在春季MVC中,双日期绑定没有发生

我正在尝试使用Spring Web提交表单,在提交数据时填写了数据后,我得到的金额,地址,createdDate为空。不明白为什么只对这些值不输入控制器。

registerUser.jsp页面

<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form"%>    
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>    
  
       <form:form method="post" action="registerUser" modelattribute="user">    
        <table > 
        <tr>    
          <td>ID : </td>   
          <td><form:input path="id"  /></td>  
         </tr>    
         <tr>    
          <td>First Name : </td>   
          <td><form:input path="firstName"  /></td>  
         </tr> 
          <tr>    
          <td>Last Name : </td>   
          <td><form:input path="lastName" /></td>  
         </tr>     
         <tr>    
          <td>Amount Given :</td>    
          <td><form:input path="amount" /></td>  
         </tr>   
         <tr>    
          <td>Phone :</td>    
          <td><form:input path="phone" /></td>  
         </tr> 
         <tr>    
          <td>Address :</td>    
          <td><form:input path="address" /></td>  
         </tr>  
         <tr>    
          <td>Account Created Date :</td>    
          <td><form:input path="createdDate" type="date"/></td>  
         </tr>     
         <tr>    
          <td colspan="2"><input type="submit" value="Save" /></td>    
         </tr>    
        </table>    
       </form:form>   

控制器:

package com.asr.thandas.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.modelattribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.asr.thandas.entity.User;

@Controller
public class MainController {
    
    @RequestMapping("/")
    public String welcomePage() {
        return "welcome";
    }

    @RequestMapping(value = "/register",method = RequestMethod.GET)
    public String displayLogin(Model model) {
        model.addAttribute("user",new User());
        return "registerUser";
    }
    
    @RequestMapping(value = "/registerUser",method = RequestMethod.POST)
    public String displayLogin(@modelattribute("user")User user,BindingResult result) {
        System.out.println(user);
        return "registerSuccess";
    }

}

实体或模型

package com.asr.thandas.entity;

import java.util.Date;

import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

import org.springframework.format.annotation.DateTimeFormat;

@Entity
public class User {
    @Id
    private Long id;
    private String firstName;
    private String lastName;
    private Double amount;
    private String phone;
    private String address;
    
    // This is for bind Date with @modelattribute
    @Temporal(TemporalType.DATE)
    @DateTimeFormat(pattern = "dd/MM/yyyy") 
    private Date createdDate;

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public Double getAmount() {
        return amount;
    }

    public void setAmount(Double amount) {
        amount = amount;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        address = address;
    }

    public Date getCreatedDate() {
        return createdDate;
    }

    public void setCreatedDate(Date createdDate) {
        this.createdDate = createdDate;
    }

    @Override
    public String toString() {
        return "User [id=" + id + ",firstName=" + firstName + ",lastName=" + lastName + ",amount=" + amount
                + ",phone=" + phone + ",address=" + address + ",createdDate=" + createdDate + "]";
    }

}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。