JSP form date input field

I created a basic multi-line input form using the Spring web app in Intellij. The form successfully saves the backend when using strings only, so I decided to add a date field to the model and tried to change the / jsp controller to accept this in the input form (and display it in the list of entries). I am having problems with an input form that does not receive a value.

Entity:

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

public Date getDueDate() {
    return dueDate;
}

public void setDueDate(Date dueDate) {
    this.dueDate = dueDate;
}

JSP (I assume the value should be empty here, since I start by filling in an empty field?):

    <div class="control-group">
        <form:label cssClass="control-label" path="dueDate">Due Date:</form:label>
        <div class="controls">
            <input type="text" path="dueDate" class= "date" name = "dueDate" value = "<fmt:formatDate value="" pattern="MM-dd-yyyy" />"/>
        </div>
    </div>

Controller:

@RequestMapping(value = "/todos/add", method = RequestMethod.POST)
public String addUser(@ModelAttribute("todo") Todo todo, BindingResult result) {
    System.err.println("Title:"+todo.getTitle());
    System.err.println("Due Date:"+todo.getDueDate());
    todoRepository.save(todo);
    return "redirect:/todos/";
}

My debug file shows the runtime: null, so nothing is sent for my date field from the form when posting. This means that the date field is never saved, and then the repository is saved.

+4
5

InitBinder , spring java.util.Date . :

 @InitBinder
    public void initBinder(WebDataBinder binder) {
        SimpleDateFormat sdf = new SimpleDateFormat("MM-dd-yyyy");
        sdf.setLenient(true);
        binder.registerCustomEditor(Date.class, new CustomDateEditor(sdf, true));
    }

jsp :

<input type="text" path="dueDate" class= "date" name = "dueDate" value = "<fmt:formatDate value="${cForm.dueDate}" pattern="MM-dd-yyyy" />"/>
+11

, DateTimeFormat, spring

import org.springframework.format.annotation.DateTimeFormat;
import java.util.Date;

@DateTimeFormat(pattern = "MM-dd-yyyy")
private Date birthdate;

jsp

<form:input path="birthdate" cssClass="form-control" />

: dd-mm-yy, .. 27-10-2017, , .

+2

spring input like:

<div class="control-group">
   <form:label cssClass="control-label" path="dueDate">Due Date:</form:label>
    <div class="controls">
      <form:input path="dueDate" class="date" />
    </div>
</div>

dueDate :

value="<fmt:formatDate value="" pattern="MM-dd-yyyy" />"

, .

+1

, , ( ). jsp:

   <div class="control-group">
        <form:label cssClass="control-label" path="dueDate">Due Date:</form:label>
        <div class="controls">
            <form:input path="dueDate" class="date" />
        </div>
    </div>

Initbinder, .

( ), for (!).

0

InitBinder: jsp, :

<input type="text" path="dueDate" class= "date" name = "dueDate" value = "<fmt:formatDate value="${cForm.dueDate}" pattern="MM-dd-yyyy" />"/>

.

@DateTimeFormat :

@DateTimeFormat(pattern = "MM-dd-yyyy")

Spring .

, , . , InitBinder.

0
source

Source: https://habr.com/ru/post/1534458/


All Articles