Java 在action类中执行CRUD操作以及Struts2中的prepare()方法
假设下面的动作类Java 在action类中执行CRUD操作以及Struts2中的prepare()方法,java,validation,struts2,crud,struts2-interceptors,Java,Validation,Struts2,Crud,Struts2 Interceptors,假设下面的动作类 @Namespace("/admin_side") @ResultPath("/WEB-INF/content") @ParentPackage(value="struts-default") public final class TestAction extends ActionSupport implements Serializable, ValidationAware, Preparable { private Long currentPage=1L;
@Namespace("/admin_side")
@ResultPath("/WEB-INF/content")
@ParentPackage(value="struts-default")
public final class TestAction extends ActionSupport implements Serializable, ValidationAware, Preparable
{
private Long currentPage=1L;
//This method is called on page load.
//Validation is skipped, location is set to a valid action, not "redirectAction", the request is dispatched.
//It is mapped to an action of <s:form>.
public String load() throws Exception
{
//Nothing to see here. Leave it empty.
return ActionSupport.SUCCESS;
}
//Assuming necessary validators and action whose result type is set to "redirectAction".
//It is mapped to an action of <s:submit>.
public String insert()
{
//Do something to either add or update data in a model based on a conditional check.
return ActionSupport.SUCCESS;
}
//Assuming necessary validators and action whose loction is set to a valid action. not "redirectAction".
//The request is dispatched/forwarded.
//It is mapped to an action of <s:a>.
public String edit()
{
//Nothing to see here. Leave it empty.
return ActionSupport.SUCCESS;
}
//Assuming necessary validators and action whose result type is set to "redirectAction".
//It is mapped to an action of <s:submit>.
public String delete()
{
//Do something to delete data from a model.
return ActionSupport.SUCCESS;
}
@Override
public void prepare() throws Exception
{
list= service.getList((int)(currentPage-1)*pageSize, pageSize);
}
}
此方法中可能有更多的条件检查和复杂的代码。
这样做仍然不够。如果由于该条件而发生任何验证/转换错误,则不会初始化列表。任何错误发生后,hasErrors()
、hasActionErrors()
和hasFieldErrors()
都不会在prepare()
方法中计算为true。这需要在validate()
方法中加载列表,如下所示
@Override
public void prepare() throws Exception
{
String actionName = ActionContext.getContext().getName();
if(actionName.equals("load")||actionName.equals("edit"))
{
list= service.getList((int)(currentPage-1)*pageSize, pageSize);
}
}
@Override
public void validate()
{
if(hasErrors())
{
list= service.getList((int)(currentPage-1)*pageSize, pageSize);
}
}
这现在满足了要求,但是有这样的条件检查看起来非常丑陋,不能被认为是一个好方法
有没有更好的方法来保证在请求执行诸如insert、update、delete等操作后,从数据库检索列表只发生一次
它应该独立于每个请求在幕后执行多少操作。尽管存在一些转换/验证错误,但仅应在请求完成之前检索一次列表
所有的@Before
,@beforefresult
,@After
注释似乎都无法避免这种情况
在用于检索/初始化列表的
validate()
方法中使用此类代码似乎不是一种好的做法
我希望有一种方法可以在执行CRUD操作后获取此列表。由于从数据库获取此列表的成本很高,因此在每个操作(插入、编辑、更新、删除)完成后,此列表只应初始化一次。使用为每个操作执行的prepare()
方法,填充列表等繁重操作不是一个好方法。因为并非所有操作都需要执行此类操作
但与验证一起使用时,根本不执行任何操作。如果出现验证错误,则返回输入
结果。此结果是用例中的dispatcher
结果,需要在执行此结果之前填充列表
您正在验证
方法中手动检查验证错误,然后在验证
方法结束并返回输入
结果之前,重新填充列表
此逻辑已由作为defaultStack
成员的实现。您可以将此拦截器配置为在发生验证错误时调用方法。在此方法中,您可以重新填充列表
public String input() throws Exception {
list = service.getList((int)(currentPage-1)*pageSize, pageSize);
return INPUT;
}
现在,您应该通过将注释放置到crud方法insert()
,delete()
,将此方法配置为与workflow
拦截器一起使用
在
edit()
和load()
方法中,如果操作返回调度程序结果,您可以手动调用input()
,可能与input
结果位于同一位置。我不确定是否理解。如果执行重定向,您将获得一个新操作,并且需要新数据。如果存在验证错误,则执行input()
方法。您可以在prepare()
中进行硬代码检查,也可以创建prepareXxx()
方法,其中xxx
将替换为需要列表的方法的名称。除此之外,考虑缓存。<代码>准备< /代码>每一个动作都被调用,<代码>验证< /COD>被称为每个验证,都在做不同的工作。似乎您坚持使用提供的方法。创建自己的方法或类,并在需要时调用这些方法或类。按预期调用input()
方法。我可以避免使用prepare()
方法中初始化列表的代码吗?在大多数情况下,方法input()
和prepare()
都将具有相同的重复代码段来初始化列表。在我的例子中,我目前正在做的是,当发生验证/转换错误时,列表将在input()
方法中初始化,成功后,列表将填充在prepare()
方法中(经过一些难看的条件检查)。我可以用一个常用的方法来初始化这个列表吗(它们都有相同的重复代码)?删除prepare
method。初始化列表的代码在input
方法中。仔细阅读后,这似乎确实是一个正确的方法!我今天也有类似的事情。我所做的是更改我的拦截器堆栈,并将prepare拦截器放在我需要的地方。
@InputConfig(methodName="input")