如何在SpringMVC @ModelAttribute中使用通过jQuery Datepicker输入的值?

时间:2019-06-02 06:59:48

标签: jquery spring hibernate spring-mvc jsp

.jsp页面上有2个<!DOCTYPE html> <html> <head> <title>Customer Registration Form</title> <style> .error { color: red } </style> <!-- Date picker jquery stuff start --> <link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css"> <link rel="stylesheet" href="/resources/demos/style.css"> <script src="https://code.jquery.com/jquery-1.12.4.js"></script> <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script> <script> $(function() { $('#datepicker').datepicker( { onSelect : function(value, ui) { var today = new Date(); var age = today.getFullYear() - ui.selectedYear; $('#age').text(age); }, maxDate : '+0d', changeMonth : true, changeYear : true, defaultDate : '-18yr', dateFormat : 'dd-mm-yy', }); }); </script> <!-- Date picker jquery stuff end --> </head> <body> <form:form action="processForm1" modelAttribute="customer"> Date of Birth: <form:input type="text" id="datepicker" path="birthdate" placeholder="dd/mm/yyyy" /> <br> <br> <!-- I'm not sure if retrieving age like this from the jQuery calculations work--> Age: <form:input type="text" id="age" path="age" /> <br> <br> <input type="submit" value="Submit" /> </form:form> </body> </html> 字段,

对于字段:一个用于dateOfBirth,一个用于年龄。

用户可以通过jQuery Datepicker脚本选择自己的出生日期,该脚本会自动计算其年龄供以后使用。

我想使用SpringMVC自动设置的计算年龄属性。我不确定如何从jQuery脚本中检索计算出的年龄。

我的.jsp页面

import org.springframework.format.annotation.DateTimeFormat;

import java.util.Date;

import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.validation.constraints.Email;

import com.luv2code.springdemo.mvc.validation.CourseCodeDefinitions;
import com.luv2code.springdemo.mvc.validation.DateConverter;

public class Customer {

    @Temporal(TemporalType.DATE)
    @DateTimeFormat(pattern = "dd-MM-yyyy")
    private Date birthdate;

    private String age; 


    public String getBirthdate() {
        return DateConverter.formatDateToStringFormat(birthdate);
    }
    public void setBirthdate(Date birthdate) {

        System.out.println("Debug: Customer.java setBirthdate()-> birthdate: " +birthdate);
        System.out.println("Debug: Customer.java setBirthdate()-> birthdate type: " + birthdate.getClass().getSimpleName());

        this.birthdate = birthdate;
        System.out.println("Debug: Customer.java setBirthdate()-> Exiting setBirthday()");
    }
    public String getAge() {
        //this keeps printing null      
        return age;
    }
    public void setAge(String age) {
        this.age = age;
    }
}

我的客户实体.java文件

    @RequestMapping("/processForm1")
    public String processForm1(
            @ModelAttribute("customer") Customer theCustomer) {

        System.out.println("Debug: CustomerController.java processForm1() => DoB: " + theCustomer.getBirthdate());
        System.out.println("Debug: CustomerController.java processForm1() => DoB type: " + theCustomer.getBirthdate().getClass().getSimpleName());

                //this keeps printing null :(
        System.out.println("Customer age: " + theCustomer.getAge());

        return "customer-confirmation";

    }

我的控制器类

val set = setOf<String>("one", "two")

从上面的类中可以看到,每当我尝试从getAge()函数中检索用户年龄时,它会一直打印空值,而不是从日期选择器中选择的用户值。将由springMVC通过jQuery datepicker正确设置,以便我能够检索和操作保存在客户对象中的数据?

感谢您的任何帮助!

0 个答案:

没有答案