`

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

分享到:
评论

相关推荐

    使用Struts 2标签的注意事项

    Struts 2 标签的使用注意事项 Struts 2 是一个基于MVC架构的Java Web应用程序框架,它提供了一系列的标签来简化Web开发。然而,在使用Struts 2标签时,需要注意一些重要的事项。 首先,在使用Struts 2标签之前,...

    struts2标签使用方法

    下面我们将详细探讨Struts2标签的使用方法以及EL(Expression Language)表达式。 1. **Struts2标签库**: - **s:textfield**: 用于创建输入字段,可以设置属性如name、value、size、maxlength等。 - **s:...

    struts2常用标签详解

    在使用Struts2标签时,还需要注意以下几点: - 标签的命名空间:通常以`s:`开头,表明来自Struts2标签库。 - 属性绑定:许多标签都允许通过`value`属性绑定到Action的属性。 - 表单提交:使用`s:form`标签创建表单,...

    Struts2 API 以及标签手册

    4. **ValueStack**: Struts2使用ValueStack来存储模型对象,这些对象可以被视图层访问,以展示数据。ActionContext提供了对当前请求上下文的访问,包括ValueStack、Parameters等。 5. **OGNL(Object-Graph ...

    struts2 action跳转action传参数

    ### Struts2中Action间的参数传递方法 在Struts2框架中,经常需要实现Action之间的跳转,并在跳转过程中传递必要的参数。这种需求在实际开发中非常常见,尤其是在需要根据用户的不同操作来调用不同的业务逻辑时。...

    struts2 表单标签的使用

    此外,Struts2表单标签还可以与其他标签结合使用,如`&lt;s:actionerror&gt;`、`&lt;s:fielderror&gt;`和`&lt;s:property&gt;`,以显示验证错误信息或动态显示数据。`&lt;s:actionerror&gt;`显示全局错误,`&lt;s:fielderror&gt;`显示特定字段的...

    struts2标签api

    在使用Struts2标签API时,需要注意以下几点: - 标签的命名空间:通常以`s:`开头,表示它是Struts2提供的标签。 - 动态属性:很多标签都支持动态属性,允许我们根据Action类的状态动态设置属性值。 - 表单标签的...

    Struts2常用标签及说明

    5. 使用`&lt;s:iterator&gt;`标签遍历集合时,集合数据应从Action中获取。 6. 使用`&lt;s:url&gt;`和`&lt;s:a&gt;`标签生成URL和超链接时,参数通过`s:param`标签添加。 #### 四、总结 通过以上介绍,我们可以看到Struts2提供的标签库...

    Struts2框架及注释和用法

    Struts2允许在Action类和Action方法上使用注解,简化配置。例如,`@Action(value="login")`用于指定Action的名称,`@Results`注解可以定义多个可能的结果。此外,`@FieldAware`和`@Validated`注解用于字段验证。 **...

    Struts2 Action参数详细说明

    在Struts2中,Action的配置和使用方式有多种,下面将详细介绍Struts2 Action参数的详细说明。 首先,我们来看Action的配置格式。在Struts2的配置文件(通常是struts.xml)中,Action的基本配置结构如下: ```xml ...

    struts2标签下的用户注册

    2. **Struts2标签**:Struts2提供了一系列自定义标签来简化视图层的开发。例如,我们可以使用`&lt;s:textfield&gt;`标签来创建输入框,`&lt;s:password&gt;`标签用于密码输入,`&lt;s:submit&gt;`标签则用于提交表单。 ```jsp ...

    struts2地市、县区二级联动下拉菜单 doubleselect标签

    在Struts2中,`doubleselect`标签就是为了解决这类问题而设计的。 `doubleselect`标签是Struts2自定义标签库的一部分,它允许开发者创建两个相关的下拉列表,当用户在第一个下拉框选择一个选项时,第二个下拉框的...

    struts标签库使用大典

    然而,需要注意的是,随着Struts框架的发展,Struts2引入了更多功能强大的标签,而Struts1.x逐渐被淘汰。在新的项目中,开发者可能需要转向Spring MVC或类似的现代框架。 7. **升级与兼容性** 如果你还在使用...

    Struts2中的标签介绍及应用实例

    在实际应用中,应避免直接通过`.jsp` URL访问包含这些标签的页面,而是应该通过Action来驱动页面展示,因为Action可以处理业务逻辑,提供数据给标签使用。 总结来说,Struts2的标签极大地简化了开发过程,特别是...

    Struts2 In Action

    - **核心概念**:标签中的“struts2 action”指出了本书关注的一个核心概念——Action,这是Struts2框架中处理用户请求的关键组件。通过理解和掌握Action的工作原理及其相关配置,开发者可以更好地设计和实现Web应用...

    在struts2中使用freemarker模版

    这样做是为了确保Struts2在服务器启动时能优先加载这个servlet,以便支持FreeMarker模板中使用Struts2的标签。 在`struts.xml`配置文件中,定义一个action,比如`test`,并指定其结果类型为`freemarker`,这样当...

    struts2 标签使用帮助

    Struts2的核心是Action类,而标签库则是其视图层的重要组成部分,提供了丰富的功能来构建动态网页。在Struts2中,标签库极大地提高了开发效率,使得开发者能够更专注于业务逻辑,而不是繁琐的HTML代码。 本文将深入...

    struts2做的分页标签

    本文将详细介绍如何在Struts2中实现分页标签,并提供一个简洁易懂的项目示例。 1. **Struts2分页概念**: 分页是将大量数据分割成若干小块,每次只加载一部分数据到页面上,使得用户可以逐页浏览,减轻服务器压力...

    struts2 optiontransferselect标签使用中的问题

    总的来说,Struts2的OptionTransferSelect标签提供了在Web应用中创建复杂用户界面的能力,但使用时需要注意正确的配置和数据绑定,以确保其正常工作。通过理解和熟悉这些知识点,开发者可以更有效地利用这个标签来...

    留言板留言板struts2留言板struts2

    5. **OGNL(Object-Graph Navigation Language)**:Struts2使用OGNL作为默认表达式语言,用于在Action和视图之间传递数据。通过OGNL,可以在JSP页面中直接访问Action中的属性,或者在Action中动态设置模型数据。 6. ...

Global site tag (gtag.js) - Google Analytics