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 {@link name.matthewgreet.strutscommons.form.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;
{@code @Trim}
{@code @Required(message = "A name is required")}
private String name;
{@code @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 {@link name.matthewgreet.strutscommons.action.AbstractFormDrivenActionSupport}
using the created form as the type parameter, such as below. Use of getBrowserTabSession() allows
server state per browser tab.
{@code @InterceptorRef("FormDrivenStack") }
{@code @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);
{@code @Override}
protected PrimeMinisterForm makeForm() {
return new PrimeMinisterForm();
}
{@code @Action("/UpdatePrimeMinister")}
{@code @Override}
public String execute() throws Exception {
HttpSession session = getBrowserTabSession();
PrimeMinisterForm form = getForm();
// Update database here
return SUCCESS;
}
{@code @Override}
public Logger getLogger() {
return LOG;
}
}
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 {@link name.matthewgreet.strutscommons.action.AbstractViewActionSupport AbstractViewActionSupport}, such as
below. Use of getBrowserTabSession() allows server state per browser tab.
{@code @InterceptorRef("ViewStack") }
{@code @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
{@code @Action("/ViewPrimeMinister")}
{@code @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 {@link name.matthewgreet.strutscommons.annotation.Form 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);
}
...
}
{@code @Override}
public Logger getLogger() {
return LOG;
}
public PrimeMinisterForm getForm() {
return form;
}
}
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>