0

ここで何が間違っているのかわかりません。私はそれについていくつかのチュートリアルを読み、それがどのように行われるべきかの「彼らの」バージョンを試しました。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

これが十分な情報であることを願っています..

4

2 に答える 2

6

問題は、ここでのリストの宣言にあります。

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

ご想像に反して、Arrays.asList()は を返しませんArrayList。むしろ、(Javadoc を引用するために) 「指定された配列に基づく固定サイズのリスト」を返します。ここでのキー フレーズは「固定サイズ」です。これは、配列のサイズを増減できないことを意味します。つまり、によって返される List はArrays.asList()、配列に基づく新しいリストではなく、コレクションのような配列のビューと見なされるべきです。

上記の行を次のように変更するだけで問題を解決できます。

private static List<Bruker> brukere = new ArrayList<Bruker>();

ArrayListの適切な完全実装であるを使用しListます。

ちなみに、によって返されるクラスの実際の実装についてもう少し詳しく説明しているこのブログ投稿Arrays.asList()をご覧になることもできます。

UnsupportedOperationExceptionをスローすることは、 の動作が文書化されList.add()ていることにも注意してください。これはインターフェースの期待される性質に違反しているように見えるかもしれませんが、JavaListクラスは実際にはいくつかのメソッド ( add()addAll()remove()clear()など、基本的には、基本的に保存されているリストを変更することを目的としたメソッド) を提供しており、これらのメソッドは単に実装するだけではオプションで実装できません。を投げるUnsupportedOperationException

于 2012-04-17T13:27:38.963 に答える
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 ファイル内

于 2012-04-18T07:30:15.877 に答える