Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Scrollbar

...

So, you fill in all the fields, submit the form (without validation errors) and voila: you get back the same form, blanked out. What happened, and where did the data go?

...

But what entities? Normally, the available entities are listed inside hibernate.cfg.xml, but that's not necessary with Tapestry; in another example of convention over configuration, Tapestry locates all entity classes inside the entities package ("com.example.tutorialtutorial1.entities" in our case) and adds them to the configuration. Currently, that is just the Address entity.

...

Code Block
languagejava
titlesrc/main/java/com/example/tutorial/entities/Address.java
package com.example.tutorialtutorial1.entities;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;

import org.apache.tapestry5.beaneditor.NonVisual;
import org.apache.tapestry5.beaneditor.Validate;

import com.example.tutorialtutorial1.data.Honorific;

@Entity
public class Address
{
  @Id
  @GeneratedValue(strategy = GenerationType.IDENTITY)
  @NonVisual
  public Long id;

  public Honorific honorific;

  @Validate("required")
  public String firstName;

  @Validate("required")
  public String lastName;

  public String street1;

  public String street2;

  @Validate("required")
  public String city;

  @Validate("required")
  public String state;

  @Validate("required,regexp")
  public String zip;

  public String email;

  public String phone;
}

...

Code Block
languagejava
titlesrc/main/java/com/example/tutorial/pages/address/CreateAddress.java
package com.example.tutorialtutorial1.pages.address;

import com.example.tutorialtutorial1.entities.Address;
import com.example.tutorialtutorial1.pages.Index;
import org.apache.tapestry5.annotations.InjectPage;
import org.apache.tapestry5.annotations.Property;
import org.apache.tapestry5.hibernate.annotations.CommitAfter;
import org.apache.tapestry5.ioc.annotations.Inject;
import org.hibernate.Session;

public class CreateAddress
{
    @Property
    private Address address;

    @Inject
    private Session session;

    @InjectPage
    private Index index;

    @CommitAfter
    Object onSuccess()
    {
        session.persist(address);

        return index;
    }
}

...

As a little preview of what's next, let's display all the Addresses entered by the user on the Index page of the application. After you enter a few names, it will look something like:

Image Modified

Adding the Grid to the Index page

...

Code Block
languagejava
titlesrc/main/java/com/example/tutorial/pages/Index.java
package com.example.tutorialtutorial1.pages;
import java.util.List;
import org.apache.tapestry5.ioc.annotations.Inject;
import org.hibernate.Session;
import com.example.tutorialtutorial1.entities.Address;
public class Index
{
    @Inject
    private Session session;
    public List<Address> getAddresses()
    {
        return session.createCriteria(Address.class).list();
    }
}

...

We have lots more to talk about: more components, more customizations, built-in Ajax support, more common design and implementation patterns, and even writing your own components (which is easy!).

...

...

...

...

...


Scrollbar

...