Java 在JSP中将日期字符串转换为日期对象
我有一个表单,它有一些与对象相关的属性。 在这个对象中,我有一个日期对象,在我的表单中,我像字符串一样恢复它。 可以用字符串填充日期对象吗 JSP表单声明:Java 在JSP中将日期字符串转换为日期对象,java,jsp,date,jakarta-ee,Java,Jsp,Date,Jakarta Ee,我有一个表单,它有一些与对象相关的属性。 在这个对象中,我有一个日期对象,在我的表单中,我像字符串一样恢复它。 可以用字符串填充日期对象吗 JSP表单声明: <form:form method="POST" action="/netmg/controller/device/search/" modelAttribute="device"> 具有字符串值的JSP: <tr> <td class="label"><sp
<form:form method="POST" action="/netmg/controller/device/search/" modelAttribute="device">
具有字符串值的JSP:
<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>
然后我在控制器中恢复它,如下所示:
/**
*
* @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;
}
/**
*
*@param设备
*@param bindingResult
*@param-uiModel
*@返回
*/
@RequestMapping(method=RequestMethod.POST)
公共字符串FindDeviceByCriteria(@Valid@ModelAttribute设备、BindingResult BindingResult、Model uiModel){
if(isCriteriaEmpty(设备)){
uiModel.addAttribute(“criteriaEmptyWarning”,“错误搜索\标准\空”);
返回viewstants.DEVICE\u SEARCH\u视图;
}
列出deviceList=identityService.searchDevices(device.getSerialNumber()、device.getOwner()、device.getiPAddress()、device.getInstallDate()、device.getEndDate());
如果(deviceList.size()==0){
uiModel.addAttribute(WebConstants.NO_结果,true);
}
uiModel.addAttribute(WebConstants.DEVICE_列表,设备列表);
返回viewstants.DEVICE\u SEARCH\u视图;
}
您知道如何填充date对象属性吗?如果该值是日期,您可以使用JSTL中的“formatDate”函数。
您必须以这种方式添加标记库。
最后为值添加一个格式,类似于下面的。。。
我希望它对你有用。
注意。在POSTController中,您可以将字符串(日期)解析为日期对象,如下所示:或者您可以在JSP输入中设置为日期
<form:input type="datetime-local" path="endDate" cssClass="datepickerMe" />
@Amarcarilla谢谢您的回复,我只需将我的表格:输入替换为?
<fmt:formatDate value="${ojbect.date}" pattern="dd/MM/yyyy" />
<form:input type="datetime-local" path="endDate" cssClass="datepickerMe" />