zoukankan      html  css  js  c++  java
  • struts2学习笔记之六:struts2的Action访问ServletAPI的几种方式

    方法一:通过ActionContext访问SerlvetAPI,这种方式没有侵入性
    Action类部分代码
    import com.opensymphony.xwork2.ActionContext;
    
        public String execute() throws Exception {
            if("admin".equals(user.getUsername()) && "admin".equals(user.getPassword())){
                //获取Map方式的request
                //ActionContext.getContext().put("msg", "登录成功");
                //获取Map方式的Session
                //ActionContext.getContext().getSession().put("msg", "登录成功");
                //获取Map方式的Application 
                ActionContext.getContext().getApplication().put("msg", "登录成功");
                return "success";
                
            }
            
            return "error";
        }
    方法二:通过ServletActionContext访问SerlvetAPI,这种方式没有侵入性,建议使用这种方式
    Action类部分代码
    import org.apache.struts2.ServletActionContext;
        public String execute() throws Exception {
            if("admin".equals(user.getUsername()) && "admin".equals(user.getPassword())){
                ServletActionContext.getRequest().setAttribute("msg", "登录成功");
                ServletActionContext.getResponse();
                ServletActionContext.getPageContext().setAttribute("msg", "登录成功");
                ServletActionContext.getRequest().getSession().setAttribute("msg", "登录成功");
                return "success";
                
            }
            
            return "error";
        }
    方法三:实现装配接口,实现方法(ServletRequestAware,ServletResponseAware)
    Action类
    package com.djoker.struts2;
    
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    import org.apache.struts2.interceptor.ServletRequestAware;
    import org.apache.struts2.interceptor.ServletResponseAware;
    
    
    public class LoginAction implements ServletRequestAware,ServletResponseAware{
    
        private User user;
        
        public User getUser() {
            return user;
        }
    
        public void setUser(User user) {
            this.user = user;
        }
    
        private HttpServletRequest request;
        
        private HttpServletResponse response;
        
        public String execute() throws Exception {
            if("admin".equals(user.getUsername()) && "admin".equals(user.getPassword())){
                this.request.setAttribute("msg", "登录成功");
                return "success";
                
            }
            
            return "error";
        }
    
        @Override
        public void setServletResponse(HttpServletResponse response) {
            this.response = response;
        }
    
        @Override
        public void setServletRequest(HttpServletRequest request) {
            this.request = request;
        }
        
    }
  • 相关阅读:
    ajax的基础知识
    前端必备的php的基础知识点
    关于事件的简单汇总
    Django rest-framework(目录)
    Django(目录)
    前端(目录)
    数据库知识(目录)
    数据库基础
    并发编程(目录)
    网络编程
  • 原文地址:https://www.cnblogs.com/djoker/p/6218587.html
Copyright © 2011-2022 走看看