在Struts2中使用类似传统的servlet、session、application作用域对象

在MVC模式中,我们对servlet、session、application三大对象并不陌生,是JSP的内置对象,对于我们在页面与servlet之间传值,交互等,有很大作用,到了Struts1中,我们依然可以建立一个**ACTION来继承Action,然后在public ActionForward execute(ActionMapping mapping,  ActionForm form, HttpServletRequest request,HttpServletResponse response){}中使用request与response等,那么在Struts2中execute()方法不需要继承任何类,那么我们如何实现以上的作用呢,在Struts2中有三个方法,可以实现上面的功能。

1、使用Struts2的ActionContext对象

Code:
  1. public String execute(){   
  2.     ActionContext context = ActionContext.getContext();   
  3.     String path=this.NONE;   
  4.     if("liping".equals(this.getUsername())&"123456".equals(this.getUserpassword())){   
  5.         path="succ";   
  6.         context.put("name"this.getUsername());   
  7.     }else{   
  8.         context.getSession().put("message""你登录失败了!");   
  9.         path="fail";   
  10.     }   
  11.     return path;   
  12. }  

2、使用Struts2的ServletActionContext对象

Code:
  1. public String execute(){   
  2.         ServletActionContext context=null;   
  3.         String path=this.NONE;   
  4.         if("liping".equals(this.getUsername())&"123456".equals(this.getUserpassword())){   
  5.             path="succ";   
  6.             //context.put("name", this.getUsername());   
  7.             context.getRequest().setAttribute("name"this.getUsername());   
  8.         }else{   
  9.             //context.getSession().put("message", "你登录失败了!");   
  10.             context.getRequest().getSession().setAttribute("message""你登录失败了!");   
  11.             path="fail";   
  12.         }   
  13.         return path;   
  14.     }  

3、需使用org.apache.struts2.interceptor org.apache.struts2.util包下的
 //ServletRequestAware,SessionAware,ServletContextAware,ServletResponseAware

Code:
  1. public class LoginAction implements Action,ServletRequestAware,SessionAware,ServletContextAware,ServletResponseAware{   
  2.     private HttpServletRequest request;   
  3.     private Map<String, Object> session;   
  4.     private ServletContext application;   
  5.     private HttpServletResponse response;   
  6.     private String username;   
  7.     private String userpassword;   
  8.     private String abc;   
  9.     public String getAbc() {   
  10.         return abc;   
  11.     }   
  12.   
  13.     public void setAbc(String abc) {   
  14.         this.abc = abc;   
  15.     }   
  16.   
  17.     public String getUsername() {   
  18.         return username;   
  19.     }   
  20.   
  21.     public void setUsername(String username) {   
  22.         this.username = username;   
  23.     }   
  24.   
  25.     public String getUserpassword() {   
  26.         return userpassword;   
  27.     }   
  28.   
  29.     public void setUserpassword(String userpassword) {   
  30.         this.userpassword = userpassword;   
  31.     }   
  32.     //使用传统的servlet session application对象   
  33.     //需使用org.apache.struts2.interceptor org.apache.struts2.util包下的   
  34.     //ServletRequestAware,SessionAware,ServletContextAware,ServletResponseAware   
  35.     public String execute1(){   
  36.         //ActionContext context = ActionContext.getContext();   
  37.         String path=this.NONE;   
  38.         if("liping".equals(this.getUsername())&"123456".equals(this.getUserpassword())){   
  39.             path="succ";   
  40.             request.setAttribute("name"this.getUsername());   
  41.             this.setAbc("欢迎你!");   
  42.         }else{   
  43.             //context.getSession().put("message", "你登录失败了!");   
  44.             session.put("message""你登录失败了!");   
  45.             path="fail";   
  46.         }   
  47.         return path;   
  48.     }   
  49.   
  50.     public void setServletRequest(HttpServletRequest request) {   
  51.         this.request=request;      
  52.     }   
  53.   
  54.     public void setSession(Map<String, Object> session) {   
  55.         this.session=session;   
  56.            
  57.     }   
  58.   
  59.     public void setServletContext(ServletContext application) {   
  60.         this.application=application;   
  61.            
  62.     }   
  63.   
  64.     public void setServletResponse(HttpServletResponse response) {   
  65.         this.response=response;   
  66.            
  67.     }   
  68. }  

这样,就可以实现了。一般第二种方法比较常用。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值