Create (Using BeanEditForm) (1)

This page is identical to the Edit (Using BeanEditForm) example except this example instantiates an empty Person before rendering the page and it uses a business method that creates a Person instead of changing a Person. Also, it does not need to include the version field because optimistic locking is not involved during create.
References: BeanEditForm, Using BeanEditForm, beaneditor package.


The source for IPersonManagerServiceLocal and @EJB is shown in the Session Beans and @EJB examples.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "">
<!-- We need a doctype to allow us to use special characters like &nbsp; 
     We use a "strict" DTD to make IE follow the alignment rules. -->
<html xmlns:t="">
<body class="container">
    <h3>Create (Using BeanEditForm) (1)</h3>
    This page is identical to the <em>Edit (Using BeanEditForm)</em> example except this example instantiates an empty Person 
    before rendering the page and it uses a business method that creates a Person instead of changing a Person. 
    Also, it does not need to include the <em>version</em> field because optimistic locking is not involved during create.
    <div class="eg">
        <t:beaneditform t:id="personForm" object="person" include="firstName,lastName,region,startDate" submitLabel="Save"/>
        <t:eventlink event="refresh">Refresh</t:eventlink>

    <a href="">BeanEditForm</a>, 
    <a href="">Using BeanEditForm</a>, 
    <a href="">beaneditor package</a>.<br/><br/>

    <t:pagelink page="Index">Home</t:pagelink><br/><br/>
    The source for IPersonManagerServiceLocal and @EJB is shown in the Session Beans and @EJB examples.<br/><br/>

        <t:sourcecodetab src="/web/src/main/java/jumpstart/web/pages/examples/input/Create1.tml"/>
        <t:sourcecodetab src="/web/src/main/java/jumpstart/web/pages/examples/input/"/>
        <t:sourcecodetab src="/web/src/main/resources/META-INF/assets/css/examples/plain.css"/>
        <t:sourcecodetab src="/business/src/main/java/jumpstart/business/domain/person/"/>
        <t:sourcecodetab src="/business/src/main/java/jumpstart/business/domain/person/"/>

package jumpstart.web.pages.examples.input;

import javax.ejb.EJB;

import jumpstart.util.ExceptionUtil;

import org.apache.tapestry5.annotations.Import;
import org.apache.tapestry5.annotations.InjectComponent;
import org.apache.tapestry5.annotations.InjectPage;
import org.apache.tapestry5.annotations.Property;
import org.apache.tapestry5.corelib.components.BeanEditForm;

@Import(stylesheet = "css/examples/plain.css")
public class Create1 {

    private final String demoModeStr = System.getProperty("jumpstart.demo-mode");

    // Screen fields

    private Person person;

    // Other pages

    private Create2 page2;

    // Generally useful bits and pieces

    private BeanEditForm personForm;

    private IPersonManagerServiceLocal personManagerService;

    // The code

    // PersonForm bubbles up the PREPARE_FOR_RENDER event when it is rendered

    void onPrepareForRender() throws Exception {

        // If fresh start, make sure there's a Person object available.

        if (personForm.isValid()) {
            person = new Person();

    // PersonForm bubbles up the PREPARE_FOR_SUBMIT event when it is submitted

    void onPrepareForSubmit() throws Exception {
        // Instantiate a Person for the form data to overlay.
        person = new Person();

    void onValidateFromPersonForm() {

        if (person.getFirstName() != null && person.getFirstName().equals("Acme")) {
            personForm.recordError("First Name must not be Acme.");

        if (demoModeStr != null && demoModeStr.equals("true")) {
            personForm.recordError("Sorry, but this function is not allowed in Demo mode.");

        if (personForm.getHasErrors()) {
            // We get here only if a server-side validator detected an error.

        try {
        catch (Exception e) {
            // Display the cause. In a real system we would try harder to get a user-friendly message.

    Object onSuccess() {
        return page2;

    void onRefresh() {
        // By doing nothing the page will be displayed afresh.

.eg {
                margin: 20px 0;
                padding: 14px;
                border: 1px solid #ddd;
                border-radius: 6px;
                -webkit-border-radius: 6px;
                -mox-border-radius: 6px;


import java.util.Date;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.PrePersist;
import javax.persistence.PreUpdate;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.Version;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;

 * The Person entity.
public class Person implements Serializable {

    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(nullable = false)
    private Long id;

    @Column(nullable = false)
    private Integer version;

    @Column(length = 10, nullable = false)
    @Size(max = 10)
    private String firstName;

    @Column(length = 10, nullable = false)
    @Size(max = 10)
    private String lastName;
    private Regions region;

    private Date startDate;

    public String toString() {
        final String DIVIDER = ", ";
        StringBuilder buf = new StringBuilder();
        buf.append(this.getClass().getSimpleName() + ": ");
        buf.append("id=" + id + DIVIDER);
        buf.append("version=" + version + DIVIDER);
        buf.append("firstName=" + firstName + DIVIDER);
        buf.append("lastName=" + lastName + DIVIDER);
        buf.append("region=" + region + DIVIDER);
        buf.append("startDate=" + startDate);
        return buf.toString();

    // Default constructor is required by JPA.
    public Person() {

    public Person(String firstName, String lastName, Regions region, Date startDate) {
        this.firstName = firstName;
        this.lastName = lastName;
        this.region = region;
        this.startDate = startDate;

    // The need for an equals() method is discussed at
    public boolean equals(Object obj) {
        return (obj == this) || (obj instanceof Person) && id != null && id.equals(((Person) obj).getId());

    // The need for a hashCode() method is discussed at

    public int hashCode() {
        return id == null ? super.hashCode() : id.hashCode();

    public void validate() throws ValidationException {


    public Long getId() {
        return id;

    public Integer getVersion() {
        return version;

    public void setVersion(Integer version) {
        this.version = version;

    public String getFirstName() {
        return firstName;

    public void setFirstName(String firstName) {
        this.firstName = firstName;

    public String getLastName() {
        return lastName;

    public void setLastName(String lastName) {
        this.lastName = lastName;

    public Regions getRegion() {
        return region;

    public void setRegion(Regions region) {
        this.region = region;

    public Date getStartDate() {
        return startDate;

    public void setStartDate(Date startDate) {
        this.startDate = startDate;



public enum Regions {