码迷,mamicode.com
首页 > Web开发 > 详细

Struts2学习之路(五)—— 访问web元素

时间:2015-07-15 22:44:19      阅读:229      评论:0      收藏:0      [点我收藏+]

标签:

以下是Action类中访问web元素的示例,共有四种方法。其中第一种和第三种依赖容器,第二种和第四种利用IOC思想。前面两种取得Map类型的request、session、application;后面两种是真实类型HttpServletRequest、HttpServletSession、ServletContext的引用。

1.第一种方法:

 1 package com.user.action;
 2 
 3 import java.util.Map;
 4 
 5 import com.opensymphony.xwork2.ActionContext;
 6 import com.opensymphony.xwork2.ActionSupport;
 7 
 8 public class LoginAction1 extends ActionSupport{
 9     private Map request;
10     private Map session;
11     private Map application;
12     
13     public LoginAction1() {
14         request = (Map<String, String>) ActionContext.getContext().get("request");
15         session = ActionContext.getContext().getSession();
16         application = ActionContext.getContext().getApplication();
17     }
18     
19     public String execute(){
20         request.put("r1","r1");
21         session.put("s1","s1");
22         application.put("a1","a1");
23         return SUCCESS;
24     }
25 }

2.第二种方法:(推荐)

 1 package com.user.action;
 2 
 3 import java.util.Map;
 4 
 5 import org.apache.struts2.interceptor.ApplicationAware;
 6 import org.apache.struts2.interceptor.RequestAware;
 7 import org.apache.struts2.interceptor.SessionAware;    
 8 
 9 import com.opensymphony.xwork2.ActionSupport;
10 
11 //IOC程序设计思想
12 
13 public class LoginAction2 extends ActionSupport implements RequestAware,SessionAware,ApplicationAware {
14     private Map<String,Object> request;
15     private Map<String,Object> session;
16     private Map<String,Object> application;
17     
18     public String execute(){
19         request.put("r1", "r1");
20         session.put("s1", "s1");
21         application.put("a1", "a1");
22         return SUCCESS;
23     }
24     
25     @Override
26     public void setRequest(Map<String, Object> request) {
27         this.request = request;
28     }
29     
30 
31     @Override
32     public void setSession(Map<String, Object> session) {
33         this.session = session;
34         
35     }
36 
37     @Override
38     public void setApplication(Map<String, Object> application) {
39         this.application = application;
40         
41     }
42 }

3.第三种方法:

 1 package com.user.action;
 2 
 3 import javax.servlet.ServletContext;
 4 import javax.servlet.http.HttpServletRequest;
 5 import javax.servlet.http.HttpSession;
 6 
 7 import org.apache.struts2.ServletActionContext;
 8 
 9 import com.opensymphony.xwork2.ActionSupport;
10 
11 public class LoginAction3 extends ActionSupport {
12     private HttpServletRequest request;
13     private HttpSession session;
14     private ServletContext application;
15     
16     public LoginAction3() {
17         request = ServletActionContext.getRequest();
18         session = request.getSession();
19         application = session.getServletContext();
20     }
21     public String execute(){
22         request.setAttribute("r1", "r1");
23         session.setAttribute("s1", "s1");
24         application.setAttribute("a1", "a1");
25         return SUCCESS;
26     }
27 }

4.第四种方法:

 1 package com.user.action;
 2 
 3 import javax.servlet.ServletContext;
 4 import javax.servlet.http.HttpServletRequest;
 5 import javax.servlet.http.HttpSession;
 6 
 7 import org.apache.struts2.interceptor.ServletRequestAware;
 8 
 9 import com.opensymphony.xwork2.ActionSupport;
10 
11 public class LoginAction4 extends ActionSupport implements ServletRequestAware {
12     private HttpServletRequest request;
13     private HttpSession session;
14     private ServletContext application;
15     
16     public String execute(){
17         request.setAttribute("r1", "r1");
18         session.setAttribute("s1", "s1");
19         application.setAttribute("a1", "a1");
20         return SUCCESS;
21     }
22     
23     public void setServletRequest(HttpServletRequest request){
24         this.request = request;
25         this.session = session;
26         this.application = application;
27     }
28 }

 

Struts2学习之路(五)—— 访问web元素

标签:

原文地址:http://www.cnblogs.com/denisechen/p/4649656.html

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