struts2 使用action标签应注意的问题

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

Java代码 <embed type="application/x-shockwave-flash" width="14" height="15" src="http://simen.iteye.com/javascripts/syntaxhighlighter/clipboard_new.swf" flashvars="clipboard=%20%20%20%40Override%0A%20%20%20public%20%20String%20execute()%20%20throws%20%20Exception%20%7B%0A%20%20%20%20%20this%20.addActionError(%20%22%20%E7%B3%BB%E7%BB%9F%E9%94%99%E8%AF%AF%20%22%20)%3B%0A%20%20%20%20%20return%20%20SUCCESS%3B%0A%20%20%7D" quality="high" allowscriptaccess="always" pluginspage="http://www.macromedia.com/go/getflashplayer"></embed>
  1.   @Override   
  2.  public   String execute()   throws   Exception {  
  3.    this  .addActionError(  " 系统错误 "  );  
  4.    return   SUCCESS;  
  5. }  
   @Override
   public  String execute()  throws  Exception {
     this .addActionError( " 系统错误 " );
     return  SUCCESS;
  }

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

Java代码 <embed type="application/x-shockwave-flash" width="14" height="15" src="http://simen.iteye.com/javascripts/syntaxhighlighter/clipboard_new.swf" flashvars="clipboard=%3C%40s.action%20name%3D%22input%22%20namespace%3D%22%2Fadmin%22%20executeResult%3Dtrue%20ignoreContextParams%3Dtrue%20%2F%3E" quality="high" allowscriptaccess="always" pluginspage="http://www.macromedia.com/go/getflashplayer"></embed>
  1. < @s .action name= "input"  namespace= "/admin"  executeResult= true  ignoreContextParams= true  />  
<@s.action name="input" namespace="/admin" executeResult=true ignoreContextParams=true />
 

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

Java代码 <embed type="application/x-shockwave-flash" width="14" height="15" src="http://simen.iteye.com/javascripts/syntaxhighlighter/clipboard_new.swf" flashvars="clipboard=%20%20%20%40Override%0A%20%20%20public%20%20String%20execute()%20%20throws%20%20Exception%20%7B%0A%20%20%20%20ActionContext%20ctx%20%20%3D%20%20ActionContext.getContext()%3B%0A%20%20%20%20ctx.put(%20%22listTemp%22%20%2C%20%20new%20%20ArrayList())%3B%0A%20%20%20%20%20return%20%20SUCCESS%3B%0A%20%20%7D%20" quality="high" allowscriptaccess="always" pluginspage="http://www.macromedia.com/go/getflashplayer"></embed>
  1. @Override   
  2. public   String execute()   throws   Exception {  
  3.  ActionContext ctx  =  ActionContext.getContext();  
  4.  ctx.put( "listTemp"  ,   new   ArrayList());  
  5.   return   SUCCESS;  
  6.    
   @Override
   public  String execute()  throws  Exception {
    ActionContext ctx  =  ActionContext.getContext();
    ctx.put( "listTemp" ,  new  ArrayList());
     return  SUCCESS;
  } 

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

Java代码 <embed type="application/x-shockwave-flash" width="14" height="15" src="http://simen.iteye.com/javascripts/syntaxhighlighter/clipboard_new.swf" flashvars="clipboard=%3Cul%3E%0A%20%20%3C%23list%20listTemp%20as%20nav%3E%0A%20%20%3Cli%3E%0A%20%20%20%20%24%7Bnav%7D%0A%20%20%3C%2Fli%3E%0A%20%20%3C%2F%23list%3E%0A%3C%2Ful%3E" quality="high" allowscriptaccess="always" pluginspage="http://www.macromedia.com/go/getflashplayer"></embed>
  1. <ul>  
  2.   <#list listTemp as nav>  
  3.   <li>  
  4.     ${nav}  
  5.   </li>  
  6.   </#list>  
  7. </ul>  
<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。修改代码如下

Java代码 <embed type="application/x-shockwave-flash" width="14" height="15" src="http://simen.iteye.com/javascripts/syntaxhighlighter/clipboard_new.swf" flashvars="clipboard=%40Component(%22inputAction%22)%0A%40Scope(%20%22prototype%22%20)%0Apublic%20%20%20class%20%20Sxt_DaoHang_XianShiAction%20%20extends%20%20ActionSupport%20%20%20implements%20%20Preparable%7B%0A%0A%20%20%40Override%0A%20%20%20public%20%20String%20execute()%20%20throws%20%20Exception%20%7B%0A%20%20%20%20%20return%20%20SUCCESS%3B%0A%20%20%7D%0A%0A%20%20%20public%20%20%20void%20%20prepare()%20%20throws%20%20Exception%20%7B%0A%20%20%20%20ActionContext%20ctx%20%20%3D%20%20ActionContext.getContext()%3B%0A%20%20%20%20ctx.put(%20%22%20listTemp%20%22%20%2C%20new%20%20ArrayList())%3B%0A%20%20%7D%0A%7D" quality="high" allowscriptaccess="always" pluginspage="http://www.macromedia.com/go/getflashplayer"></embed>
  1. @Component ( "inputAction" )  
  2. @Scope "prototype"  )  
  3. public     class   Sxt_DaoHang_XianShiAction   extends   ActionSupport    implements   Preparable{  
  4.   
  5.   @Override   
  6.    public   String execute()   throws   Exception {  
  7.      return   SUCCESS;  
  8.   }  
  9.   
  10.    public     void   prepare()   throws   Exception {  
  11.     ActionContext ctx  =  ActionContext.getContext();  
  12.     ctx.put( " listTemp "  ,  new   ArrayList());  
  13.   }  
  14. }  
@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标签的时候是这样写的

Java代码 <embed type="application/x-shockwave-flash" width="14" height="15" src="http://simen.iteye.com/javascripts/syntaxhighlighter/clipboard_new.swf" flashvars="clipboard=%3C%40s.action%20name%3D%22login%22%20namespace%3D%22%2Fadmin%22%20executeResult%3Dtrue%20ignoreContextParams%3Dtrue%20%2F%3E" quality="high" allowscriptaccess="always" pluginspage="http://www.macromedia.com/go/getflashplayer"></embed>
  1. < @s .action name= "login"  namespace= "/admin"  executeResult= true  ignoreContextParams= true  />  
<@s.action name="login" namespace="/admin" executeResult=true ignoreContextParams=true />

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

 

转载:http://simen.iteye.com/blog/219744

你可能感兴趣的:(apache,freemarker,prototype,Flash,Go)