0

HELP using ZK & MVC patterns

asked 2009-03-31 15:33:29 +0800

gerhecar gravatar image gerhecar
33 1

updated 2009-03-31 15:34:20 +0800

Hi ZK developers!

I'm starting programming a web application using the ZK technologies. I read some smalltalk about the MVC patterns that can be used with ZK. But I have some problems developing my own application. I have the View (mvc.zul), the Control (Control.java) but I want add the business, the Model (xxx.java). When I try to call from the Control.java to a method of the Model, I receive a java null pointer exception. How can I proceed? what am I doing wrong? (I'm trying to follow the Zlora example)

Thanks in advance


And here is the code:

mvc.zul

<zk>
<window title="composer1 example" border="normal" width="300px" use="org.atos.inicio.controller.Control">
    <grid>
        <rows>
            <row>First Name: <textbox id="firstName" forward="onChange=onFirstName"/></row>
            <row>Last Name: <textbox id="lastName" forward="onChange=onLastName"/></row>
            <row>Full Name: <label id="fullName"/></row>
            <row><button label="OK" forward="onLogin" /><button label="Cancel" /></row>
    	 </rows>
    </grid>
   </window>
</zk>


Control.java

public class Control extends Window implements AfterCompose{

    private Textbox firstName;
    private Textbox lastName;
    private Label fullName;
    protected PruebaModelBusiness pmb;

    //-- AfterCompose --//
    public void afterCompose() {
           //wire variables
        Components.wireVariables(this, this);
        //NO need to register onXxx event listeners
        //auto forward
        Components.addForwards(this, this);
    }

    public void onChange$firstName(Event event) { 
        fullName.setValue(firstName.getValue()+" "+lastName.getValue());
    }
    
    public void onChange$lastName(Event event) {
        fullName.setValue(firstName.getValue()+" "+lastName.getValue());
    }
    
    public void onLogin() {  
	 	System.out.println("hemos llegado aqui");
		pmb.escribeLogin("aa", "bb");
	 	try {
	 		 System.out.println("user + pswd: "+firstName.getValue()+" "+lastName.getValue());
	 		 Executions.getCurrent().sendRedirect("prueba.zul");
			//win.doModal();
		} catch (Exception e) {
		 	System.out.println("hemos llegado error");
		}
  // refresh UI
  }  
}

The Model (is an interface called PruebaModelBusiness and the respective Impl: PruebaModelBusinessImpl)

package org.atos.inicio.model;

public class PruebaModelBusinessImpl implements PruebaModelBusiness {

	protected PruebaModelBusiness _pmb;
	
	public PruebaModelBusinessImpl(){
		_pmb=this;	
	}
	
	@Override
	public void escribeLogin(String Name, String Password) {
			System.out.println("El nombre y password"+ Name+" "+Password);
	}	
}

delete flag offensive retag edit

5 Replies

Sort by » oldest newest

answered 2009-03-31 17:01:21 +0800

robertpic71 gravatar image robertpic71
1275 1

1.) First, you do not need forward="onChange=onFirstName" ÁND autoforward.

Autofoward + the correct name is enough.

2.) You do not create an instance of your mode!
Something like _pmb = new PruebaModelBusiness();

/Robert

link publish delete flag offensive edit

answered 2009-03-31 17:09:58 +0800

hideokidd gravatar image hideokidd
750 1 2

updated 2009-03-31 17:13:09 +0800

Hi,

I didn't try your code, just ask a question from your words,
"When I try to call from the Control.java to a method of the Model, I receive a java null pointer exception."

So I searched your Control.java and found that you declare

 protected PruebaModelBusiness pmb;

and you only use the variable here
pmb.escribeLogin("aa", "bb");

If this is where the exception happened,
please try revising your declaration,
such as

 protected PruebaModelBusiness pmb = new PruebaModelBusiness();

BTW, providing error message would be appreciated.
Thanks.

(oh, I didn't cheat, I swear I didn't refer to Robert's solution before my post)

link publish delete flag offensive edit

answered 2009-03-31 17:15:42 +0800

gerhecar gravatar image gerhecar
33 1

Hi robertpic71,

Thank you about the suggesions!!

but I have a comment:

About the 2nd suggestion, PruebaModelBusiness is an interface for PruebaModelBusinessImpl class, so, if it is needed a constructor, it should be the class, isn't it?

And the instance, it should be instantiated on the afterCompose method of the controller class?

Thanks!

link publish delete flag offensive edit

answered 2009-03-31 19:05:29 +0800

YamilBracho gravatar image YamilBracho
1722 2

It is no clear the usage of the PruebaModelBusiness...
Chen you reach the onLogin event the "pbm" var is null because it was not created as instance..

link publish delete flag offensive edit

answered 2009-04-01 12:56:05 +0800

gerhecar gravatar image gerhecar
33 1

Hi All,

Thank very much for your help!!!

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-03-31 15:33:29 +0800

Seen: 245 times

Last updated: Apr 01 '09

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