0

Can't display list in zul page

asked 2017-12-07 00:02:36 +0800

mutandaoscura gravatar image mutandaoscura
1 1

updated 2017-12-11 09:34:37 +0800

cor3000 gravatar image cor3000
4079 1 7
ZK Team

i have following code veicoloImp:

package model;

import java.util.LinkedList;
import java.util.List;

import java.sql.*;

public class veicoloImp implements veicoloServ {

List<veicolo> allAuto= new LinkedList<veicolo>();
public veicoloImp() {
    try {
        String url="jdbc:mysql://localhost:3306/odin";
        Class.forName("com.mysql.jdbc.Driver");
        Connection conn=DriverManager.getConnection(url,"root","stage123");
        Statement stmt= conn.createStatement();
        ResultSet rs= stmt.executeQuery("SELECT *FROM vehiclemodelyear");
        veicolo vei;
        while(rs.next())
        {   
            vei = new veicolo();
            vei.setId(rs.getInt(1));
            vei.setYear(rs.getInt(2));
            vei.setMake(rs.getString(3));
            vei.setModel(rs.getString(4));
            allAuto.add(vei);
            }

        }catch( SQLException | ClassNotFoundException e) 

{e.printStackTrace();}

}

public List<veicolo>findAll(){
    return allAuto;
    }

public List<veicolo> search(String keyword){
    List<veicolo> result = new LinkedList<veicolo>();

    if (keyword==null || "".equals(keyword)){
        result = allAuto;
    }else{
        for (veicolo c: allAuto){
            if (c.getModel().toLowerCase().contains(keyword.toLowerCase())
                ||c.getMake().toLowerCase().contains(keyword.toLowerCase())){
                result.add(c);
            }
        }
    }
    return result;
}
}

veicoloServ:

package model;

import java.util.List;


public interface veicoloServ {
    public List<veicolo>findAll();
    public List<veicolo> search(String keyword);
}

veicoloController:

package controller;

import org.zkoss.zul.*;
import org.zkoss.zul.ext.Selectable;

import java.util.List;
import java.util.Set;

import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.select.SelectorComposer;
import org.zkoss.zk.ui.select.annotation.Listen;
import org.zkoss.zk.ui.select.annotation.Wire;
import org.zkoss.zul.Label;
import org.zkoss.zul.Listbox;
import model.veicolo;
import model.veicoloServ;
import model.veicoloImp;

public class veicoloController extends SelectorComposer<Component> {
/**

 * 
 */
private static final long serialVersionUID = -1772685322161992019L;
@Wire
private Textbox keywordBox;
@Wire 
private Listbox carListBox;
@Wire
private Label modelLabel;
@Wire
private Label makeLabel;
@Wire
private Label yearLabel;


private veicoloServ carService = new veicoloImp();

@Listen("onClick = #searchButton")
public void search(){
    String keyword = keywordBox.getValue();
    List<veicolo> result = carService.search(keyword);
    carListBox.setModel(new ListModelList<veicolo>(result));
}

@Listen("onSelect=#carListBox")
public void show(){
    Set<veicolo> selection=((Selectable<veicolo>)carListBox.getModel()).getSelection();
    if (selection!=null && !selection.isEmpty()){
        veicolo selected = selection.iterator().next();
        yearLabel.setValue(selected.getYear().toString());
        modelLabel.setValue(selected.getModel());
        makeLabel.setValue(selected.getMake());}
    }

}

veicoloViewModel:

package controller;

import java.util.List;
import org.zkoss.bind.annotation.*;
import model.*;

public class veicoloViewModel {
    private String keyword;

    private veicolo selectedCar;

    private veicoloServ carService = new veicoloImp();
    private veicoloServ all=new veicoloImp();
    private List<veicolo> allAuto=all.findAll();

    public void setKeyword(String keyword) {
        this.keyword = keyword;
    }
    public String getKeyword() {
        return keyword;
    }

    public List<veicolo> getCarList(){
        return allAuto;

    }


    public void setSelectedCar(veicolo selectedCar) {
        this.selectedCar = selectedCar;
    }
    public veicolo getSelectedCar() {
        return selectedCar;
    }


    @Command
    @NotifyChange("allAuto")
    public void search(){
        allAuto = carService.search(keyword);
}


}

index.zul

<window title="Search" width="600px" border="normal"
viewModel="@id('vm') @init('controller.veicoloViewModel')">
<hbox align="center">
    Keyword:
    <textbox value="@save(vm.keyword)" />
    <button label="Search"  onClick="@command('search')" />
</hbox>
<listbox rows="3" model="@load(vm.allAuto)" emptyMessage="No car found in the result"
selectedItem="@save(vm.selectedCar)">
    <listhead>
        <listheader label="Year" />
        <listheader label="Model" />
        <listheader label="Make"/>
    </listhead>
    <template name="model">
        <listitem>
            <listcell label="@init(each.year)"></listcell>
            <listcell label="@init(each.model)"></listcell>
            <listcell label="@init(each.make)"></listcell>
        </listitem>
    </template>
</listbox>
<hlayout style="margin-top:20px" width="100%">
    <image width="250px" src="@load(vm.selectedCar.preview)" />
    <vlayout hflex="1">
        <label value="@load(vm.selectedCar.year)" />
        <label value="@load(vm.selectedCar.model)" />
        <label value="@load(vm.selectedCar.make)" />

    </vlayout>
</hlayout>
</window>

when i start on server my list is empty

delete flag offensive retag edit

Comments

it's your getter what's wrong. use vm.carList or change your getter to getAllAuto

chillworld ( 2017-12-07 15:04:21 +0800 )edit

thx i change it to getAllAuto but i still have the same problem :(

mutandaoscura ( 2017-12-07 16:32:48 +0800 )edit

Ik don't know why you have posted the controller as you use MVVM, but if list is empty, check if your getter is invoked and print the size of it in logging

chillworld ( 2017-12-07 21:44:16 +0800 )edit

with a main class i invoke getallAuto and findAll to see if i get something and they return to console the same list ,as i expected,[model.veicolo@... ,model.veicolo@.etc]. I put a breakpoint to all get and debug from main,all is ok but if i try to debug from the project on server it skip breakpnt

mutandaoscura ( 2017-12-07 22:33:21 +0800 )edit

you are working with ZK8 or later? otherwise you need to add the BindComposer. if the logging doesn't work, the class is not loaded. So check if you are on the right zul page by altering the page, if you see the pages you are good. Any stacktraces in the logging of the server?

chillworld ( 2017-12-08 01:17:02 +0800 )edit

1 Answer

Sort by ยป oldest newest most voted
0

answered 2017-12-08 08:48:05 +0800

cooperkuo gravatar image cooperkuo
1

or change your getter to

link publish delete flag offensive edit
Your answer
Please start posting your answer anonymously - your answer will be saved within the current session and published after you log in or create a new account. Please try to give a substantial answer, for discussions, please use comments and please do remember to vote (after you log in)!

[hide preview]

Question tools

Follow
2 followers

RSS

Stats

Asked: 2017-12-07 00:02:36 +0800

Seen: 19 times

Last updated: Dec 11 '17

Support Options
  • Email Support
  • Training
  • Consulting
  • Outsourcing
Learn More