定义 Action
Struts2 中的 Action 可以是一个简单类 (POJO),里面包含 public String execute() 方法即可。
1
2
31: package com.cdp.struts2;
1
2
32:
1
2
33: public class TestAction{
1
2
34:
1
2
3
45: private final String SUCCESS = "success";
1
2
36:
1
2
3
47: public String execute() throws Exception {
1
2
3
48: return SUCCESS;
1
2
3
49: }
1
2
310: }
或者去实现 com.opensymphony.xwork2.Action 接口。
1
2
31: package com.cdp.struts2;
1
2
32:
1
2
33: import com.opensymphony.xwork2.Action;
1
2
34:
1
2
35: public class TestAction implements Action{
1
2
36:
1
2
3
47: private final String SUCCESS = "success";
1
2
38:
1
2
3
49: public String execute() throws Exception {
1
2
3
410: return SUCCESS;
1
2
3
411: }
1
2
312: }
或者直接继承 com.opensymphony.xwork2.ActionSupport 类。
1
2
31: package com.cdp.struts2;
1
2
32:
1
2
33: import com.opensymphony.xwork2.ActionSupport;
1
2
34:
1
2
35: public class TestAction extends ActionSupport{
1
2
36:
1
2
3
47: private final String SUCCESS = "success";
1
2
38:
1
2
3
49: public String execute() throws Exception {
1
2
3
410: return SUCCESS;
1
2
3
411: }
1
2
312: }
用的最多的是最后一种,直接继承 ActionSupport 类的 Action ,因为 ActionSupport 类为我们定义了许多我们以后可以直接调用的方便的方法。
定义完 Action 之后,需要在 struts.xml 中配置相应的 Action 和 对应的 result :
1
2
31: <action name="helloaction" class="com.cdp.struts2.TestAction">
1
2
3
42: <result name="success">
1
2
3
43: /Hello.jsp
1
2
3
4
54: result>
1
2
3
45: action>
我们还可以在 struts.xml 中指定 action 的类和类中的方法:
1
2
3
4
5
6
71: "helloaction" class= "com.cdp.struts2.TestAction" method= "add">
1
2
3
42: "add">
1
2
3
43: /Hello.jsp
1
2
34:
1
2
35:
还可以在调用 action 的 URL 上面指定调用的方法来取代在 struts.xml 中配置方法,格式为:
1
2
31: http://localhost/webapp/namespace/in!add
感叹号前面对应的是 action ,后面对应的是方法名称,实现方法的动态调用 ( DMI, Dynamic Method Invocation )。
Action 中的通配符 (wildcard)。
如果我们每次写一个 action 都得去配置就会显得十分麻烦,此时,Struts2 的通配符就应运而生了,通配符就是用 * 来代替任意文字,用{i} 代替匹配第 i 个 * 的文字。看下面的配置文件:
1
2
31: <package name="default" namespace="/name" extends="struts-default">
1
2
3
42: <action name="*_*" class="com.cdp.struts2.{1}Action" method="{2}">
1
2
3
43: <result>
1
2
3
44: /{1}_{2}_success.jsp
1
2
3
4
55: result>
1
2
3
4
56: action>
1
2
3
47: 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
31: package com.cdp.struts2.cls;
1
2
32:
1
2
33: public class User {
1
2
3
44: private String name;
1
2
3
45: private int age;
1
2
36:
1
2
3
47: public String getName() {
1
2
3
48: return name;
1
2
3
49: }
1
2
310:
1
2
3
411: public void setName(String name) {
1
2
3
412: this.name = name;
1
2
3
413: }
1
2
314:
1
2
3
415: public int getAge() {
1
2
3
416: return age;
1
2
3
417: }
1
2
318:
1
2
3
419: public void setAge(int age) {
1
2
3
420: this.age = age;
1
2
3
421: }
1
2
322:
1
2
323: }
Action :
1
2
31: package com.cdp.struts2;
1
2
32:
1
2
33: import com.cdp.struts2.cls.User;
1
2
34: import com.opensymphony.xwork2.ActionSupport;
1
2
35:
1
2
36: public class TestAction extends ActionSupport {
1
2
37:
1
2
3
48: private final String SUCCESS = "success";
1
2
3
49: private User user;
1
2
310:
1
2
3
411: public String add() {
1
2
3
412: System.out.println("add name=" + user.getName());
1
2
3
413: System.out.println("add age=" + user.getAge());
1
2
3
414: return SUCCESS;
1
2
3
415: }
1
2
316:
1
2
3
417: public String remove() {
1
2
3
418: System.out.println("remove name=" + user.getName());
1
2
3
419: System.out.println("remove age=" + user.getAge());
1
2
3
420: return SUCCESS;
1
2
3
421: }
1
2
322:
1
2
3
423: public User getUser(){
1
2
3
424: return user;
1
2
3
425: }
1
2
326:
1
2
3
427: public void setUser(User user){
1
2
3
428: this.user = user;
1
2
3
429: }
1
2
330: }
当访问 URL 为 localhost/mywebapp/name/Test_add?user.name=CDP&user.age=22 即可在 action 中得到参数。
如果我们的参数为中文的时候,会发现乱码问题出现了,头疼 ING...
修改 struts.xml 与 web.xml 即可解决乱码问题,或者写个转码类也可以:
1
2
3
41: <constant name="struts.i18n.encoding" value="GBK"/> --struts.xml-->
1
2
3
4
51: <filter-class>org.apache.struts2.dispatcher.FilterDispatcher filter-class> --web.xml-->
在 struts.xml 中定义默认 action
定义默认 action 是在输错 action 的时候可以有 action 可以匹配。
1
2
3
41: <default-action-ref name="a_action"> default-action-ref>
最后
以上就是眼睛大电话最近收集整理的关于Struts2 的 Action的全部内容,更多相关Struts2内容请搜索靠谱客的其他文章。
发表评论 取消回复