0

Assignig a property from Spring bean to ZK page

asked 2009-04-16 20:08:34 +0800

Khosro gravatar image Khosro
48

updated 2009-04-27 05:32:10 +0800

Hello ,
I have a spring bean called(candidatePage) and in this page i have a property "List<News> pageDataModel".
I want to access the member of this property in a Listbox in zk page,and also i want paging.
I have tired this:

<?init class="org.zkoss.zk.ui.util.Composition" arg0="/WEB-INF/template-normal.zul"?>
<?init class="org.zkoss.zkplus.databind.AnnotateDataBinderInit"?>
<?variable-resolver class="org.zkoss.zkplus.spring.DelegatingVariableResolver"?>

<zk xmlns="http://www.zkoss.org/2005/zul"
xmlns:h="http://www.w3.org/1999/xhtml"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.zkoss.org/2005/zul http://www.zkoss.org/2005/zul/zul.xsd"
xmlns:c="http://www.zkoss.org/dsp/web/core"
xmlns:a="http://www.zkoss.org/2005/zk/annotation">

<window id="mainwindow" self="@{define(content)}" title="${c:l('user.search.title')}" width="820px" border="normal"
apply="${candidatePage}">

<!--<listitem self="@{each=userList}"> -->

<listbox id="list" mold="paging" apply="${candidatePage}" >

<listitem forEach="${candidatePage.pageDataModel}" >
<listcell>
<label value="${each.id}"/>
</listcell>
</listitem>
</listbox>
<paging id="page_1" pageSize="10"/>
</window>
</zk>


but first time when the application start it does not show anything but after loading it again ,show the id of news,but paging does not work,and only show the first page,but i am sure there us at least 22 pages.
My bean is:

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package ir.ideacenter.ui.page;

import org.compass.core.CompassDetachedHits;
import org.zkoss.zk.ui.util.GenericForwardComposer;
import ir.ideacenter.biz.model.News;
import ir.ideacenter.biz.model.Agency;
import ir.ideacenter.biz.model.Subject;
import ir.ideacenter.biz.service.AgencyService;
import ir.ideacenter.biz.service.NewsSearchService;
import ir.ideacenter.biz.service.NewsService;
import ir.ideacenter.biz.service.SubjectService;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.zkoss.zhtml.Li;
import org.zkoss.zhtml.Text;
import org.zkoss.zhtml.Ul;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.util.GenericForwardComposer;
import org.zkoss.zul.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.ArrayList;
import java.util.Calendar;
import javax.annotation.PostConstruct;
import org.compass.core.CompassHit;
import org.springframework.web.context.ContextLoader;
import org.zkoss.zk.ui.Page;
import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.event.EventListener;
import org.zkoss.zk.ui.util.Composer;
import org.zkoss.zul.event.PagingEvent;

/**
*
* @author Khosro
*/
@Controller
@Scope("session")
public class CandidatePage extends GenericForwardComposer implements EventListener{

@Resource
NewsService newsService;
List<News> newsList = new ArrayList<News>();
Listbox list;
NewsSearchService newsSearchService;
Paging page_1;
List<Long> idList1;
int PAGE_SIZE1 ;
private CompassDetachedHits hits;
private List<News> pageDataModel;
private SimpleListModel listModelList;

public void setPageDataModel(List<News> pageDataModel) {
this.pageDataModel = pageDataModel;
}

public List<News> getPageDataModel() {
return pageDataModel;
}

public SimpleListModel getListModelList() {
return listModelList;
}

public void setListModelList(SimpleListModel listModelList) {
this.listModelList = listModelList;
}

public void setPage_1(Paging page_1) {
this.page_1 = page_1;
}

public Paging getPage_1() {
return page_1;
}
String s;

@Override
public void doAfterCompose( Component comp1) throws Exception {
super.doAfterCompose(comp1);
PAGE_SIZE1=page_1.getPageSize();
newsSearchService = (NewsSearchService) ContextLoader.getCurrentWebApplicationContext().getBean("newsSearchService");
s = execution.getParameter("query");
//s = new String( s.getBytes("UTF-8"), "UTF-8");
int countAll = newsSearchService.countAll(s);
page_1.setTotalSize(countAll);
Object[] newses = newsSearchService.findRange(s, 0, 10);
List<Long> idList = new ArrayList<Long>();
for (int i = 0; i < newses.length; i++) {
News n = (News) newses;
idList.add(n.getId());
}
pageDataModel = newsService.findById(idList.toArray(new Long[0]));
page.addEventListener("onPaging", this);
}
@Override
public void onEvent(Event event) throws Exception {
PagingEvent pe = (PagingEvent) event;
// int pgno = pe.getActivePage();
int pgno = page_1.getActivePage();
int ofs = pgno * PAGE_SIZE1;
hits = newsSearchService.findAndDetach(s, ofs, PAGE_SIZE1);
idList1 = new ArrayList<Long>();
for (CompassHit hit : hits) {
News n = (News) hit.data();
idList1.add(n.getId());
}
pageDataModel = newsService.findById(idList1.toArray(new Long[0]));

}
}

I am really got confused.
What must i do?

Khosro.

delete flag offensive retag edit

3 Replies

Sort by ยป oldest newest

answered 2009-04-17 01:31:30 +0800

hideokidd gravatar image hideokidd
750 1 2

updated 2009-04-17 01:32:20 +0800

Hi,

Your code seems complicated, I just propose personal opinions,

1.In your zul file, it's wield that you apply two components to the same composer class

<window id="mainwindow" self="@{define(content)}" title="${c:l('user.search.title')}" width="820px" border="normal"
apply="${candidatePage}">
<listbox id="list" mold="paging" apply="${candidatePage}" >

But I have no idea if this will mess up something,
in normal case we only apply the window to a composer because we can access any children of it by composer

2.Do the following code work?

apply="${candidatePage}"

Since it seems you didn't import this class, and I always make the code like
apply="${ir.ideacenter.ui.page.candidatePage}"

3.Is your spring setting corrent? Annotation works?
http://docs.zkoss.org/wiki/Spring

4.Summary: please make sure
(1)Does composer class is called?
(2)Is spring setting corrent?

link publish delete flag offensive edit

answered 2009-04-27 05:54:00 +0800

Khosro gravatar image Khosro
48

updated 2009-04-27 05:55:44 +0800

Hello hideokidd,
First of all,excuse me for delay for answer,because ,i was working on a project, and i faced this problem and i was too busy and also i had another option to solve this problem so i chose that,and i must deliver it soon , then i could not answer you (at last I created the layout in Java code).

My Problem:

I want to have a complex layout in one listcell in listbox and for that i must use vbox and hbox and so on,for example i have a object and this object has some property and in a controller i retrieve this object from DB and want to display the value of this object in zul page,so ,is there any way to bind the object in controller to zul page.
I know if my controller class extends GenericForwardComposer class i can access in controller the component that i have defined in zul page and vice versa....but the problem is that how can i bind the property(that is object) in controller to zul page (something like JSF)and retrive the value of property of the that object.
That is all my problem.


>>1.In your zul file, it's wield that you apply two components to the same composer class

Yes you are right i removed the apply in listbox but it does not work.

>>>2.Do the following code work?
>>

apply="${candidatePage}"

yes it works

>>3.Is your spring setting corrent? Annotation works?

Yes,i am sure ,because i can access the component that i have defined in zul page in controller.


>>>(1)Does composer class is called?
Yes

>>>(2)Is spring setting corrent?
Yes

In the end ,i want something like JSF(binding the property in backing bean to page) without using it in ZK.

Khosro.

link publish delete flag offensive edit

answered 2009-04-27 06:51:55 +0800

Khosro gravatar image Khosro
48

updated 2009-04-27 06:53:04 +0800

Hello to all,
I found the Solution:

My ZK Page:

<window id="mainwindow"  width="820px" border="normal" apply="${binding}">
        <listbox id="list" mold="paging" model="@{binding.pageDataModel}">
            <listitem self="@{each=pageDataModel}" >
                <listcell>
                    <label value="@{pageDataModel.id}"/>
                </listcell>
                <listcell>
                    <label value="@{pageDataModel.title}"/>
                </listcell>
            </listitem>
        </listbox>
        <paging id="page_1" pageSize="10"/>
    </window>

My Controller:

@Controller
@Scope("prototype")
public class Binding extends GenericForwardComposer implements EventListener{

    @Resource
    NewsService newsService;
    List<News> newsList = new ArrayList<News>();
    Listbox list;
    @Resource
    NewsSearchService newsSearchService;
    @Resource
    SubjectService subjectService;
    Paging page_1;
    List<Long> idList1;
    int PAGE_SIZE1;
    private CompassDetachedHits hits;
    private List<News> pageDataModel;
    

    public void setPageDataModel(List<News> pageDataModel) {
        this.pageDataModel = pageDataModel;
    }

    public List<News> getPageDataModel() {
        return pageDataModel;
    }
    String sub;
    @Override
    public void doAfterCompose(Component comp1) throws Exception {
        super.doAfterCompose(comp1);
        PAGE_SIZE1 = page_1.getPageSize();

       sub=subjectService.findAll().get(1).getQuery();
        int countAll = newsSearchService.countAll(sub);
        page_1.setTotalSize(countAll);
        Object[] newses = newsSearchService.findRange(sub, 0, 10);
        List<Long> idList = new ArrayList<Long>();
        for (int i = 0; i < newses.length; i++) {
            News n = (News) newses<i >;
            idList.add(n.getId());
        }
        pageDataModel = newsService.findById(idList.toArray(new Long[0]));
        page.addEventListener("onPaging", this);
    }

    @Override
    public void onEvent(Event event) throws Exception {
        int pgno = page_1.getActivePage();
        int ofs = pgno * PAGE_SIZE1;
        hits = newsSearchService.findAndDetach(sub, ofs, PAGE_SIZE1);
        idList1 = new ArrayList<Long>();
        for (CompassHit hit : hits) {
            News n = (News) hit.data();
            idList1.add(n.getId());
        }
        pageDataModel = newsService.findById(idList1.toArray(new Long[0]));
        list.setModel(new SimpleListModel(pageDataModel));
    }
}

Khosro

link publish delete flag offensive edit
Your reply
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

RSS

Stats

Asked: 2009-04-16 20:08:34 +0800

Seen: 584 times

Last updated: Apr 27 '09

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