Struts 2 @VisitorFieldValidator проверяет все формы, а не только ту, которая вам нужна.JAVA

Программисты JAVA общаются здесь
Anonymous
Struts 2 @VisitorFieldValidator проверяет все формы, а не только ту, которая вам нужна.

Сообщение Anonymous »

Рассмотрите следующее действие Struts 2 с использованием ядра Struts 2 и подключаемого модуля соглашений 6.7.0, который использует стек перехватчиков по умолчанию.

Код: Выделить всё

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;
}

}
Формы простые.

Код: Выделить всё

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 + "]";
}

}

Код: Выделить всё

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 + "]";
}

}
/pages/experiment4/Arithmetic.jsp

Код: Выделить всё




Experiment 4


Experiment 4


Arithmetic

[list]
[*]



[*]



[*]



[*]



[*]
 
Submit

[/list]


Dummy

[list]
[*]



[*]
 
Submit

[/list]




Web.xml является стандартным.

Код: Выделить всё


struts2
org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter


struts2
/*


/index.html
/index.jsp


Struts.xml не используется.
Перед вызовом функции отправки распознаются обе аннотации @VisitorFieldValidator, и обе формы проверяются, но фиктивная форма завершается сбоем, поскольку в ней нет данных. Не существует механизма, позволяющего ограничить форму определенной функцией.
Кстати, проверку без аннотаций можно выполнить с помощью функций проверки с именем submitValidate.
Использование @ConditionalVisitorFieldValidator кажется очевидным, но я не могу разработать какое-либо условие на основе функции, которая будет вызываться в случае успеха проверки. Кажется, что если действие использует две или более формы, проверка без аннотаций возможна, а проверка на основе аннотаций — нет. Я что-то упустил?

Вернуться в «JAVA»