Struts2 的 Action

    技术2026-05-15  17

    定义 Action

    Struts2 中的 Action 可以是一个简单类 (POJO),里面包含 public String execute() 方法即可。

    1: package com.cdp.struts2; 2:   3: public class TestAction{ 4: 5: private final String SUCCESS = "success"; 6: 7: public String execute() throws Exception { 8: return SUCCESS; 9: } 10: } .csharpcode, .csharpcode pre { font-size: small; color: black; font-family: consolas, "Courier New", courier, monospace; background-color: #ffffff; /*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt { background-color: #f4f4f4; width: 100%; margin: 0em; } .csharpcode .lnum { color: #606060; }

     

    或者去实现 com.opensymphony.xwork2.Action 接口。

    1: package com.cdp.struts2; 2:   3: import com.opensymphony.xwork2.Action; 4:   5: public class TestAction implements Action{ 6: 7: private final String SUCCESS = "success"; 8: 9: public String execute() throws Exception { 10: return SUCCESS; 11: } 12: } .csharpcode, .csharpcode pre { font-size: small; color: black; font-family: consolas, "Courier New", courier, monospace; background-color: #ffffff; /*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt { background-color: #f4f4f4; width: 100%; margin: 0em; } .csharpcode .lnum { color: #606060; }

    或者直接继承 com.opensymphony.xwork2.ActionSupport 类。

    1: package com.cdp.struts2; 2:   3: import com.opensymphony.xwork2.ActionSupport; 4:   5: public class TestAction extends ActionSupport{ 6: 7: private final String SUCCESS = "success"; 8: 9: public String execute() throws Exception { 10: return SUCCESS; 11: } 12: } .csharpcode, .csharpcode pre { font-size: small; color: black; font-family: consolas, "Courier New", courier, monospace; background-color: #ffffff; /*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt { background-color: #f4f4f4; width: 100%; margin: 0em; } .csharpcode .lnum { color: #606060; }

    用的最多的是最后一种,直接继承 ActionSupport 类的 Action ,因为 ActionSupport 类为我们定义了许多我们以后可以直接调用的方便的方法。

    定义完 Action 之后,需要在 struts.xml 中配置相应的 Action 和 对应的 result :

    1: <action name="helloaction" class="com.cdp.struts2.TestAction"> 2: <result name="success"> 3: /Hello.jsp 4: result> 5: action> .csharpcode, .csharpcode pre { font-size: small; color: black; font-family: consolas, "Courier New", courier, monospace; background-color: #ffffff; /*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt { background-color: #f4f4f4; width: 100%; margin: 0em; } .csharpcode .lnum { color: #606060; }

    我们还可以在 struts.xml 中指定 action 的类和类中的方法:

    1: "helloaction" class= "com.cdp.struts2.TestAction" method= "add"> 2: "add"> 3: /Hello.jsp 4: 5: .csharpcode, .csharpcode pre { font-size: small; color: black; font-family: consolas, "Courier New", courier, monospace; background-color: #ffffff; /*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt { background-color: #f4f4f4; width: 100%; margin: 0em; } .csharpcode .lnum { color: #606060; }

    还可以在调用 action 的 URL 上面指定调用的方法来取代在 struts.xml 中配置方法,格式为:

    1: http://localhost/webapp/namespace/in!add  

    感叹号前面对应的是 action ,后面对应的是方法名称,实现方法的动态调用 ( DMI, Dynamic Method Invocation )。

     

    Action 中的通配符 (wildcard)。

    如果我们每次写一个 action 都得去配置就会显得十分麻烦,此时,Struts2 的通配符就应运而生了,通配符就是用 * 来代替任意文字,用{i} 代替匹配第 i  个 * 的文字。看下面的配置文件:

    1: <package name="default" namespace="/name" extends="struts-default"> 2: <action name="*_*" class="com.cdp.struts2.{1}Action" method="{2}"> 3: <result> 4: /{1}_{2}_success.jsp 5: result> 6: action> 7: package>

    .csharpcode, .csharpcode pre { font-size: small; color: black; font-family: consolas, "Courier New", courier, monospace; background-color: #ffffff; /*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt { background-color: #f4f4f4; width: 100%; margin: 0em; } .csharpcode .lnum { color: #606060; }

    当我们在地址栏访问 localhost/mywebapp/namespace/Money_in ,Tomcat 就会认定 Money 匹配 {1},in 匹配 {2} ,这时就会调用 com.cdp.struts2.MoneyAction  里面的 in 方法,定位到 Money_in_success.jsp 文件。

    所以使用通配符,只要我们遵循特定的命名约定,就会使 struts.xml 的配置工作降到最低。

    在 Action 中使用参数

    在相应的 action 类中定义与参数名相同的私有变量,并为其设定相应的 Getters 和 Setters 方法,之后就可以在 action 中使用参数了。

    我们还可以将需要的参数封装在一个特定的类中 ( domain model ),然后再 action 类中定义此类的私有对象,设定 Getters 和 Setters ,如下:

    参数类:

    1: package com.cdp.struts2.cls; 2:   3: public class User { 4: private String name; 5: private int age; 6:   7: public String getName() { 8: return name; 9: } 10:   11: public void setName(String name) { 12: this.name = name; 13: } 14:   15: public int getAge() { 16: return age; 17: } 18:   19: public void setAge(int age) { 20: this.age = age; 21: } 22:   23: } .csharpcode, .csharpcode pre { font-size: small; color: black; font-family: consolas, "Courier New", courier, monospace; background-color: #ffffff; /*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt { background-color: #f4f4f4; width: 100%; margin: 0em; } .csharpcode .lnum { color: #606060; }

    Action :

    1: package com.cdp.struts2; 2:   3: import com.cdp.struts2.cls.User; 4: import com.opensymphony.xwork2.ActionSupport; 5:   6: public class TestAction extends ActionSupport { 7:   8: private final String SUCCESS = "success"; 9: private User user; 10:   11: public String add() { 12: System.out.println("add name=" + user.getName()); 13: System.out.println("add age=" + user.getAge()); 14: return SUCCESS; 15: } 16:   17: public String remove() { 18: System.out.println("remove name=" + user.getName()); 19: System.out.println("remove age=" + user.getAge()); 20: return SUCCESS; 21: } 22: 23: public User getUser(){ 24: return user; 25: } 26: 27: public void setUser(User user){ 28: this.user = user; 29: } 30: } .csharpcode, .csharpcode pre { font-size: small; color: black; font-family: consolas, "Courier New", courier, monospace; background-color: #ffffff; /*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt { background-color: #f4f4f4; width: 100%; margin: 0em; } .csharpcode .lnum { color: #606060; }

    当访问 URL 为 localhost/mywebapp/name/Test_add?user.name=CDP&user.age=22 即可在 action 中得到参数。

    如果我们的参数为中文的时候,会发现乱码问题出现了,头疼 ING...

    修改 struts.xml 与 web.xml 即可解决乱码问题,或者写个转码类也可以:

    1: <constant name="struts.i18n.encoding" value="GBK"/> --struts.xml--> 1: <filter-class>org.apache.struts2.dispatcher.FilterDispatcher filter-class> --web.xml--> .csharpcode, .csharpcode pre { font-size: small; color: black; font-family: consolas, "Courier New", courier, monospace; background-color: #ffffff; /*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt { background-color: #f4f4f4; width: 100%; margin: 0em; } .csharpcode .lnum { color: #606060; }

     

    在 struts.xml 中定义默认 action

    定义默认 action 是在输错 action 的时候可以有 action 可以匹配。

    1: <default-action-ref name="a_action"> default-action-ref> .csharpcode, .csharpcode pre { font-size: small; color: black; font-family: consolas, "Courier New", courier, monospace; background-color: #ffffff; /*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt { background-color: #f4f4f4; width: 100%; margin: 0em; } .csharpcode .lnum { color: #606060; }
    最新回复(0)