package name.matthewgreet.basicstrutsexperiments.experiment4.action;
import org.apache.struts2.ActionSupport;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Actions;
import org.apache.struts2.convention.annotation.InterceptorRef;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.apache.struts2.interceptor.validation.SkipValidation;
import com.opensymphony.xwork2.validator.annotations.VisitorFieldValidator;
import name.matthewgreet.basicstrutsexperiments.experiment4.form.ArithmeticForm;
import name.matthewgreet.basicstrutsexperiments.experiment4.form.DummyForm;
@InterceptorRef("defaultStack")
@Results({
@Result(name=ActionSupport.ERROR, location="/pages/experiment4/Arithmetic.jsp"),
@Result(name=ActionSupport.INPUT, location="/pages/experiment4/Arithmetic.jsp"),
@Result(name=ActionSupport.SUCCESS, location="/pages/experiment4/Arithmetic.jsp")
})
public class ArithmeticAction extends ActionSupport {
private static final long serialVersionUID = -6084392315736424463L;
private ArithmeticForm form;
private DummyForm dummyForm;
private String resultText;
@Actions({
@Action("/experiment4/"),
@Action("/experiment4/Arithmetic")
})
@Override
@SkipValidation
public String execute() throws Exception {
try {
int result;
if (form == null) {
resultText = "";
} else if (form.getOperand2() != null) {
switch (form.getOperator()) {
case "+":
result = form.getOperand1() + form.getOperand2();
break;
case "-":
result = form.getOperand1() - form.getOperand2();
break;
case "/":
result = form.getOperand1() / form.getOperand2();
break;
case "*":
result = form.getOperand1() + form.getOperand2();
break;
default:
result = 0;
break;
}
resultText = form.getOperand1() + " " + form.getOperator() + " " + form.getOperand2() + " = " + result;
} else {
resultText = "Operand 2 not set";
}
return SUCCESS;
}
catch (Exception e) {
System.out.println("Action failed reason=" + e.getMessage());
return ERROR;
}
}
@Action("/Arithmetic_submit")
public String submit() throws Exception {
try {
int result;
if (form.getOperand2() != null) {
switch (form.getOperator()) {
case "+":
result = form.getOperand1() + form.getOperand2();
break;
case "-":
result = form.getOperand1() - form.getOperand2();
break;
case "/":
result = form.getOperand1() / form.getOperand2();
break;
case "*":
result = form.getOperand1() + form.getOperand2();
break;
default:
result = 0;
break;
}
resultText = form.getOperand1() + " " + form.getOperator() + " " + form.getOperand2() + " = " + result;
} else {
resultText = "Operand 2 not set";
}
return SUCCESS;
}
catch (Exception e) {
System.out.println("Action failed reason=" + e.getMessage());
return ERROR;
}
}
@VisitorFieldValidator(appendPrefix = true)
public ArithmeticForm getForm() {
if (form == null) {
form = new ArithmeticForm();
}
return form;
}
@VisitorFieldValidator(appendPrefix = true)
public DummyForm getDummyForm() {
if (dummyForm == null) {
dummyForm = new DummyForm();
}
return dummyForm;
}
public String getResultText() {
return resultText;
}
}
< /code>
Die Formulare sind einfach. < /p>
package name.matthewgreet.basicstrutsexperiments.experiment4.form;
import java.io.Serializable;
import com.opensymphony.xwork2.validator.annotations.ConversionErrorFieldValidator;
import com.opensymphony.xwork2.validator.annotations.RequiredFieldValidator;
import com.opensymphony.xwork2.validator.annotations.RequiredStringValidator;
public class ArithmeticForm implements Serializable {
private static final long serialVersionUID = -5287580238710655353L;
private int operand1;
private String operator;
private Integer operand2;
@ConversionErrorFieldValidator(message = "Operand 1 must be a number!", repopulateField = true, shortCircuit = true)
@RequiredFieldValidator(message = "Operand 1 is required")
public int getOperand1() {
return operand1;
}
public void setOperand1(int operand1) {
this.operand1 = operand1;
}
@RequiredStringValidator(message = "Operator is required")
public String getOperator() {
return operator;
}
public void setOperator(String operator) {
this.operator = operator;
}
@ConversionErrorFieldValidator(message = "Operand 2 must be a number!", repopulateField = true, shortCircuit = true)
@RequiredFieldValidator(message = "Operand 2 is required")
public Integer getOperand2() {
return operand2;
}
public void setOperand2(Integer operand2) {
this.operand2 = operand2;
}
@Override
public String toString() {
return "ArithmeticForm [operand1=" + operand1 + ", operator=" + operator + ", operand2=" + operand2 + "]";
}
}
< /code>
package name.matthewgreet.basicstrutsexperiments.experiment4.form;
import java.io.Serializable;
import com.opensymphony.xwork2.validator.annotations.ConversionErrorFieldValidator;
import com.opensymphony.xwork2.validator.annotations.RequiredFieldValidator;
public class DummyForm implements Serializable {
private static final long serialVersionUID = -5287580238710655353L;
private Integer dummy;
@RequiredFieldValidator(message = "Dummy is required")
@ConversionErrorFieldValidator(message = "Dummy must be a number!", repopulateField = true)
public Integer getDummy() {
return dummy;
}
public void setDummy(Integer value) {
this.dummy = value;
}
@Override
public String toString() {
return "DummyForm [dummy=" + dummy + "]";
}
}
< /code>
/pages/experiment4/Arithmetic.jsp
Experiment 4
Experiment 4
Arithmetic
-
Submit
Dummy
-
Submit
< /code>
The web.xml is standard.
struts2
org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter
struts2
/*
/index.html
/index.jsp
< /code>
No struts.xml is used.
Before the submit function is called, both @VisitorFieldValidator annotations are noticed and both forms are validated but the dummy fails as it has no data. There is mechanism to restrict a form to a specific function.
Incidentally, validation without annotations can be done with validation functions named like submitValidate.
Using @ConditionalVisitorFieldValidator seems obvious but I can't work out any condition based on the function to be called if validation succeeds. It seems, if the action uses two or more forms, non-annotation-based validation is possible but annotation-based validation is not. Ami I missing something?