我是靠谱客的博主 飘逸指甲油,这篇文章主要介绍8 动态调用Action与Action中方法(一),现在分享给大家,希望可以做个参考。

Struts1 提供了DispatchAction 实现一个Action中名含多个业务逻辑

 

现在我们使用Struts2 也可实现现样的效果, 是使用通配符来实现的.

 

struts.xml 文件中,常用

       <action /> 的name , class, method 三个属性都支持通配符

       <resutl/> 也支持通配符

 

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"> <struts> <package name="struts2" extends="struts-default"> <action name="*Action_*" class="ssh.org.web.{1}Action" method="{2}"> <result name="success">/user/success.jsp</result> <result name="input">/user/user.jsp</result> </action> </package> </struts>

 

 针对上面的配置, 假如我拉的URL 是:http://localhost/struts/UserAction_addUser .action

 

UserAction_addUser 与*Action_* 匹配  , 第一个* 是User, 第二个*是addUser,

 

{1} 的值为 User,  {2}的值为addUser , 区分大小写.

 

class="ssh.org.web.{1}Action"  赋值后是,由User 替代{1}

 

method="{2}" 赋值后是, 由addUser 替代{2}

 

从而实现动态的找到Action 与找个Action中的方法.

 

----------------------------------------------------------------------

 

其中一个Action代码: 类名是UaerAction,   其中有addUser()方法

 

与URL http://localhost/struts/UserAction_addUser .action  相匹配, 模式为*Action_*

 

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package ssh.org.web; import java.util.Date; import java.util.List; import com.opensymphony.xwork2.ActionSupport; public class UserAction extends ActionSupport { private static final long serialVersionUID = 1L; private String username; private int age; private Date birthday; private List<String> aihao; public String addUser() throws Exception { //添加用户 return "success"; } public void validateAddUser() { if (null == username || "".equals(username)) { addFieldError("username", "用户名不能为空"); } System.out.println("validateAddUser() 验证方法 "); } public String getUserList() { //取得所有用户 return "success"; } public void validateGetUserList() { //验证方法略... System.out.println("validateAddUser() 验证方法 "); } public String getUsername() { return username; } public void setUsername(final String username) { this.username = username; } public int getAge() { return age; } public void setAge(final int age) { this.age = age; } public Date getBirthday() { return birthday; } public void setBirthday(final Date birthday) { this.birthday = birthday; } public List<String> getAihao() { return aihao; } public void setAihao(final List<String> aihao) { this.aihao = aihao; } }
 

 

------------------------------------------------------------------

 

<resutl/> 也支持通配符,

 

struts.xml文件 一例子:

 

复制代码
1
2
3
<action name="page_*" class="ssh.org.web.HrefAction" > <result name="success">{1}.jsp</result> </action>
 

 

 

-----------------------------------------------------------------------

 

最后在说一说优先级的问题

 

通配符 匹配方式 , 当我们写多个<action name="*"  .../>   时, 意思就是 你的URL 可能与多个  <action name="*"  .../>  匹配上,  先执行那一个?

 

答: 按 书写的先后顺序, 先找到哪个, 就执行哪个.

 

 

 

 

 

 

 

 

 

 

 

最后

以上就是飘逸指甲油最近收集整理的关于8 动态调用Action与Action中方法(一)的全部内容,更多相关8内容请搜索靠谱客的其他文章。

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

评论列表共有 0 条评论

立即
投稿
返回
顶部