卡在Default DataTable组件上 - 找不到错误

时间:2012-04-17 13:18:31

标签: java wicket

我陷入困境,无法找到我在这里做错了什么。我已经阅读了几个关于它的教程,并尝试了如何完成它的“他们的”版本。 Netbeans没有给出任何错误,但我的浏览器确实如此。 她是我的代码。欢迎任何提示

RegistrerteBrukere.java

    package com.myapp.wicket;

import java.util.ArrayList;
import java.util.List;
import org.apache.wicket.PageParameters;
import org.apache.wicket.extensions.markup.html.repeater.data.table.DefaultDataTable;
import org.apache.wicket.extensions.markup.html.repeater.data.table.IColumn;
import org.apache.wicket.extensions.markup.html.repeater.data.table.PropertyColumn;
import org.apache.wicket.model.Model;

/**
 *
 * @author Bjørn J Aurheim
 */
public final class RegistrerteBrukere extends BasePage {

    private DefaultDataTable<Bruker> table;
    private int antallRekkerPrSide = 50;

    private BrukerProvider provider = new BrukerProvider();


    public RegistrerteBrukere() {
        super();

        // opprette testdata i liste for datatabellen


        // List<IColumn<Bruker>> kolonner = new ArrayList<IColumn<Bruker>>();
        List kolonner = new ArrayList();
        kolonner.add(new PropertyColumn<Bruker>(new Model("Fornavn"),"bruker","bruker"));
        kolonner.add(new PropertyColumn<Bruker>(new Model("Etternavn"),"etternavn","bruker"));
        kolonner.add(new PropertyColumn<Bruker>(new Model("Telefonnummer"),"telefonnummer","telefonnummer"));
        kolonner.add(new PropertyColumn<Bruker>(new Model("Mailadresse"),"mailadresse","mailadresse"));
        kolonner.add(new PropertyColumn<Bruker>(new Model("StartDato"),"startdato","startdato"));
        kolonner.add(new PropertyColumn<Bruker>(new Model("Passord"),"passord","passord"));
        table = new DefaultDataTable<Bruker>("brukertable", kolonner, provider, antallRekkerPrSide);
        add(table);

        }


    public RegistrerteBrukere(PageParameters params) {


        //TODO:  process page parameters

    }
}

RegistrerteBrukere.html

    <?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns:wicket="http://wicket.apache.org">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
        <title>RegistrerteBrukere</title>
        <link rel="stylesheet" type="text/css" href="style.css"/>
    </head>
    <body>
        <wicket:extend>
            <table wicket:id="brukertable" class="brukere"></table>
        </wicket:extend>

    </body>
</html>

BrukerProvider.java

public class BrukerProvider extends SortableDataProvider<Bruker>{
Bruker u = new Bruker();
Bruker u1 = new Bruker(); 
private static List<Bruker> brukere = Arrays.asList( );

        public BrukerProvider(){
            setSort("fornavn",true);
            u.setForNavn("Tore");
            u.setEtterNavn("Hansen");
            u.setTelefonnummer("99887766");
            u.setMailadresse("noen@home.her");
            u.setStartDato(new Date());
            u.setPassord("passord");
            brukere.add(u);
            u1.setForNavn("Hanne");
            u1.setEtterNavn("Ås");
            u1.setTelefonnummer("99324766");
            u1.setMailadresse("noener@homdfe.her");
            u1.setStartDato(new Date());
            u1.setPassord("passsdford");
            brukere.add(u1);
        }
@Override
        public Iterator<? extends Bruker> iterator(int first, int count){
          List<Bruker> data = new ArrayList<Bruker>(brukere);
          Collections.sort (data, new Comparator<Bruker>(){
        @Override
           public int compare(Bruker o1, Bruker o2) {
               int dir = getSort().isAscending() ? 1: -1;
               if ("fornavn".equals(getSort().getProperty())){
                   return dir * (o1.getForNavn().compareTo(o2.getForNavn()));
               } else {
                   return dir * (o1.getEtterNavn().compareTo(o2.getEtterNavn()));
               }

           }

        });
        return data.subList(first, Math.min(first + count, data.size())).iterator();
 }
@Override
        public int size(){
            return brukere.size();
        }
@Override
        public IModel<Bruker> model(Bruker object){
            return Model.of(object);
        }
}

我的错误消息:

    WicketMessage: Can't instantiate page using constructor public com.myapp.wicket.RegistrerteBrukere()

Root cause:

java.lang.UnsupportedOperationException
     at java.util.AbstractList.add(AbstractList.java:148)
     at java.util.AbstractList.add(AbstractList.java:108)
     at com.myapp.wicket.BrukerProvider.<init>(BrukerProvider.java:29)
     at com.myapp.wicket.RegistrerteBrukere.<init>(RegistrerteBrukere.java:24)
     at java.lang.reflect.Constructor.newInstance(Constructor.java:525)
     at org.apache.wicket.session.DefaultPageFactory.createPage(DefaultPageFactory.java:192)
     at org.apache.wicket.session.DefaultPageFactory.newPage(DefaultPageFactory.java:57)
     at org.apache.wicket.request.target.component.BookmarkablePageRequestTarget.newPage(BookmarkablePageRequestTarget.java:298)
     at org.apache.wicket.request.target.component.BookmarkablePageRequestTarget.getPage(BookmarkablePageRequestTarget.java:320)
     at org.apache.wicket.request.target.component.BookmarkablePageRequestTarget.processEvents(BookmarkablePageRequestTarget.java:234)
     at org.apache.wicket.request.AbstractRequestCycleProcessor.processEvents(AbstractRequestCycleProcessor.java:92)
     at org.apache.wicket.RequestCycle.processEventsAndRespond(RequestCycle.java:1279)
     at org.apache.wicket.RequestCycle.step(RequestCycle.java:1358)
     at org.apache.wicket.RequestCycle.steps(RequestCycle.java:1465)
     at org.apache.wicket.RequestCycle.request(RequestCycle.java:545)
     at org.apache.wicket.protocol.http.WicketFilter.doGet(WicketFilter.java:486)
     at org.apache.wicket.protocol.http.WicketFilter.doFilter(WicketFilter.java:319)
     at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
     at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
     at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:224)
     at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:169)
     at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
     at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:168)
     at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
     at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:929)
     at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
     at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:405)
     at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:964)
     at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:515)
     at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:304)
     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)
     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)
     at java.lang.Thread.run(Thread.java:722)

Complete stack:

org.apache.wicket.WicketRuntimeException: Can't instantiate page using constructor public com.myapp.wicket.RegistrerteBrukere()
     at org.apache.wicket.session.DefaultPageFactory.createPage(DefaultPageFactory.java:212)
     at org.apache.wicket.session.DefaultPageFactory.newPage(DefaultPageFactory.java:57)
     at org.apache.wicket.request.target.component.BookmarkablePageRequestTarget.newPage(BookmarkablePageRequestTarget.java:298)
     at org.apache.wicket.request.target.component.BookmarkablePageRequestTarget.getPage(BookmarkablePageRequestTarget.java:320)
     at org.apache.wicket.request.target.component.BookmarkablePageRequestTarget.processEvents(BookmarkablePageRequestTarget.java:234)
     at org.apache.wicket.request.AbstractRequestCycleProcessor.processEvents(AbstractRequestCycleProcessor.java:92)
     at org.apache.wicket.RequestCycle.processEventsAndRespond(RequestCycle.java:1279)
     at org.apache.wicket.RequestCycle.step(RequestCycle.java:1358)
     at org.apache.wicket.RequestCycle.steps(RequestCycle.java:1465)
     at org.apache.wicket.RequestCycle.request(RequestCycle.java:545)
     at org.apache.wicket.protocol.http.WicketFilter.doGet(WicketFilter.java:486)

java.lang.reflect.InvocationTargetException
     at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
     at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
     at java.lang.reflect.Constructor.newInstance(Constructor.java:525)
     at org.apache.wicket.session.DefaultPageFactory.createPage(DefaultPageFactory.java:192)
     at org.apache.wicket.session.DefaultPageFactory.newPage(DefaultPageFactory.java:57)
     at org.apache.wicket.request.target.component.BookmarkablePageRequestTarget.newPage(BookmarkablePageRequestTarget.java:298)
     at org.apache.wicket.request.target.component.BookmarkablePageRequestTarget.getPage(BookmarkablePageRequestTarget.java:320)
     at org.apache.wicket.request.target.component.BookmarkablePageRequestTarget.processEvents(BookmarkablePageRequestTarget.java:234)
     at org.apache.wicket.request.AbstractRequestCycleProcessor.processEvents(AbstractRequestCycleProcessor.java:92)
     at org.apache.wicket.RequestCycle.processEventsAndRespond(RequestCycle.java:1279)
     at org.apache.wicket.RequestCycle.step(RequestCycle.java:1358)
     at org.apache.wicket.RequestCycle.steps(RequestCycle.java:1465)
     at org.apache.wicket.RequestCycle.request(RequestCycle.java:545)
     at org.apache.wicket.protocol.http.WicketFilter.doGet(WicketFilter.java:486)



display page view

希望这是足够的信息..

2 个答案:

答案 0 :(得分:6)

问题在于您在此处声明您的清单:

private static List<Bruker> brukere = Arrays.asList( );

与您猜测的相反,Arrays.asList()不会返回ArrayList。相反,它返回(引用Javadoc)“由指定数组支持的固定大小的列表”。这里的关键短语是“fixed-size”,这意味着你不能增加或减少数组的大小。换句话说,Arrays.asList()返回的List应该被视为数组的类似Collection的视图,而不是数组支持的新列表。

您只需将以上行更改为:

即可解决问题
private static List<Bruker> brukere = new ArrayList<Bruker>();

哪个会使用ArrayList,这是List的正确完整实现。

顺便说一下,您可以查看this blog post,它可以更深入地了解Arrays.asList()返回的类的实际实现。

值得注意的是,投掷UnsupportedOperationExceptiondocumented behavior for List.add()。这似乎违反了接口的预期性质,但Java List类实际上提供了许多方法(add()addAll()remove(),{{1}等等,基本上,用于更改底层存储列表的方法)实现可以通过简单地抛出clear()来实现。

答案 1 :(得分:0)

将Arrays.asList更改为ArrayList后,我仍然遇到错误。通过替换

进行了纠正
 List kolonner = new ArrayList();
    kolonner.add(new PropertyColumn<Bruker>(new Model("Fornavn"),"bruker","bruker"));
    kolonner.add(new PropertyColumn<Bruker>(new Model("Etternavn"),"etternavn","bruker"));
    kolonner.add(new PropertyColumn<Bruker>(new Model("Telefonnummer"),"telefonnummer","telefonnummer"));
    kolonner.add(new PropertyColumn<Bruker>(new Model("Mailadresse"),"mailadresse","mailadresse"));
    kolonner.add(new PropertyColumn<Bruker>(new Model("StartDato"),"startdato","startdato"));
    kolonner.add(new PropertyColumn<Bruker>(new Model("Passord"),"passord","passord"));
    table = new DefaultDataTable<Bruker>("brukertable", kolonner, provider, antallRekkerPrSide);

使用:

  `List kolonner = new ArrayList();
    kolonner.add(new PropertyColumn<Bruker>(new Model("Fornavn"),"fornavn","fornavn"));
    kolonner.add(new PropertyColumn<Bruker>(new Model("Etternavn"),"etternavn","etternavn"));
    kolonner.add(new PropertyColumn<Bruker>(new Model("Telefonnummer"),"telefonnummer","telefonnummer"));
    kolonner.add(new PropertyColumn<Bruker>(new Model("Mailadresse"),"mailadresse","mailadresse"));
    kolonner.add(new PropertyColumn<Bruker>(new Model("StartDato"),"startdato","startDato"));
    kolonner.add(new PropertyColumn<Bruker>(new Model("Passord"),"passord","passord"));
    table = new DefaultDataTable<Bruker>("brukertable", kolonner, provider, antallRekkerPrSide);`

在RegistrerteBrukere.java文件中