Struts2使用action标签的注意有关问题

Struts2使用action标签的注意问题

为index的Action中重写excute方法如下:

   @Override
   public  String execute()  throws  Exception {
     this .addActionError( " 系统错误 " );
     return  SUCCESS;
  }

对应的SUCCESS是一个ftl模板,使用了Struts2的action标签:

<@s.action name="input" namespace="/admin" executeResult=true ignoreContextParams=true />
 

名为input的Action中重写excute方法如下:

   @Override
   public  String execute()  throws  Exception {
    ActionContext ctx  =  ActionContext.getContext();
    ctx.put( "listTemp" ,  new  ArrayList());
     return  SUCCESS;
  } 

对应的SUCCESS也是一个ftl模板:

<ul>
  <#list listTemp as nav>
  <li>
    ${nav}
  </li>
  </#list>
</ul>

 

这个时候执行代码 freemarker会提示“<pre>Expression listDaoHangLieBiao is undefined</pre>”为什么会这样了?
仔细查找了相关资料 发现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 server page, so that the errors can be presented, and so that the client can fix the problem and resubmit the form. Of course, aside from the errors, we may also need to present rich controls, like drop down lists.
If we try to populate rich controls in an Action method, like input or execute , and validation fails, the method will not be invoked, and the controls are not populated. Two alternative ways to populate controls are the Preparable interface and the action tag.
大致的意思是但出现validation错误的时候会影响到Action的正常执行,这个时候应该实现Preparable 接口中的prepare()方法,这个方法中的操作不会因为validation错误而不执行。
联想到上面的错,会不会也是因为addActionError而导致不能正常使用action标签了。为此在input的Action中实现Preparable接口并在prepare()方法中put listTemp。修改代码如下

@Component("inputAction")
@Scope( "prototype" )
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());
  }
}

 执行---成功

总结 Struts2目前的资料相对Struts1来说是非常少的,尤其是研究的很深的资料,看来现在想学好Struts2还必须从Apache的原始资料中寻找。

另外上面使用action标签的时候是这样写的

<@s.action name="login" namespace="/admin" executeResult=true ignoreContextParams=true />

注意和Struts2的标签写法略有不同,因为这里使用了Freemarker做模板,所以使用的freemarker的写法,特别的是executeResult=true ignoreContextParams=true而按照Struts2的标签应该是executeResult="true" ignoreContextParams="true"

1 楼 pekkle 2009-06-17  
我在freemarker中用
<@s.action name="zwindex" executeResult="true" ignoreContextParams="true"/>
一直报java.lang.IllegalArgumentException: argument type mismatch
The problematic instruction:
----------
==> user-directive s.action [on line 22, column 17 in template/admin/area/add.ftl]
----------

Java backtrace for programmers:

但是直接jsp中用<s:action name="zwindex" executeResult="true" ignoreContextParams="true"/>
是正常显示的。

楼主后来这个在freemarker中是否已经解决了?
我这个问题是什么原因引起的,还是freemarker不支持这个action标签
2 楼 KimShen 2009-06-17  
<#assign s=JspTaglibs["/WEB-INF/struts-tags.tld"]>
<@s.action name="test" executeResult=true/>

出现
java.lang.IllegalStateException:
Cannot create a session after the response has been committed

为什么呢?难道FreeMarker应为是静态的所以关闭了流?
LZ是怎么试验成功的?
3 楼 pekkle 2009-06-19  
楼上的,我的解决了,
可以在jsp中引入freemarker,用到s:action标签,
貌似只能在jsp中用,曲线救国吧
4 楼 KimShen 2009-06-19  
我也实验过了 freemarker又没问题了.
[又] 很摸名奇妙的又好了
5 楼 treblesoftware 2009-06-20  
呵呵,在STRUTS2中还没有使用过freemarker。