Search code examples
javahtmlwicketdropdownchoice

Apache Wicket - How to retrieve the selected value from DropdownChoice in java


Java class : This is the method where i created the dropdownchoice object for select menu and add it to the form. In this method adding the projects list to the billableProjectsList

public class ReportCriteria implements Serializable {
    private static final long serialVersionUID = xxxxxxxxxL;
   
    private ReportCriteria selectedBillableProject;


    public ReportCriteria getSelectedBillableProject() {
        return selectedBillableProject;
    }

    public void setSelectedBillableProject(ReportCriteria selectedBillableProject) {
        this.selectedBillableProject = selectedBillableProject;
    }
}

private Form<ReportCriteria> createCriteriaPanel(String id, 
                                         IModel<ReportCriteria> model) {
  SelectionForm form = new SelectionForm(id, model);
  ReportCriteria criteria = (ReportCriteria) getDefaultModelObject();
  List<Project> billableProjects = ProjectUtil.filterBillable(allProjects);
  IModel<Project> dropdownModel =   new 
                    PropertyModel<Project(criteria,"selectedBillableProject");
  DropDownChoice<Project> billableProjectsList = new DropDownChoice<>(
                    "projectsList",
                    dropdownModel,
                    billableProjects,
                    new ChoiceRenderer<Project>("fullNameWithCustomer"));
   billableProjectsList.setLabel(new ResourceModel("printMonth.billable"));
   form.add(billableProjectsList);
   System.out.println("selectedBillableProject --- >"
                                  +billableProjectsList.getModelObject()
                                  +"value is --->"
                                  +criteria.getSelectedBillableProject());
    }  

Html file code : This html file is for select menu display .

<td style="padding-left: 5px">
   <strong>
       <wicket:message key="printMonth.billable"/>
   </strong><br/>
   <p style="border-top: solid 1px white;margin:0 0 5px 0;padding:0">
      <select wicket:id="projectsList"></select>
    </p>
</td>

Error : This error is resulted once i select the dropdown value from the list . I have created the variable selectedBillableObject in ReportCriteria class and have added the setter and getter. But i am unable to convert it in this method to add it to form since it is type casting issue . But when i tried to print the value in selectedBillablePorject , its is null .

<pre>11-19@16:52:50 ERROR org.apache.wicket.DefaultExceptionMapper - Unexpected error occurred
org.apache.wicket.WicketRuntimeException: Method onFormSubmitted of interface org.apache.wicket.markup.html.form.IFormSubmitListener targeted at 
[SelectionForm [Component id = criteriaForm]] on component [SelectionForm [Component id = criteriaForm]] threw an exception
    at org.apache.wicket.RequestListenerInterface.internalInvoke(RequestListenerInterface.java:268)
    at org.apache.wicket.RequestListenerInterface.invoke(RequestListenerInterface.java:216)
    at org.apache.wicket.core.request.handler.ListenerInterfaceRequestHandler.invokeListener(ListenerInterfaceRequestHandler.java:243)
    at org.apache.wicket.core.request.handler.ListenerInterfaceRequestHandler.respond(ListenerInterfaceRequestHandler.java:236)
    at org.apache.wicket.request.cycle.RequestCycle$HandlerExecutor.respond(RequestCycle.java:890)
    at org.apache.wicket.request.RequestHandlerStack.execute(RequestHandlerStack.java:64)
    at org.apache.wicket.request.cycle.RequestCycle.execute(RequestCycle.java:261)
    at org.apache.wicket.request.cycle.RequestCycle.processRequest(RequestCycle.java:218)
    at org.apache.wicket.request.cycle.RequestCycle.processRequestAndDetach(RequestCycle.java:289)
    at org.apache.wicket.protocol.http.WicketFilter.processRequestCycle(WicketFilter.java:259)
    at org.apache.wicket.protocol.http.WicketFilter.processRequest(WicketFilter.java:201)
    at org.apache.wicket.protocol.http.WicketFilter.doFilter(WicketFilter.java:282)
    at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1484)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:330)
    at org.springframework.security.web.context.SecurityContextPersistenceFilter.doFilter(SecurityContextPersistenceFilter.java:87)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
    at org.springframework.security.web.FilterChainProxy.doFilterInternal(FilterChainProxy.java:192)
    at org.springframework.security.web.FilterChainProxy.doFilter(FilterChainProxy.java:160)
    at org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:346)
    at org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:262)
    at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1484)
    at org.springframework.orm.hibernate4.support.OpenSessionInViewFilter.doFilterInternal(OpenSessionInViewFilter.java:151)
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
    at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1476)
    at org.eclipse.jetty.servlet.ServletHandler.doHandle(ServletHandler.java:501)
    at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:137)
    at org.eclipse.jetty.security.SecurityHandler.handle(SecurityHandler.java:557)
    at org.eclipse.jetty.server.session.SessionHandler.doHandle(SessionHandler.java:231)
    at org.eclipse.jetty.server.handler.ContextHandler.doHandle(ContextHandler.java:1086)
    at org.eclipse.jetty.servlet.ServletHandler.doScope(ServletHandler.java:429)
    at org.eclipse.jetty.server.session.SessionHandler.doScope(SessionHandler.java:193)
    at org.eclipse.jetty.server.handler.ContextHandler.doScope(ContextHandler.java:1020)
    at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:135)
    at org.eclipse.jetty.server.handler.ContextHandlerCollection.handle(ContextHandlerCollection.java:255)
    at org.eclipse.jetty.server.handler.HandlerCollection.handle(HandlerCollection.java:154)
    at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:116)
    at org.eclipse.jetty.server.Server.handle(Server.java:370)
    at org.eclipse.jetty.server.AbstractHttpConnection.handleRequest(AbstractHttpConnection.java:494)
    at org.eclipse.jetty.server.AbstractHttpConnection.content(AbstractHttpConnection.java:982)
    at org.eclipse.jetty.server.AbstractHttpConnection$RequestHandler.content(AbstractHttpConnection.java:1043)
    at org.eclipse.jetty.http.HttpParser.parseNext(HttpParser.java:865)
    at org.eclipse.jetty.http.HttpParser.parseAvailable(HttpParser.java:240)
    at org.eclipse.jetty.server.AsyncHttpConnection.handle(AsyncHttpConnection.java:82)
    at org.eclipse.jetty.io.nio.SelectChannelEndPoint.handle(SelectChannelEndPoint.java:696)
    at org.eclipse.jetty.io.nio.SelectChannelEndPoint$1.run(SelectChannelEndPoint.java:53)
    at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:608)
    at org.eclipse.jetty.util.thread.QueuedThreadPool$3.run(QueuedThreadPool.java:543)
    at java.lang.Thread.run(Thread.java:748)
Caused by: java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.apache.wicket.RequestListenerInterface.internalInvoke(RequestListenerInterface.java:258)
    ... 47 more
Caused by: org.apache.wicket.util.convert.ConversionException: Could not convert value: net.rrm.ehour.domain.Project[----] to type: net.rrm.ehour.report.criteria.ReportCriteria. Could not find compatible converter.
    at org.apache.wicket.ConverterLocator$DefaultConverter.convertToObject(ConverterLocator.java:121)
    at org.apache.wicket.core.util.lang.PropertyResolverConverter.convert(PropertyResolverConverter.java:95)
    at org.apache.wicket.core.util.lang.PropertyResolver$MethodGetAndSet.setValue(PropertyResolver.java:1194)
    at org.apache.wicket.core.util.lang.PropertyResolver$ObjectAndGetSetter.setValue(PropertyResolver.java:651)
    at org.apache.wicket.core.util.lang.PropertyResolver.setValue(PropertyResolver.java:151)
    at org.apache.wicket.model.AbstractPropertyModel.setObject(AbstractPropertyModel.java:132)
    at org.apache.wicket.Component.setDefaultModelObject(Component.java:3052)
    at org.apache.wicket.markup.html.form.FormComponent.setModelObject(FormComponent.java:1577)
    at org.apache.wicket.markup.html.form.FormComponent.updateModel(FormComponent.java:1098)
    at org.apache.wicket.markup.html.form.Form$FormModelUpdateVisitor.component(Form.java:230)
    at org.apache.wicket.markup.html.form.Form$FormModelUpdateVisitor.component(Form.java:200)
    at org.apache.wicket.util.visit.Visits.visitPostOrderHelper(Visits.java:274)
    at org.apache.wicket.util.visit.Visits.visitPostOrderHelper(Visits.java:262)
    at org.apache.wicket.util.visit.Visits.visitPostOrder(Visits.java:245)
    at org.apache.wicket.markup.html.form.FormComponent.visitComponentsPostOrder(FormComponent.java:424)
    at org.apache.wicket.markup.html.form.Form.internalUpdateFormComponentModels(Form.java:1860)
    at org.apache.wicket.markup.html.form.Form.updateFormComponentModels(Form.java:1825)
    at org.apache.wicket.markup.html.form.Form.process(Form.java:955)
    at org.apache.wicket.markup.html.form.Form.onFormSubmitted(Form.java:788)
    at org.apache.wicket.markup.html.form.Form.onFormSubmitted(Form.java:721)
    ... 52 more
11-19@16:53:18 ERROR org.apache.wicket.DefaultExceptionMapper - Unexpected error occurred
org.apache.wicket.WicketRuntimeException: Method onFormSubmitted of interface org.apache.wicket.markup.html.form.IFormSubmitListener targeted at [SelectionForm [Component id = criteriaForm]] on component [SelectionForm [Component id = criteriaForm]] threw an exception
    at org.apache.wicket.RequestListenerInterface.internalInvoke(RequestListenerInterface.java:268)
    at org.apache.wicket.RequestListenerInterface.invoke(RequestListenerInterface.java:216)
    at org.apache.wicket.core.request.handler.ListenerInterfaceRequestHandler.invokeListener(ListenerInterfaceRequestHandler.java:243)
    at org.apache.wicket.core.request.handler.ListenerInterfaceRequestHandler.respond(ListenerInterfaceRequestHandler.java:236)
    at org.apache.wicket.request.cycle.RequestCycle$HandlerExecutor.respond(RequestCycle.java:890)
    at org.apache.wicket.request.RequestHandlerStack.execute(RequestHandlerStack.java:64)
    at org.apache.wicket.request.cycle.RequestCycle.execute(RequestCycle.java:261)
    at org.apache.wicket.request.cycle.RequestCycle.processRequest(RequestCycle.java:218)
    at org.apache.wicket.request.cycle.RequestCycle.processRequestAndDetach(RequestCycle.java:289)
    at org.apache.wicket.protocol.http.WicketFilter.processRequestCycle(WicketFilter.java:259)
    at org.apache.wicket.protocol.http.WicketFilter.processRequest(WicketFilter.java:201)
    at org.apache.wicket.protocol.http.WicketFilter.doFilter(WicketFilter.java:282)
    at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1484)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:330)
    at org.springframework.security.web.context.SecurityContextPersistenceFilter.doFilter(SecurityContextPersistenceFilter.java:87)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
    at org.springframework.security.web.FilterChainProxy.doFilterInternal(FilterChainProxy.java:192)
    at org.springframework.security.web.FilterChainProxy.doFilter(FilterChainProxy.java:160)
    at org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:346)
    at org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:262)
    at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1484)
    at org.springframework.orm.hibernate4.support.OpenSessionInViewFilter.doFilterInternal(OpenSessionInViewFilter.java:151)
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
    at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1476)
    at org.eclipse.jetty.servlet.ServletHandler.doHandle(ServletHandler.java:501)
    at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:137)
    at org.eclipse.jetty.security.SecurityHandler.handle(SecurityHandler.java:557)
    at org.eclipse.jetty.server.session.SessionHandler.doHandle(SessionHandler.java:231)
    at org.eclipse.jetty.server.handler.ContextHandler.doHandle(ContextHandler.java:1086)
    at org.eclipse.jetty.servlet.ServletHandler.doScope(ServletHandler.java:429)
    at org.eclipse.jetty.server.session.SessionHandler.doScope(SessionHandler.java:193)
    at org.eclipse.jetty.server.handler.ContextHandler.doScope(ContextHandler.java:1020)
    at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:135)
    at org.eclipse.jetty.server.handler.ContextHandlerCollection.handle(ContextHandlerCollection.java:255)
    at org.eclipse.jetty.server.handler.HandlerCollection.handle(HandlerCollection.java:154)
    at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:116)
    at org.eclipse.jetty.server.Server.handle(Server.java:370)
    at org.eclipse.jetty.server.AbstractHttpConnection.handleRequest(AbstractHttpConnection.java:494)
    at org.eclipse.jetty.server.AbstractHttpConnection.content(AbstractHttpConnection.java:982)
    at org.eclipse.jetty.server.AbstractHttpConnection$RequestHandler.content(AbstractHttpConnection.java:1043)
    at org.eclipse.jetty.http.HttpParser.parseNext(HttpParser.java:865)
    at org.eclipse.jetty.http.HttpParser.parseAvailable(HttpParser.java:240)
    at org.eclipse.jetty.server.AsyncHttpConnection.handle(AsyncHttpConnection.java:82)
    at org.eclipse.jetty.io.nio.SelectChannelEndPoint.handle(SelectChannelEndPoint.java:696)
    at org.eclipse.jetty.io.nio.SelectChannelEndPoint$1.run(SelectChannelEndPoint.java:53)
    at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:608)
    at org.eclipse.jetty.util.thread.QueuedThreadPool$3.run(QueuedThreadPool.java:543)
    at java.lang.Thread.run(Thread.java:748)
Caused by: java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.apache.wicket.RequestListenerInterface.internalInvoke(RequestListenerInterface.java:258)
    ... 47 more
Caused by: org.apache.wicket.util.convert.ConversionException: Could not convert value: net.rrm.ehour.domain.Project@328849ff[]] to type: net.rrm.ehour.report.criteria.ReportCriteria. 
ould not find compatible converter.
    at org.apache.wicket.ConverterLocator$DefaultConverter.convertToObject(ConverterLocator.java:121)
    at org.apache.wicket.core.util.lang.PropertyResolverConverter.convert(PropertyResolverConverter.java:95)
    at org.apache.wicket.core.util.lang.PropertyResolver$MethodGetAndSet.setValue(PropertyResolver.java:1194)
    at org.apache.wicket.core.util.lang.PropertyResolver$ObjectAndGetSetter.setValue(PropertyResolver.java:651)
    at org.apache.wicket.core.util.lang.PropertyResolver.setValue(PropertyResolver.java:151)
    at org.apache.wicket.model.AbstractPropertyModel.setObject(AbstractPropertyModel.java:132)
    at org.apache.wicket.Component.setDefaultModelObject(Component.java:3052)
    at org.apache.wicket.markup.html.form.FormComponent.setModelObject(FormComponent.java:1577)
    at org.apache.wicket.markup.html.form.FormComponent.updateModel(FormComponent.java:1098)
    at org.apache.wicket.markup.html.form.Form$FormModelUpdateVisitor.component(Form.java:230)
    at org.apache.wicket.markup.html.form.Form$FormModelUpdateVisitor.component(Form.java:200)
    at org.apache.wicket.util.visit.Visits.visitPostOrderHelper(Visits.java:274)
    at org.apache.wicket.util.visit.Visits.visitPostOrderHelper(Visits.java:262)
    at org.apache.wicket.util.visit.Visits.visitPostOrder(Visits.java:245)
    at org.apache.wicket.markup.html.form.FormComponent.visitComponentsPostOrder(FormComponent.java:424)
    at org.apache.wicket.markup.html.form.Form.internalUpdateFormComponentModels(Form.java:1860)
    at org.apache.wicket.markup.html.form.Form.updateFormComponentModels(Form.java:1825)
    at org.apache.wicket.markup.html.form.Form.process(Form.java:955)
    at org.apache.wicket.markup.html.form.Form.onFormSubmitted(Form.java:788)
    at org.apache.wicket.markup.html.form.Form.onFormSubmitted(Form.java:721)
    ... 52 more
    </pre>

Solution

  • What is

     new PropertyModel<Test>(this," ");
    

    ?

    PropertyModel is used to read/write object's member fields. I am pretty sure Java syntax does not allow to name a member field as " ", i.e. a space character.

    In addition the method that you should use is dropDownChoice.getModelObject(), not getObject().

    Update:

    public class ReportCriteria implements Serializable {
    private static final long serialVersionUID = xxxxxxxxxL;
    
    private ReportCriteria selectedBillableProject;
    

    selectedBillableProject has to be a Project, not ReportCriteria.