Quick Start Guide

Form Processing Actions

Form Processing Actions receive forms in a POST request, validates them, updates models if successful, and redirects to Viewer Actions.

First, create a form derived from AbstractFormattableForm, such as below, and annotate fields for their conversion and validation rules. See Form Field Annotations - Built-in.

 

public class PrimeMinisterForm extends AbstractFormattableForm {

    private static final long serialVersionUID = -6937072630784482008L;

    

    @Trim

    @Required(message = "A name is required")

    private String name;

    

    @DateConversion(message = "Date of Birth, if set, must be in dd/mm/yyyy format")

    private Date dateOfBirth;



    

    public PrimeMinisterForm() {

        // Default constructor required

    }

    

    public PrimeMinisterForm(PrimeMinisterDTO primeMinister) throws Exception {

        // Uses commons-beanutils

        PropertyUtils.copyProperties(this, primeMinister);

    }

    

    

    public String getName() {

        return name;

    }

    public void setName(String name) {

        this.name = name;

    }



    public Date getDateOfBirth() {

        return dateOfBirth;

    }

    public void setDateOfBirth(Date dateOfBirth) {

        this.dateOfBirth = dateOfBirth;

    }

    

}

Second, create the Action derived from AbstractFormDrivenActionSupport using the created form as the type parameter, such as below. Use of getBrowserTabSession() allows server state per browser tab.


@InterceptorRef("FormDrivenStack") 

@Results({ 

     @Result(name=ActionSupport.INPUT, type="redirectAction", params = {"namespace", "/", "actionName", "ViewPrimeMinister"}),

     @Result(name=ActionSupport.SUCCESS, type="redirectAction", params = {"namespace", "/", "actionName", "ViewPrimeMinister"})

 }) 

public class UpdatePrimeMinisterAction extends AbstractFormDrivenActionSupport<PrimeMinisterForm> { 

    private static final long serialVersionUID = -7979376337555804889L;

    

    private Logger LOG = LogManager.getLogger(UpdatePrimeMinisterAction.class);

    

    @Override

    protected Logger getLogger() {

        return LOG;

    }



    @Override

    protected PrimeMinisterForm makeForm() {

        return new PrimeMinisterForm();

    }





    @Action("/UpdatePrimeMinister")

    @Override

    public String execute() throws Exception {

        HttpSession session = getBrowserTabSession();

        PrimeMinisterForm form = getForm();

        // Update database here        

        return SUCCESS;

    }



}

Viewer Actions

Viewer Actions are invoked by a GET request to display data. They usually display messages and rejected forms from Form Processing Actions.

Create the Action derived from AbstractViewActionSupport, such as below. Use of getBrowserTabSession() allows server state per browser tab.


@InterceptorRef("ViewStack") 

@Results({ 

    @Result(name=ActionSupport.SUCCESS, location="/pages/PrimeMinister.jsp")

 }) 

public class ViewPrimeMinisterAction extends AbstractViewActionSupport { 

    private static final long serialVersionUID = -7979376337555804889L;

    

    private Logger LOG = LogManager.getLogger(ViewPrimeMinisterAction.class);

    

    // Fields here

    

    @Override

    protected Logger getLogger() {

        return LOG;

    }





    @Action("/ViewPrimeMinister")

    @Override

    public String execute() throws Exception {

        HttpSession session = getBrowserTabSession();

        // Read database and update fields here        

        return SUCCESS;

    }



    // Field getters and setters here

}

  

The forms of Form Processing Actions that redirect to this Action should be added as member fields, such as below. By default, if a form's validation fails, it's injected into the Viewer Action. Reception can be set with the Form annotation. The execute function should initialise any missing forms, usually from the database record to be edited.


public class ViewPrimeMinisterAction extends AbstractViewActionSupport {

    ...

    private PrimeMinisterForm form;

    ...

    

    public String execute() throws Exception {

        ...

        if (form == null) {

            form = new PrimeMinisterForm(primeMinister);

        }

        ...

    }

    

    public PrimeMinisterForm getForm() {

        return form;

    }



} 

  

JSP Pages

The browsertab2.js JavaScript library is required for server state per browser tab, installed such as below.

<HEAD>

  ...

  <SCRIPT TYPE="text/javascript" SRC="<s:url value="/js/browserTab2.js" />"></SCRIPT>

  ...

</HEAD>