关于Struts2中在多个action中传递fieldError的问题
????return? SUCCESS;
? }
后来我仔细分析了一下,后台的list之所以没有数据时因为,程序没有执行到execute方法,因为在validate方法那就已经没通过,所以直接跳转到input页面了。
?
再仔细查找了相关资料 发现apache的文档http://struts.apache.org/2.x/docs/how-do-we-repopulate-controls-when-validation-fails.html中写道
When validation fails, we typically forward back to the same serverpage, so that the errors can be presented, and so that the clientcan fix the problem and resubmit the form. Of course, aside fromthe errors, we may also need to present rich controls, like dropdown lists.
If we try to populate rich controls in an Action method, like inputor execute , and validation fails, the method will not be invoked,and the controls are not populated. Two alternative ways topopulate controls are the Preparable interface and the actiontag.
大致的意思是但出现validation错误的时候会影响到Action的正常执行,这个时候应该实现Preparable接口中的prepare()方法,这个方法中的操作不会因为validation错误而不执行。
联想到上面的错,会不会也是因为addActionError而导致不能正常使用action标签了。为此在input的Action中实现Preparable接口并在prepare()方法中putlistTemp。修改代码如下
public??class? Sxt_DaoHang_XianShiAction?extends?ActionSupport??implements? Preparable{
? @Override
?? public?String execute()? throws?Exception {
????return? SUCCESS;
? }
??public?? void?prepare()? throws? Exception{
???ActionContext ctx? =?ActionContext.getContext();
??? ctx.put( "listTemp " , new? ArrayList());
? }
}
执行---成功
原来prepare是在validate拦截器之前执行的。因此可以把一些初始化的工作放到prepare方法中去执行
?
http://blog.sina.com.cn/s/blog_4b6f8d150100i9wm.html