我是靠谱客的博主 眼睛大电话,这篇文章主要介绍Struts2 的 Action,现在分享给大家,希望可以做个参考。

定义 Action

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

复制代码
1
2
3
1: package com.cdp.struts2;
复制代码
1
2
3
2:  
复制代码
1
2
3
3: public class TestAction{
复制代码
1
2
3
4:
复制代码
1
2
3
4
5: private final String SUCCESS = "success";
复制代码
1
2
3
6:
复制代码
1
2
3
4
7: public String execute() throws Exception {
复制代码
1
2
3
4
8: return SUCCESS;
复制代码
1
2
3
4
9: }
复制代码
1
2
3
10: }

 

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

复制代码
1
2
3
1: package com.cdp.struts2;
复制代码
1
2
3
2:  
复制代码
1
2
3
3: import com.opensymphony.xwork2.Action;
复制代码
1
2
3
4:  
复制代码
1
2
3
5: public class TestAction implements Action{
复制代码
1
2
3
6:
复制代码
1
2
3
4
7: private final String SUCCESS = "success";
复制代码
1
2
3
8:
复制代码
1
2
3
4
9: public String execute() throws Exception {
复制代码
1
2
3
4
10: return SUCCESS;
复制代码
1
2
3
4
11: }
复制代码
1
2
3
12: }

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

复制代码
1
2
3
1: package com.cdp.struts2;
复制代码
1
2
3
2:  
复制代码
1
2
3
3: import com.opensymphony.xwork2.ActionSupport;
复制代码
1
2
3
4:  
复制代码
1
2
3
5: public class TestAction extends ActionSupport{
复制代码
1
2
3
6:
复制代码
1
2
3
4
7: private final String SUCCESS = "success";
复制代码
1
2
3
8:
复制代码
1
2
3
4
9: public String execute() throws Exception {
复制代码
1
2
3
4
10: return SUCCESS;
复制代码
1
2
3
4
11: }
复制代码
1
2
3
12: }

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

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

复制代码
1
2
3
1: <action name="helloaction" class="com.cdp.struts2.TestAction">
复制代码
1
2
3
4
2: <result name="success">
复制代码
1
2
3
4
3: /Hello.jsp
复制代码
1
2
3
4
5
4: result>
复制代码
1
2
3
4
5: action>

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

复制代码
1
2
3
4
5
6
7
1: "helloaction" class= "com.cdp.struts2.TestAction" method= "add">
复制代码
1
2
3
4
2: "add">
复制代码
1
2
3
4
3: /Hello.jsp
复制代码
1
2
3
4:
复制代码
1
2
3
5:

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

复制代码
1
2
3
1: http://localhost/webapp/namespace/in!add
 

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

 

Action 中的通配符 (wildcard)。

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

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

当我们在地址栏访问 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
2
3
1: package com.cdp.struts2.cls;
复制代码
1
2
3
2:  
复制代码
1
2
3
3: public class User {
复制代码
1
2
3
4
4: private String name;
复制代码
1
2
3
4
5: private int age;
复制代码
1
2
3
6:  
复制代码
1
2
3
4
7: public String getName() {
复制代码
1
2
3
4
8: return name;
复制代码
1
2
3
4
9: }
复制代码
1
2
3
10:  
复制代码
1
2
3
4
11: public void setName(String name) {
复制代码
1
2
3
4
12: this.name = name;
复制代码
1
2
3
4
13: }
复制代码
1
2
3
14:  
复制代码
1
2
3
4
15: public int getAge() {
复制代码
1
2
3
4
16: return age;
复制代码
1
2
3
4
17: }
复制代码
1
2
3
18:  
复制代码
1
2
3
4
19: public void setAge(int age) {
复制代码
1
2
3
4
20: this.age = age;
复制代码
1
2
3
4
21: }
复制代码
1
2
3
22:  
复制代码
1
2
3
23: }

Action :

复制代码
1
2
3
1: package com.cdp.struts2;
复制代码
1
2
3
2:  
复制代码
1
2
3
3: import com.cdp.struts2.cls.User;
复制代码
1
2
3
4: import com.opensymphony.xwork2.ActionSupport;
复制代码
1
2
3
5:  
复制代码
1
2
3
6: public class TestAction extends ActionSupport {
复制代码
1
2
3
7:  
复制代码
1
2
3
4
8: private final String SUCCESS = "success";
复制代码
1
2
3
4
9: private User user;
复制代码
1
2
3
10:  
复制代码
1
2
3
4
11: public String add() {
复制代码
1
2
3
4
12: System.out.println("add name=" + user.getName());
复制代码
1
2
3
4
13: System.out.println("add age=" + user.getAge());
复制代码
1
2
3
4
14: return SUCCESS;
复制代码
1
2
3
4
15: }
复制代码
1
2
3
16:  
复制代码
1
2
3
4
17: public String remove() {
复制代码
1
2
3
4
18: System.out.println("remove name=" + user.getName());
复制代码
1
2
3
4
19: System.out.println("remove age=" + user.getAge());
复制代码
1
2
3
4
20: return SUCCESS;
复制代码
1
2
3
4
21: }
复制代码
1
2
3
22:
复制代码
1
2
3
4
23: public User getUser(){
复制代码
1
2
3
4
24: return user;
复制代码
1
2
3
4
25: }
复制代码
1
2
3
26:
复制代码
1
2
3
4
27: public void setUser(User user){
复制代码
1
2
3
4
28: this.user = user;
复制代码
1
2
3
4
29: }
复制代码
1
2
3
30: }

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

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

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

复制代码
1
2
3
4
1: <constant name="struts.i18n.encoding" value="GBK"/> --struts.xml-->
复制代码
1
2
3
4
5
1: <filter-class>org.apache.struts2.dispatcher.FilterDispatcher filter-class> --web.xml-->

 

在 struts.xml 中定义默认 action

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

复制代码
1
2
3
4
1: <default-action-ref name="a_action"> default-action-ref>

最后

以上就是眼睛大电话最近收集整理的关于Struts2 的 Action的全部内容,更多相关Struts2内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(79)

评论列表共有 0 条评论

立即
投稿
返回
顶部