我是靠谱客的博主 复杂丝袜,这篇文章主要介绍Struts2中的3种传值方式,现在分享给大家,希望可以做个参考。

1、直接通过action中行设置相应的get和set方法

复制代码
1
2
3
this.setName("张三"); this.setPassword("123");

2、通过ActionContext完成值得传递

复制代码
1
2
3
ActionContext.getContext().put("aa", 12345); ActionContext.getContext().put("bb", "abc");

3、可以通过Servlet的API传值

复制代码
1
2
ServletActionContext.getRequest().setAttribute("dd", "Hello,World.");

例程如下:

复制代码
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
package net.qbzhong.action; import org.apache.struts2.ServletActionContext; import com.opensymphony.xwork2.ActionContext; public class HelloAction { private String name; private String password; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String execute(){ //传值的第一种方式,直接通过在action中设置相应的get方法和set方法 this.setName("张三"); this.setPassword("123"); //第二种方式通过ActionContext完成值得传递 ActionContext.getContext().put("aa", 12345); ActionContext.getContext().put("bb", "abc"); ActionContext.getContext().put("cc", this.getName()); //第三种传值方式,可以通过Servlet的API传值 ServletActionContext.getRequest().setAttribute("dd", "Hello,World."); return "success"; } }

最后

以上就是复杂丝袜最近收集整理的关于Struts2中的3种传值方式的全部内容,更多相关Struts2中内容请搜索靠谱客的其他文章。

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

评论列表共有 0 条评论

立即
投稿
返回
顶部