0

MVC and databinding

asked 2009-04-15 13:36:40 +0800

fusion35 gravatar image fusion35
237 2 5

Hi :

Using the MVC I am tying populate the form fields, which I am not being able to do. Here is the code :

The .zul file

<window id="applicantWindow" title="Applicant Form" border="normal" closable="true" use="the.ApplicantController">
		<grid>
			<rows>
				<row>
					<label value="Name of the Applicant" />
					<hbox>
						<textbox id="name" width="400" />
						<space spacing="5em" />
                         <listbox id="lbCategory" mold="select"/>
					</hbox>
				</row>

				<row>
					<label value="Father/Husband Name" />
					<hbox>
						<textbox id="fathername" width="400" />
						<radiogroup>
							<radio label="Father" checked="true" />
							<radio label="Husband" />
						</radiogroup>
					</hbox>
				</row>
</grid>
</window>

The Controller


public class ApplicantController extends Window implements AfterCompose {
    private static Logger log =  LoggerFactory.getLogger(ApplicantController.class);

    // Variables that are being autowired

        private Listbox lbCategory;
        private Listbox lbMailState;
        private Listbox lbPermanentState;
        private Listbox lbPropertyType;
        
    //
    protected Map params = Executions.getCurrent().getArg();

    private MasterApplicant applicant;

    protected AnnotateDataBinder binder;

    public ApplicantController() {
        super();
    }
    
    public void afterCompose() {
   		Components.wireVariables(this, this);
		Components.addForwards(this, this);
    }

    public void onCreate$applicantWindow() {
        try {
            applicant = (MasterApplicant) params.get("applicant");

            binder = new AnnotateDataBinder(this);
            binder.loadAll();

        } catch (DAOException ex) {
            log.error("onCreate$applicantWindow : "+ex.getMessage());
        }
    }

    public void onClick$btnCopy() {

    }

    public void onClick$btnSave() {       
       binder.saveAll();

        try {
            ApiManager.getMasterApplicantDao().createMasterApplicant(applicant);
        } catch (DAOException ex) {
            java.util.logging.Logger.getLogger(ApplicantController.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public MasterApplicant getApplicant() {
        return applicant;
    }

    public void setApplicant(MasterApplicant applicant) {
        this.applicant = applicant;
    }
}

The Bean :

public class MasterApplicant {

    private String name;
    private String fathername;


    public String getFathername() {
        return fathername;
    }

    public void setFathername(String fathername) {
        this.fathername = fathername;
    }


    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public String toString() {

        StringBuffer sb = new StringBuffer();
        sb.append("[");
        sb.append(", name=");
        sb.append(name);
        sb.append(", fathername=");
        sb.append(fathername);
        sb.append("]");
        return sb.toString();
    }


The binder.saveAll(), doesn't populate the bean. What is it that I am missing?

regards
Devinder

delete flag offensive retag edit

14 Replies

Sort by ยป oldest newest

answered 2009-04-15 13:54:58 +0800

kkurt gravatar image kkurt
300 1 2

Hi Devinder;
First you have to use databinder :

<?init use="org.zkoss.zkplus.databind.AnnotateDataBinderInit" arg0="applicantWindow" ?/> 

and then dont use "use", use "apply" for controller :
<window id="applicantWindow" title="Applicant Form" border="normal" closable="true" use="the.ApplicantController">
...

link publish delete flag offensive edit

answered 2009-04-15 14:07:44 +0800

fusion35 gravatar image fusion35
237 2 5

updated 2009-04-15 14:10:06 +0800

Thanks for the reply.

I have the databinder initialized in the controller :

binder = new AnnotateDataBinder(this);
binder.loadAll();

Do I still need to use the initiator?

Actually I was taking a clue from here
regards

Devinder

link publish delete flag offensive edit

answered 2009-04-15 14:36:21 +0800

kkurt gravatar image kkurt
300 1 2

hmm sorry i see now, you are extending window, using "use" is ok.
try adding "<?init use="org.zkoss.zkplus.databind.AnnotateDataBinderInit" arg0="applicantWindow" ?/>", and we will see the result, i dont know :).
but i am preferring this mvc modelling style:
zul file:

<?init use="org.zkoss.zkplus.databind.AnnotateDataBinderInit" arg0="applicantWindow" ?/> 
<window id="applicantWindow" title="Applicant Form" border="normal" closable="true" use="the.ApplicantController">
...


controller:
public class ApplicantController extends GenericForwardComposer  {
@Override
public void doAfterCompose(Component comp) throws Exception {
//No need to this lines :
//Components.wireVariables(this, this);
//Components.addForwards(this, this);
....
}

public void onCreate$applicantWindow(ForwardEvent event) {
...
}

link publish delete flag offensive edit

answered 2009-04-15 16:08:32 +0800

robertpic71 gravatar image robertpic71
1275 1

???

Sorry, i can't see any databinding here. Where are the bindings for the GUI??

i.e.

<textbox id="fathername" value="@{masterApplicant.fathername}" width="400" />

I think you mix 2 options to build up the GUI:
Variant 1: Use an renderer to build the list/grid. The renderer is called for every modelentry to translate the bean to an grid/listitem-entry.
Variant 2: Use the AnnotateDataBinder, setup a "template" for lists (Listbox, Grid) and mark values with ="@{xxx}" inside the zul-file, offer getters for the models and getter/setters for the values.

Variant 3 (Expression and forEach)
Variant 4 (plain Java)

TerryTornados example from you thread is an example for Variant 1 (w/o @databinding.
Here is an example for @databinding.

/Robert

link publish delete flag offensive edit

answered 2009-04-15 16:48:41 +0800

fusion35 gravatar image fusion35
237 2 5

Thanks for the reply.

In my case I have textboxes (name and fathername) that I expect to be mapped with the beans.

Also like terry's example I want strict MVC where I don't want the zul to contain the value="@..."(as you noted).


Question :

    public void onCreate$applicantWindow() {
        try {
            applicant = (MasterApplicant) params.get("applicant");

            binder = new AnnotateDataBinder(this);
            binder.loadAll();

        } catch (DAOException ex) {
            log.error("onCreate$applicantWindow : "+ex.getMessage());
        }
    }

Shouldn't binder.loadAll() do the stuff fo me?


regards

Devinder

link publish delete flag offensive edit

answered 2009-04-15 20:25:34 +0800

robertpic71 gravatar image robertpic71
1275 1

>> Shouldn't binder.loadAll() do the stuff fo me?
AnnotateDataBinder requires annotations - and this is MVC-conform (like all other webbindings, like jsf).

>> like terry's example
Terry's example works w/o AnnotateDatabinder, he use a model and the listrender:

public void onCreate$branchListWindow(Event event) throws Exception {
		...
		// Set the ListModel and the itemRenderer. The ZKoss ListmodelList do in
		// most times satisfy your needs
		listBoxBranch.setModel(new ListModelList(getBrancheService().getAlleBranche()));
                ..
		listBoxBranch.setItemRenderer(new BranchListModelItemRenderer());
	}

/Robert

link publish delete flag offensive edit

answered 2009-04-15 21:04:33 +0800

fusion35 gravatar image fusion35
237 2 5

updated 2009-04-15 21:07:39 +0800

Thanks for the reply.

Okay, that means I need no binder and need to do the set/get zul components by myself, right?

Then a look at the Terry's code suggest that these lines shouldn't be there :

protected AnnotateDataBinder binder;

public void doOnCreateCommon(Window w) throws Exception {
		binder = new AnnotateDataBinder(w);
		binder.loadAll();
		// ws = Workspace.getWorkspace();

	}

This I guess is there for a change in mind (use annotation)

regards

Devinder

link publish delete flag offensive edit

answered 2009-04-15 22:35:27 +0800

robertpic71 gravatar image robertpic71
1275 1

Yes, this code is obsolete for this example. But he uses the binder inside the abstract class BaseCtrl - maybe this class should be generic. The BaseCtrl could handle @databinding, but the example do not use annotations.

Here is an example for renderer: It is called for every object inside the model and creates the detail for the Listitem (in your case the RowRenderer for each row).

public class OsebaListModelItemRenderer implements ListitemRenderer {

 @Override
 public void render(Listitem item, Object data) throws Exception {
  new Listcell(((Oseba)data).getIme()).setParent(item);
  new Listcell(((Oseba)data).getPriimek()).setParent(item);

  item.setAttribut("data", data);

  ComponentsCtrl.applyForward(item, "onDoubleClick=onDoubleClicked");
 }
}

/Robert

link publish delete flag offensive edit

answered 2009-04-16 04:26:28 +0800

fusion35 gravatar image fusion35
237 2 5

Alright.

Thank you both for your time and interest in the thread.

regards

Devinder

link publish delete flag offensive edit

answered 2009-04-16 09:10:43 +0800

terrytornado gravatar image terrytornado flag of Germany
9393 3 7 16
http://www.oxitec.de/

updated 2009-04-16 09:17:42 +0800

@fusion35

Hi, sorry i oversee this thread.

Yes, like Robert says the piece of code

protected AnnotateDataBinder binder;

public void doOnCreateCommon(Window w) throws Exception {
		binder = new AnnotateDataBinder(w);
		binder.loadAll();

it's obsolet for the example code for the branch modul.

As you can see this is standing in the BaseCtrl.java.
It's a AllInOne Base Controller for all windows.
1. if needed do the Annotate DataBinding
2. wire the zul-components id's
3. wire the forwarder

By extending my windowControllers from this baseCtrl i have nothing
to do more with this stuff. And can only concentrate me on my java code for the logic.
Thanks to hkn for this class.
And yes i'm try to do strong mvc (not easy in zk), so i use not dataBinding in zul.

regards
Stephan

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-15 13:36:40 +0800

Seen: 3,148 times

Last updated: Dec 29 '09

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