Validating dates
dahm Apr 4, 2006 3:39 PMHi,
I'm having an Entity class which contains Date objects, say
(excuse me, I'm too lazy to translate all the names ;-)
@Entity @Name("vertrag") @Scope(SESSION) public class Vertrag implements Serializable { ... private Date _vertragsBeginn; @Basic @Temporal(TemporalType.DATE) public Date getVertragsBeginn() { return _vertragsBeginn; } public void setVertragsBeginn(Date vertragsBeginn) { _vertragsBeginn = vertragsBeginn; } .... }
Now I'd like to fill in a form and set the values of that entity:
<c:view> <h:form> <table border="0"> <tr> <td>Vertragsbeginn:</td> <td><h:inputText value="#{vertrag.vertragsBeginn}"> <f:convertDateTime type="date" pattern="dd.MM.yyyy"/> </h:inputText> </td> </tr> </table> <FONT color="RED"> <h:messages /> </FONT> <h:commandButton type="submit" value="Anlegen" action="#{registerVertrag.register}" /> </h:form> </c:view>
I got a bean to do it all:
@Stateless @Name("registerVertrag") @Interceptors(SeamInterceptor.class) public class RegisterVertragActionBean implements RegisterVertragAction { private static final Category _logger = Logger.getInstance(RegisterVertragActionBean.class); @In(value = "vertrag") @Out(value = "vertrag", required = false) @Valid private Vertrag _vertrag; @PersistenceContext private EntityManager _em; @IfInvalid(outcome = Outcome.REDISPLAY) public String register() { _em.persist(_vertrag); _vertrag = null; return "success"; } }
Looks fine so far for me. However it does seem to me that Hibernate,
Seam or
JSF MyFaces does not recognize that the property "vertragsBeginn" is
a date and tries to set is a a string:
15:00:27,906 ERROR [[/iv]] Cannot set value for expression '#{vertrag.vertragsBeginn}' to a new value of type java.lang.String javax.faces.el.EvaluationException: Cannot set value for expression '#{vertrag.vertragsBeginn}' to a new value of type java.lang.String at org.apache.myfaces.el.ValueBindingImpl.setValue(ValueBindingImpl.java:304) at javax.faces.component.UIInput.updateModel(UIInput.java:226) at javax.faces.component.UIInput.processUpdates(UIInput.java:165) at javax.faces.component.UIForm.processUpdates(UIForm.java:85) at javax.faces.component.UIComponentBase.processUpdates(UIComponentBase.java:428) at javax.faces.component.UIViewRoot.processUpdates(UIViewRoot.java:153) at org.apache.myfaces.lifecycle.LifecycleImpl.updateModelValues(LifecycleImpl.java:277) at org.apache.myfaces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:81) at javax.faces.webapp.FacesServlet.service(FacesServlet.java:106) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:252) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173) at org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:96) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:202) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173) at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:213) at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:178) at org.jboss.web.tomcat.security.CustomPrincipalValve.invoke(CustomPrincipalValve.java:54) at org.jboss.web.tomcat.security.SecurityAssociationValve.invoke(SecurityAssociationValve.java:174) at org.jboss.web.tomcat.security.JaccContextValve.invoke(JaccContextValve.java:74) at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:126) at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:105) at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:107) at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:148) at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:868) at org.apache.coyote.http11.Http11BaseProtocol$Http11ConnectionHandler.processConnection(Http11BaseProtocol.java:663) at org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:527) at org.apache.tomcat.util.net.MasterSlaveWorkerThread.run(MasterSlaveWorkerThread.java:112) at java.lang.Thread.run(Thread.java:595) Caused by: javax.faces.el.EvaluationException: de.kvbb.ingver.model.Vertrag at org.apache.myfaces.el.PropertyResolverImpl.setValue(PropertyResolverImpl.java:155) at org.apache.myfaces.el.ValueBindingImpl.setValue(ValueBindingImpl.java:269) ... 27 more Caused by: javax.faces.el.EvaluationException: Bean: de.kvbb.ingver.model.Vertrag, property: vertragsBeginn at org.apache.myfaces.el.PropertyResolverImpl.setProperty(PropertyResolverImpl.java:372) at org.apache.myfaces.el.PropertyResolverImpl.setValue(PropertyResolverImpl.java:148) ... 28 more Caused by: java.lang.IllegalArgumentException: argument type mismatch at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) at java.lang.reflect.Method.invoke(Method.java:585) at org.apache.myfaces.el.PropertyResolverImpl.setProperty(PropertyResolverImpl.java:368)
What can I do to ensure that the value is converted to a date given
the specified format ("dd.MM.yyyy"). I didn't find a Hibernate tag for
this purpose (Struts did have this, never mind :-)
BTW: I'd like to use the validators/setters for other kind of objects, too.
Even, if I have to write my own validator for that. Quite simple thanks
to Hibernate...
Thanks in advance
Markus