-1

I have a form with some attribute related to an object. In this object I have a date object and in my form I recover it like a string. Is it possible to fill the date object by the string ?

JSP form declaration :

<form:form method="POST" action="/netmg/controller/device/search/" modelAttribute="device"> 

JSP with string value :

<tr>
                <td class="label"><spring:message code="device.endDate" /></td>
                <td class="value">
                    <form:input path="endDate" cssClass="datepickerMe" />
                    <form:errors path="endDate" cssClass="errormsg" />
                </td> 
            </tr>

Then I recover it like this in my controller :

/**
 *
 * @param device
 * @param bindingResult
 * @param uiModel
 * @return
 */
@RequestMapping(method = RequestMethod.POST)
public String findDevicesByCriteria(@Valid @ModelAttribute Device device, BindingResult bindingResult, Model uiModel) {
    if (isCriteriaEmpty(device)) {
        uiModel.addAttribute("criteriaEmptyWarning", "error_search_criteria_empty");
        return ViewConstants.DEVICE_SEARCH_VIEW;
    }
    List<IDevice> deviceList = identityService.searchDevices(device.getSerialNumber(), device.getOwner(), device.getIpAdress(), device.getInstallDate(), device.getEndDate());
    if (deviceList.size() == 0) {
        uiModel.addAttribute(WebConstants.NO_RESULT, true);
    }
    uiModel.addAttribute(WebConstants.DEVICE_LIST, deviceList);
    return ViewConstants.DEVICE_SEARCH_VIEW;
}

Do you have any idea to fill the date object attribute ?

2 Answers2

0

If the value is a date ,you could use the "formatDate" function from JSTL.
You have to add the taglib in this way.

<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>


And finally add a format for the value , similar to this...

<fmt:formatDate value="${ojbect.date}" pattern="dd/MM/yyyy" />


I hope it's useful to you.
Regards.

0

In the POSTController you can parse the String (date) into a Date object, something like this: Parse String to Date or you can set into the JSP input as a Date.

 <form:input type="datetime-local" path="endDate" cssClass="datepickerMe" />
Community
  • 1
  • 1
Molinetas
  • 56
  • 4