apache wicket-如何在java中从dropdownchoice检索选定的值

khbbv19g  于 2021-07-11  发布在  Java
关注(0)|答案(2)|浏览(340)

java类:这是我为select menu创建dropdowntchoice对象并将其添加到表单中的方法。在此方法中,将项目列表添加到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文件代码:此html文件用于选择菜单显示。

<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>

错误:一旦我从列表中选择了下拉值,就会产生这个错误。我在reportcriteria类中创建了变量selectedbillableobject,并添加了setter和getter。但我无法在这个方法中转换它来添加到表单中,因为它是类型转换问题。但是当我试图打印selectedbillableporject中的值时,它是空的。

<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>
zpf6vheq

zpf6vheq1#

是什么

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

?
propertymodel用于读/写对象的成员字段。我很确定java语法不允许将成员字段命名为“”,即空格字符。
此外,应该使用的方法是dropdownchoice.getmodelobject(),而不是 getObject() .
更新:

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

private ReportCriteria selectedBillableProject;
``` `selectedBillableProject` 必须是一个 `Project` ,不是 `ReportCriteria` .
mpgws1up

mpgws1up2#

以防分拆:
1.-为什么要使用属性模型来处理dropbox选定选项?您定义的属性模型如下:

IModel<Test> dropdownModel =   new PropertyModel<Test>(this," ");

属性模型用于将youtpojo的字段与model对象绑定,在pojo和wicket组件之间自动执行“bing”。在你的辛塔克斯 this 应该是处理pojo的模型对象,而不是 " " 您应该将pojo的属性的名称绑定到该属性模型。也许这就是为什么你得到“没有得到方法定义”的原因,因为 this 没有具有名为 "selectedBillableItem" ,记住,属性模型使用“getter”和“setters”来查找pojo的属性。
2.-我使用了dropdownchoice,如下所示:

DropDownChoice myDropDownChoice = new DropDownChoice<String>("myDropDownChoiceWicketId", new Model<String>(), List<String> object);

3.-恢复dropdownchoice的选定项,如下所示:

anyModelObject.getObject().setToAnyProperty(myDropDownChoice.getModelObject());

希望你能从中得到一些提示。

相关问题