我整天都在尝试将 jQuery datepicker 函数与 java.util.Date 类型的基础 java spring 表单日期字段的两个字段绑定,但出现错误。我正在发布所有文件和错误消息。
form.jspx(jquery 是从瓦片默认模板加载的):
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<jsp:root xmlns:jsp="http://java.sun.com/JSP/Page"
xmlns:c="http://java.sun.com/jsp/jstl/core"
xmlns:spring="http://www.springframework.org/tags"
version="2.0" xmlns:form="http://www.springframework.org/tags/form">
<jsp:directive.page contentType="text/html;charset=UTF-8"/>
<jsp:output omit-xml-declaration="yes"/>
<spring:message code="city_label" var="cityLabel"/>
<spring:message code="state_label" var="stateLabel"/>
<spring:message code="arrivalDate_label" var="arrivalDateLabel"/>
<spring:message code="departureDate_label" var="departureDateLabel"/>
<spring:message code="numberOfAdults_label" var="numberOfAdultsLabel"/>
<spring:message code="submit_label" var="submitLabel"/>
<spring:message code="select_city_label" var="selectCityLabel"/>
<script type="text/javascript">
$(function(){
$('#arrivalDate').datepicker({
dateFormat: 'yy-mm-dd',
changeYear: true
});
$('#departureDate').datepicker({
dateFormat: 'yy-mm-dd',
changeYear: true
});
});
</script>
<h2>Enter search for hotel!</h2>
<c:if test="${not empty message}">${message}</c:if>
<form:form modelAttribute="formdata">
<table>
<tr>
<td>${cityLabel}</td>
<td>
<form:select path="city" id="cityOptions">
<form:option value="">${selectCityLabel}</form:option>
<form:option value="New York">New York</form:option>
<form:option value="Los Angeles">Los Angeles</form:option>
<form:option value="Chicago">Chicago</form:option>
<form:option value="Houston">Houston</form:option>
<form:option value="Phoenix">Phoenix</form:option>
</form:select>
</td>
</tr>
<tr>
<td>${arrivalDateLabel}</td>
<td><form:input path="arrivalDate"/></td>
</tr>
<tr>
<td>${departureDateLabel}</td>
<td><form:input path="departureDate"/></td>
</tr>
<tr>
<td>${numberOfAdultsLabel}</td>
<td><form:input path="numberOfAdults" id="adultsInput"/></td>
</tr>
<tr>
<td></td>
<td><input type="submit" value="${submitLabel}"/></td>
</tr>
</table>
</form:form>
</jsp:root>
表单数据.java:
package domain;
import org.springframework.format.annotation.DateTimeFormat;
import java.util.Date;
public class FormData {
private String stateCode;
private String city;
@DateTimeFormat(pattern = "yy-mm-dd")
private Date arrivalDate;
@DateTimeFormat(pattern = "yy-mm-dd")
private Date departureDate;
private Integer numberOfAdults;
//getters and setters
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public Date getArrivalDate() {
return arrivalDate;
}
public void setArrivalDate(Date arrivalDate) {
this.arrivalDate = arrivalDate;
}
public Date getDepartureDate() {
return departureDate;
}
public void setDepartureDate(Date departureDate) {
this.departureDate = departureDate;
}
public Integer getNumberOfAdults() {
return numberOfAdults;
}
public void setNumberOfAdults(Integer numberOfAdults) {
this.numberOfAdults = numberOfAdults;
}
//other methods
public String toString()
{
if (city.equals("New York"))
stateCode = "NY";
else if(city.equals("Los Angeles"))
stateCode = "CA";
else if(city.equals("Chicago"))
stateCode = "IL";
else if(city.equals("Houston"))
stateCode = "TX";
else if(city.equals("Phoenix"))
stateCode = "AZ";
return "http://api.ean.com/ean" + "-" + "services/rs/hotel/v3/list?cid=55505&apiKey=97zvrux6eahgv64r3aarb7n2&locale=en_US¤cyCode=USD"
+ "&city=" + city
+ "&stateProvinceCode=" + stateCode
+ "&countryCode=US&supplierCacheTolerance=MED"
+ "&arrivalDate=" + arrivalDate.toString()
+ "&departureDate=" + departureDate.toString()
+ "&room1=" + numberOfAdults
+ "&numberOfResults=1";
}
}
搜索控制器.java:
package web.search.controller;
import domain.FormData;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import web.blogapp.controller.EntryController;
@RequestMapping("/search")
@Controller
public class SearchController {
final Logger logger = LoggerFactory.getLogger(EntryController.class);
@RequestMapping(method = RequestMethod.GET)
public String loadFormPage(Model uiModel) {
logger.info("Listing blog entries");
uiModel.addAttribute("formdata", new FormData());
return "search/form";
}
@RequestMapping(method = RequestMethod.POST)
public String submitForm(@ModelAttribute FormData formdata, Model uiModel)
{
uiModel.addAttribute("message", "Successfully saved data: " + formdata.toString());
return "search/form";
}
}
按提交后我得到的错误如下:
java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'formdata' available as request attribute org.springframework.web.servlet.support.BindStatus.<init>(BindStatus.java:141) org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.getBindStatus(AbstractDataBoundFormElementTag.java:179) org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.getPropertyPath(AbstractDataBoundFormElementTag.java:199) org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.getName(AbstractDataBoundFormElementTag.java:165) org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.writeDefaultAttributes(AbstractDataBoundFormElementTag.java:128) org.springframework.web.servlet.tags.form.AbstractHtmlElementTag.writeDefaultAttributes(AbstractHtmlElementTag.java:421) org.springframework.web.servlet.tags.form.SelectTag.writeTagContent(SelectTag.java:200) org.springframework.web.servlet.tags.form.AbstractFormTag.doStartTagInternal(AbstractFormTag.java:103) org.springframework.web.servlet.tags.RequestContextAwareTag.doStartTag(RequestContextAwareTag.java:80) org.apache.jsp.WEB_002dINF.views.search.form_jspx._jspService(form_jspx.java:145) org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70) javax.servlet.http.HttpServlet.service(HttpServlet.java:728) org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:432) org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390) org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334) javax.servlet.http.HttpServlet.service(HttpServlet.java:728) org.apache.jasper.runtime.JspRuntimeLibrary.include(JspRuntimeLibrary.java:954) org.apache.jasper.runtime.PageContextImpl.doInclude(PageContextImpl.java:684) org.apache.jasper.runtime.PageContextImpl.include(PageContextImpl.java:678) org.apache.tiles.request.jsp.JspRequest.doInclude(JspRequest.java:123) org.apache.tiles.request.AbstractViewRequest.dispatch(AbstractViewRequest.java:47) org.apache.tiles.request.render.DispatchRenderer.render(DispatchRenderer.java:45) org.apache.tiles.request.render.ChainedDelegateRenderer.render(ChainedDelegateRenderer.java:68) org.apache.tiles.impl.BasicTilesContainer.render(BasicTilesContainer.java:259) org.apache.tiles.template.InsertAttributeModel.renderAttribute(InsertAttributeModel.java:188) org.apache.tiles.template.InsertAttributeModel.execute(InsertAttributeModel.java:132) org.apache.tiles.jsp.taglib.InsertAttributeTag.doTag(InsertAttributeTag.java:299) org.apache.jsp.WEB_002dINF.layouts.default_jspx._jspx_meth_tiles_005finsertAttribute_005f2(default_jspx.java:619) org.apache.jsp.WEB_002dINF.layouts.default_jspx._jspService(default_jspx.java:140) org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70) javax.servlet.http.HttpServlet.service(HttpServlet.java:728) org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:432) org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390) org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334) javax.servlet.http.HttpServlet.service(HttpServlet.java:728) org.apache.tiles.request.servlet.ServletRequest.forward(ServletRequest.java:265) org.apache.tiles.request.servlet.ServletRequest.doForward(ServletRequest.java:228) org.apache.tiles.request.AbstractClientRequest.dispatch(AbstractClientRequest.java:57) org.apache.tiles.request.render.DispatchRenderer.render(DispatchRenderer.java:45) org.apache.tiles.impl.BasicTilesContainer.render(BasicTilesContainer.java:259) org.apache.tiles.impl.BasicTilesContainer.render(BasicTilesContainer.java:397) org.apache.tiles.impl.BasicTilesContainer.render(BasicTilesContainer.java:238) org.apache.tiles.impl.BasicTilesContainer.render(BasicTilesContainer.java:221) org.apache.tiles.renderer.DefinitionRenderer.render(DefinitionRenderer.java:59) org.springframework.web.servlet.view.tiles3.TilesView.renderMergedOutputModel(TilesView.java:145) org.springframework.web.servlet.view.AbstractView.render(AbstractView.java:263) org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1208) org.springframework.web.servlet.DispatcherServlet.processDispatchResult(DispatcherServlet.java:992) org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:939) org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:856) org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:936) org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:838) javax.servlet.http.HttpServlet.service(HttpServlet.java:647) org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:812) javax.servlet.http.HttpServlet.service(HttpServlet.java:728) org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter.doFilterInternal(OpenEntityManagerInViewFilter.java:180) org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:77) org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:88) org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:330) org.springframework.security.web.access.intercept.FilterSecurityInterceptor.invoke(FilterSecurityInterceptor.java:118) org.springframework.security.web.access.intercept.FilterSecurityInterceptor.doFilter(FilterSecurityInterceptor.java:84) org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342) org.springframework.security.web.access.ExceptionTranslationFilter.doFilter(ExceptionTranslationFilter.java:113) org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342) org.springframework.security.web.session.SessionManagementFilter.doFilter(SessionManagementFilter.java:103) org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342) org.springframework.security.web.authentication.AnonymousAuthenticationFilter.doFilter(AnonymousAuthenticationFilter.java:113) org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342) org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter.doFilter(SecurityContextHolderAwareRequestFilter.java:54) org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342) org.springframework.security.web.savedrequest.RequestCacheAwareFilter.doFilter(RequestCacheAwareFilter.java:45) org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342) org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter.doFilter(AbstractAuthenticationProcessingFilter.java:183) org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342) org.springframework.security.web.authentication.logout.LogoutFilter.doFilter(LogoutFilter.java:105) org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342) org.springframework.security.web.context.SecurityContextPersistenceFilter.doFilter(SecurityContextPersistenceFilter.java:87) org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342) org.springframework.security.web.FilterChainProxy.doFilterInternal(FilterChainProxy.java:192) org.springframework.security.web.FilterChainProxy.doFilter(FilterChainProxy.java:160) org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:346) org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:259)
调试应用程序并在 submitForm 方法内停止我有以下值
stateCode :TX
城市:休斯顿
到达日期:2013 年 1 月 21 日星期一 00:07:00 EET
出发日期:2013 年 1 月 30 日星期三 00:07:00 EET
numberOfAdults:1