码迷,mamicode.com
首页 > Windows程序 > 详细

Struts2-学习笔记系列(4)-访问servlet api

时间:2016-06-04 23:25:18      阅读:316      评论:0      收藏:0      [点我收藏+]

标签:

5.1通过actioncontext:

 1 public String execute() throws Exception {
 2 
 3     ActionContext ctx = ActionContext.getContext();
 4 
 5     // 通过ActionContext访问application范围的属性值
 6 
 7     Integer counter = (Integer) ctx.getApplication().get("counter");
 8 
 9     if (counter == null) {
10 
11         counter = 1;
12 
13     } else {
14 
15         counter = counter + 1;
16 
17     }
18 
19     // 通过ActionContext设置application范围的属性
20 
21     ctx.getApplication().put("counter", counter);
22 
23     // 通过ActionContext设置session范围的属性
24 
25     ctx.getSession().put("user", getUser());
26 
27     if (getUser().equals("zcx")) {
28 
29         // 通过ActionContext设置request范围的属性
30 
31         ctx.put("tip", "欢迎登录");
32 
33         return SUCCESS;
34 
35     }
36 
37     // 通过ActionContext设置request范围的属性
38 
39     ctx.put("tip", "登录失败");
40 
41     return ERROR;
42 
43 }

 

取数据:注意写在HTML页面的OGNL表达式语法

${applicationScope.counter}
${
sessionScope.user}
${
requestScope.tip}

5.2实现servletcontextaware、servletrequestaware、servletresponseaware

实现ServletResponseAware 设置cookie

 

  1 public class LoginAction implements Action, ServletResponseAware {
  2 
  3     private String user;
  4 
  5     private String pwd;
  6 
  7     private String tip;
  8 
  9     private HttpServletResponse response;
 10 
 11 
 12 
 13     public String getPwd() {
 14 
 15         return pwd;
 16 
 17     }
 18 
 19 
 20 
 21     public void setPwd(String pwd) {
 22 
 23         this.pwd = pwd;
 24 
 25     }
 26 
 27 
 28 
 29     public String getUser() {
 30 
 31         return user;
 32 
 33     }
 34 
 35 
 36 
 37     public void setUser(String user) {
 38 
 39         this.user = user;
 40 
 41     }
 42 
 43 
 44 
 45     public String execute() throws Exception {
 46 
 47         ActionContext ctx = ActionContext.getContext();
 48 
 49         // 通过ActionContext访问application范围的属性值
 50 
 51         Integer counter = (Integer) ctx.getApplication().get("counter");
 52 
 53         if (counter == null) {
 54 
 55             counter = 1;
 56 
 57         } else {
 58 
 59             counter = counter + 1;
 60 
 61         }
 62 
 63         // 通过ActionContext设置application范围的属性
 64 
 65         ctx.getApplication().put("counter", counter);
 66 
 67         // 通过ActionContext设置session范围的属性
 68 
 69         ctx.getSession().put("user", getUser());
 70 
 71         if (getUser().equals("zcx")) {
 72 
 73             // 通过response添加Cookie
 74 
 75             Cookie c = new Cookie("user", getUser());
 76 
 77             c.setMaxAge(60 * 60);
 78 
 79             response.addCookie(c);
 80 
 81             // 通过ActionContext设置request范围的属性
 82 
 83             ctx.put("tip", "服务器提示:您已经成功的登录");
 84 
 85             return SUCCESS;
 86 
 87         }
 88 
 89         // 通过ActionContext设置request范围的属性
 90 
 91         ctx.put("tip", "登录失败");
 92 
 93         return ERROR;
 94 
 95     }
 96 
 97 
 98 
 99     public String getTip() {
100 
101         return tip;
102 
103     }
104 
105 
106 
107     public void setTip(String tip) {
108 
109         this.tip = tip;
110 
111     }
112 
113 
114 
115     @Override
116 
117     public void setServletResponse(HttpServletResponse httpServletResponse) {
118 
119         this.response = response;
120 
121     }
122 
123 }

 

 

5.3使用servletactioncontext

  1 public class LoginAction implements Action {
  2 
  3     private String user;
  4 
  5     private String pwd;
  6 
  7     private String tip;
  8 
  9     private HttpServletResponse response;
 10 
 11 
 12 
 13     public String getPwd() {
 14 
 15         return pwd;
 16 
 17     }
 18 
 19 
 20 
 21     public void setPwd(String pwd) {
 22 
 23         this.pwd = pwd;
 24 
 25     }
 26 
 27 
 28 
 29     public String getUser() {
 30 
 31         return user;
 32 
 33     }
 34 
 35 
 36 
 37     public void setUser(String user) {
 38 
 39         this.user = user;
 40 
 41     }
 42 
 43 
 44 
 45     public String execute() throws Exception {
 46 
 47         ActionContext ctx = ActionContext.getContext();
 48 
 49         // 通过ActionContext访问application范围的属性值
 50 
 51         Integer counter = (Integer) ctx.getApplication().get("counter");
 52 
 53         if (counter == null) {
 54 
 55             counter = 1;
 56 
 57         } else {
 58 
 59             counter = counter + 1;
 60 
 61         }
 62 
 63         // 通过ActionContext设置application范围的属性
 64 
 65         ctx.getApplication().put("counter", counter);
 66 
 67         // 通过ActionContext设置session范围的属性
 68 
 69         ctx.getSession().put("user", getUser());
 70 
 71         if (getUser().equals("zcx")) {
 72 
 73             // 通过response添加Cookie
 74 
 75             Cookie c = new Cookie("user", getUser());
 76 
 77             c.setMaxAge(60 * 60);
 78 
 79             ServletActionContext.getResponse().addCookie(c);
 80 
 81             // 通过ActionContext设置request范围的属性
 82 
 83             ctx.put("tip", "服务器提示:您已经成功的登录");
 84 
 85             return SUCCESS;
 86 
 87         }
 88 
 89         // 通过ActionContext设置request范围的属性
 90 
 91         ctx.put("tip", "登录失败");
 92 
 93         return ERROR;
 94 
 95     }
 96 
 97 
 98 
 99     public String getTip() {
100 
101         return tip;
102 
103     }
104 
105 
106 
107     public void setTip(String tip) {
108 
109         this.tip = tip;
110 
111     }
112 
113 
114 
115 }

 

Struts2-学习笔记系列(4)-访问servlet api

标签:

原文地址:http://www.cnblogs.com/zijiyanxi/p/5559675.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!