struts.xml 配置详解之四 Action 配置1

1.      Action 配置

1.1 Action 映射

action 元素的属性

属性名称

是否必须

说明

  name

  action 的名字,用于匹配请求URI

  class

  Action 实现类的完整类名

  method

  执行Action 时调用的方法

  converter

  用于action 的类型转换器的完整类名

action 配置示例:

代码清单 1 struts.xml 片段

< package name = "loginTest" extends = "struts-default">

      < action name = "login" class = "com.coderdream.action.LoginAction">

           < result name = "success"> /msg/loginSuc.jsp </ result >

           < result name = "input"> /msg/loginFail.jsp </ result >

      </ action >

</ package >

注意,

1、 action name 属性是必须的,其他属性是可选的。

2、 name 中默认不能有/ (斜线),虽然可以修改常量来打开这个功能,但是强烈建议不要这样做,通过命名空间可以实现类似的功能。

代码清单 2 struts.xml 片段

< constant name = "struts.enable.SlashesInActionNames" value = "true" />

3、 名字中尽量不要使用点号(. )和连字符(- ),否则可能出现一些莫名其妙的问题。

1.2 、使用 method 属性

新建一个TestAction 类,其中包含增删改查方法。

代码清单 3 TestAction 片段

public class TestAction extends ActionSupport {

      public String add() {

           System.out .println("Add 方法被调用 " );

           return SUCCESS ;

      }

 

      public String delete() {

           System.out .println("delete 方法被调用 " );

           return SUCCESS ;

      }

 

      public String update() {

           System.out .println("update 方法被调用 " );

           return SUCCESS ;

      }

 

      public String query() {

           System.out .println("query 方法被调用 " );

           return SUCCESS ;

      }

}

action method 配置:

代码清单 4 struts.xml 片段

< package name = "test" extends = "struts-default" >

      < action name = "add" class = "com.coderdream.action.TestAction"

           method="add" >

           < result name = "success" > /msg/add.jsp </ result >

      </ action >

 

      < action name = "delete" class = "com.coderdream.action.TestAction"

           method="delete" >

           < result name = "success" > /msg/delete.jsp </ result >

      </ action >

 

      < action name = "update" class = "com.coderdream.action.TestAction"

           method="update" >

           < result name = "success" > /msg/update.jsp </ result >

      </ action >

 

      < action name = "query" class = "com.coderdream.action.TestAction"

           method="query" >

           < result name = "success" > /msg/query.jsp </ result >

      </ action >

</ package >

jsp 调用代码:

代码清单 5 main.jsp 片段

< a href = " <%= path %> /add.action" > 增加 </ a >

< br />

< a href = " <%= path %> /delete.action" > 删除 </ a >

< br />

< a href = " <%= path %> /update.action" > 修改 </ a >

< br />

< a href = " <%= path %> /query.action" > 查询 </ a >

< br />

1.3 动态方法调用

动态方法调用(Dynamic Method InvocationDMI )是在action 的名字中使用感叹号(! )来标识要调用的方法名,其语法格式为:

actionName.methodName.action

action 不配置method 属性,4 个方法返回同一个结果。

代码清单 6 struts.xml 片段

< action name = "test" class = "com.coderdream.action.TestAction" >

      < result name = "success" > /msg/operateSuc.jsp </ result >

</ action >

jsp 调用代码:

代码清单 7 main.jsp 片段

< a href = " <%= path %> /test!add.action" > 增加 </ a >

< br />

< a href = " <%= path %> /test!delete.action" > 删除 </ a >

< br />

< a href = " <%= path %> /test!update.action" > 修改 </ a >

< br />

< a href = " <%= path %> /test!query.action" > 查询 </ a >

< br />

运行结果(结果页面相同,后台显示执行的方法不同):


【张冰Struts2学习笔记】0501.struts.xml配置详解之四 Action配置1

 

我的联系方式: 85337464

我的博客: http://coderdream.iteye.com

 

相关文章:

  • 2021-06-15
  • 2021-12-09
  • 2021-12-09
猜你喜欢
  • 2022-12-23
  • 2021-12-09
  • 2021-05-22
  • 2022-12-23
  • 2021-08-25
  • 2022-12-23
相关资源
相似解决方案