У меня есть следующий класс действия:Метод подготовки() вызывается дважды, когда тип результата является перенаправление действия в Struts2
@Namespace("/admin_side")
@ResultPath("/WEB-INF/content")
@ParentPackage(value="struts-default")
public final class FabricAction extends ActionSupport implements Serializable, ValidationAware, Preparable, ModelDriven<Fabric>
{
@Autowired
private final transient FabricService fabricService=null;
private static final long serialVersionUID = 1L;
private int pageSize=5;
private Long id;
private Boolean deleteOneRow;
private Boolean deleteMultipleRows;
private String message;
private List<Long>chk;
private Long deleteId;
private Long begin;
private Long end;
private Long currentPage=1L;
private Long rowCount;
private Long totalPages;
private Integer status;
private Fabric entity=new Fabric();
private List<Fabric>fabrics=new ArrayList<Fabric>();
//Getters & Setters.
@Action(value = "Fabric",
results = {
@Result(name=ActionSupport.SUCCESS, location="Fabric.jsp"),
@Result(name = ActionSupport.INPUT, location = "Fabric.jsp")},
interceptorRefs={
@InterceptorRef(value="paramsPrepareParamsStack", params={"params.acceptParamNames", "id, currentPage, rowCount, totalPages, message, status", "validation.validateAnnotatedMethodOnly", "true", "validation.excludeMethods", "load"})})
public String load() throws Exception
{
//Invokes, when the page is loaded.
return ActionSupport.SUCCESS;
}
@Action(value = "FabricPage",
results = {@Result(name=ActionSupport.SUCCESS, location="Fabric.jsp", params={"namespace", "/admin_side", "actionName", "Fabric", "currentPage", "${currentPage}"}),
@Result(name = ActionSupport.INPUT, location = "Fabric.jsp")},
interceptorRefs={
@InterceptorRef(value="conversionError"),
@InterceptorRef(value="paramsPrepareParamsStack", params={"params.acceptParamNames", "currentPage", "validation.validateAnnotatedMethodOnly", "true"})})
public String page()
{
//Invokes, when a page link is clicked.
return ActionSupport.SUCCESS;
}
@Validations(
requiredStrings={
@RequiredStringValidator(fieldName="fabricName", type= ValidatorType.FIELD, key = "fabric.name.required")},
stringLengthFields={
@StringLengthFieldValidator(fieldName="fabricName", type= ValidatorType.FIELD, minLength="2", maxLength="45", key="fabric.name.length", messageParams={"2", "45"})})
@Action(value = "AddFabric",
results = {
@Result(name=ActionSupport.SUCCESS, type="redirectAction", location="Fabric.jsp", params={"namespace", "/admin_side", "actionName", "Fabric", "currentPage", "${currentPage}", "message", "${message}", "id", "${id}", "status", "${status}"}),
@Result(name = ActionSupport.INPUT, location = "Fabric.jsp")},
interceptorRefs={
@InterceptorRef(value="conversionError"),
@InterceptorRef(value="paramsPrepareParamsStack", params={"params.acceptParamNames", "id, fabricId, fabricName, currentPage, rowCount, totalPages, status", "validation.validateAnnotatedMethodOnly", "true"})
})
public String insert()
{
//Handles insert and update operations.
return ActionSupport.SUCCESS;
}
@Action(value = "EditFabric",
results = {
@Result(name=ActionSupport.SUCCESS, location="Fabric.jsp"),
@Result(name = ActionSupport.INPUT, location = "Fabric.jsp")},
interceptorRefs={
@InterceptorRef(value="paramsPrepareParamsStack", params={"params.acceptParamNames", "id, fabricId, fabricName, currentPage", "validation.validateAnnotatedMethodOnly", "true"}),
@InterceptorRef(value="conversionError")})
public String edit()
{
//Invokes, when an edit link is clicked.
return ActionSupport.SUCCESS;
}
@Validations(
fieldExpressions={@FieldExpressionValidator(fieldName="deleteOneRow", expression="deleteOneRow==true", shortCircuit=true, key="delete.row.reject")})
@Action(value = "DeleteFabric",
results = {
@Result(name=ActionSupport.SUCCESS, type="redirectAction", location="Fabric.action", params={"currentPage", "${currentPage}", "message", "${message}", "status", "${status}"}),
@Result(name = ActionSupport.INPUT, location = "Fabric.jsp")},
interceptorRefs={
@InterceptorRef(value="paramsPrepareParamsStack", params={"params.acceptParamNames", "deleteId, deleteOneRow, currentPage, status", "validation.validateAnnotatedMethodOnly", "true"}),
@InterceptorRef(value="conversionError")})
public String deleteSingleRow()
{
//Handles deletion of a single row.
return ActionSupport.SUCCESS;
}
@Validations(
requiredFields={
@RequiredFieldValidator(type= ValidatorType.FIELD, fieldName="chk", key="delete.multiple.alert"),
@RequiredFieldValidator(type= ValidatorType.FIELD, fieldName="deleteMultipleRows", key="delete.multiple.confirm")})
@Action(value = "DeleteFabrics",
results = {
@Result(name=ActionSupport.SUCCESS, type="redirectAction", location="Fabric.jsp", params={"namespace", "/admin_side", "actionName", "Fabric", "currentPage", "${currentPage}", "message", "${message}", "status", "${status}"}),
@Result(name = ActionSupport.INPUT, location = "Fabric.jsp")},
interceptorRefs={
@InterceptorRef(value="paramsPrepareParamsStack", params={"params.acceptParamNames", "deleteMultipleRows, chk, currentPage, rowCount, totalPages", "validation.validateAnnotatedMethodOnly", "true"}),
@InterceptorRef(value="conversionError")})
public String deleteMultipleRows()
{
//Handles deletion of multiple rows.
return ActionSupport.SUCCESS;
}
public Fabric getEntity() {
return entity;
}
public void setEntity(Fabric entity) {
this.entity = entity;
}
public List<Fabric> getFabrics()
{
return fabrics;
}
@Override
public Fabric getModel()
{
return entity;
}
@Override
public void prepare() throws Exception
{
fabrics= fabricService.getList((int)(currentPage-1)*pageSize, pageSize);
}
}
При выполнении всех операций, за исключением вставки и обновления, связанные с методом insert()
(оба из них связаны с insert()
, который отображается в действии <s:submit>
), метод prepare()
выполняется только один раз.
При выполнении либо вставки, либо обновления, метод prepare()
рассматривается как вызываемый дважды. Почему это происходит?
В случае type="redirectAction"
в @Result()
, метод prepare()
выполнен в два раза. Есть ли способ предотвратить выполнение метода prepare()
дважды, когда type
из @Result
установлено в redirectAction
?
Не можете ли вы напечатать имя действия в 'prepare()'? –
Как отобразить имя действия? – Tiny
Что-то вроде 'System.out.println (" actionName = "+ ActionContext.getContext(). GetName())' –