-
FEATURED COMPONENTS
First time here? Check out the FAQ!
1 | initial version | |
A little bit modifications, for a better basic skeleton. You must adapt it to your needs.
zul-file for playing
<?xml version="1.0" encoding="UTF-8" ?>
<?taglib uri="http://www.zkoss.org/dsp/web/core" prefix="c"?>
<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">
<!-- +++++++++++++++++++++++++++++++++++++++++++++++++++ -->
<!-- DataBinding Initiator. -->
<!-- +++++++++++++++++++++++++++++++++++++++++++++++++++ -->
<?init class="org.zkoss.zkplus.databind.AnnotateDataBinderInit" arg0="./windowGrambergTest" ?>
<window id="windowGrambergTest"
apply="de.gramberg.webui.test.GrambergTestCtrl" border="none"
width="100%">
<button id="btnTest1" label="changed beanProperties to Willis" />
<button id="btnTest2" label="changed beanProperties to Hanks" />
<vbox>
<textbox id="txtb_personLastname"
value="@{controller.person.lastName}" readonly="true" />
<textbox id="txtb_personFirstname"
value="@{controller.person.firstName}" readonly="true" />
<textbox id="txtb_personCity"
value="@{controller.person.city}" readonly="true" />
<textbox id="txtb_personStreet"
value="@{controller.person.street}" readonly="true" />
<textbox id="txtb_personPhone"
value="@{controller.person.phone}" readonly="true" />
</vbox>
<h:hr />
<separator />
<vbox>
<textbox id="txtb_personLastnameNew" />
<button id="btnTestSave" label="change manual LastName" />
</vbox>
</window>
</zk>
. GrambergTestCtrl .java (Java-Controller for the zul-file)
public class GrambergTestCtrl extends GenericForwardComposer implements Serializable {
private static final long serialVersionUID = 1L;
/**
* ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
* All the components that are defined here have a corresponding component
* with the same 'id' in the zul-file. <br>
*
* 1. They are getting autowired by the GenericForwardComposer.<br>
* 2. Their events are 'forwarded' automatically by the
* GenericForwardComposer.<br>
*
* Can access them with followed code:<br>
*
* public void onEventName&componentId(Event event){...} <br>
* public void onClick&btnTest(Event event){...} <br>
* ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
*/
protected Window windowGrambergTest; // autowired
protected Button btnTest1; // autowired
protected Button btnTest2; // autowired
protected Button btnTestSave; // autowired
protected Textbox txtb_personLastnameNew; // autowired
// binding stuff
private AnnotateDataBinder binder = null;
private Person person;
@Override
public void doAfterCompose(Component window) throws Exception {
super.doAfterCompose(window);
// set the composer's name in the zul page as an ALIAS for access.
if (self != null)
self.setAttribute("controller", this, false);
}
/**
* This event is called automatically by zk.
*
* @param event
*/
public void onCreate$windowGrambergTest(Event event) {
System.out.println("EventName/ComponentID : " + event.getName());
binder = (AnnotateDataBinder) event.getTarget().getAttribute("binder", true);
// init the bean with the first record in the personList
Person person = new Person();
person.setId(1100);
person.setFirstName("Tom");
person.setLastName("Hanks");
person.setCity("Chicago");
person.setStreet("5, Boulderdrive");
person.setPhone("123456789");
// init the binding mechanism
setPerson(person);
// beanToUi
binder.loadAll();
}
/**
* Forwarded 'onClick' button event.
*
* @param event
*/
public void onClick$btnTest1(Event event) {
System.out.println("EventName/ComponentID : " + event.getName());
// change the persons name
getPerson().setFirstName("Bruce");
getPerson().setLastName("Willis");
getPerson().setCity("New York");
getPerson().setStreet("Hugo-Menzies-Drive");
getPerson().setPhone("76353535353");
// beanToUi
binder.loadAll();
}
/**
* Forwarded 'onClick' button event.
*
* @param event
*/
public void onClick$btnTest2(Event event) {
System.out.println("EventName/ComponentID : " + event.getName());
// change the persons name
getPerson().setFirstName("Tom");
getPerson().setLastName("Hanks");
getPerson().setCity("Chicago");
getPerson().setStreet("5, Boulderdrive");
getPerson().setPhone("123456789");
// beanToUi
binder.loadAll();
}
/**
* Forwarded 'onClick' button event.
*
* @param event
*/
public void onClick$btnTestSave(Event event) {
System.out.println("EventName/ComponentID : " + event.getName());
// change the persons LastName from the Textbox
getPerson().setLastName(txtb_personLastnameNew.getValue());
// beanToUi
binder.loadAll();
}
// +++++++++++++++++++++++++++++++++++++++++++++++++ //
// ++++++++++++++++ Setter/Getter ++++++++++++++++++ //
// +++++++++++++++++++++++++++++++++++++++++++++++++ //
public void setPerson(Person person) {
this.person = person;
}
public Person getPerson() {
return person;
}
}
Person.java (Test Bean like your Gutacht.java)
public class Person implements Serializable {
private static final long serialVersionUID = -4591732272301777879L;
private int id = 0;
private String firstName = "";
private String lastName = "";
private String city = "";
private String street = "";
private String phone = "";
private String fax = "";
public Person() {
}
public Person(int id, String firstName, String lastName, String city, String street, String phone, String fax) {
this.setId(id);
this.firstName = firstName;
this.lastName = lastName;
this.city = city;
this.street = street;
this.phone = phone;
this.fax = fax;
}
public void setId(int id) {
this.id = id;
}
public int getId() {
return id;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getFirstName() {
return firstName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getLastName() {
return lastName;
}
public void setCity(String city) {
this.city = city;
}
public String getCity() {
return city;
}
public void setStreet(String street) {
this.street = street;
}
public String getStreet() {
return street;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getPhone() {
return phone;
}
public void setFax(String fax) {
this.fax = fax;
}
public String getFax() {
return fax;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((city == null) ? 0 : city.hashCode());
result = prime * result + ((fax == null) ? 0 : fax.hashCode());
result = prime * result + ((firstName == null) ? 0 : firstName.hashCode());
result = prime * result + id;
result = prime * result + ((lastName == null) ? 0 : lastName.hashCode());
result = prime * result + ((phone == null) ? 0 : phone.hashCode());
result = prime * result + ((street == null) ? 0 : street.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Person other = (Person) obj;
if (city == null) {
if (other.city != null)
return false;
} else if (!city.equals(other.city))
return false;
if (fax == null) {
if (other.fax != null)
return false;
} else if (!fax.equals(other.fax))
return false;
if (firstName == null) {
if (other.firstName != null)
return false;
} else if (!firstName.equals(other.firstName))
return false;
if (id != other.id)
return false;
if (lastName == null) {
if (other.lastName != null)
return false;
} else if (!lastName.equals(other.lastName))
return false;
if (phone == null) {
if (other.phone != null)
return false;
} else if (!phone.equals(other.phone))
return false;
if (street == null) {
if (other.street != null)
return false;
} else if (!street.equals(other.street))
return false;
return true;
}
}
best Stephan