• struts2获取web元素(request、session、application)


    一、Action中获取

    第一种方式:

    通过ActionContext,这种方式取到的对象是Map类型

    import java.util.Map;
    
    import com.opensymphony.xwork2.ActionContext;
    import com.opensymphony.xwork2.ActionSupport;
    
    public class UserAction extends ActionSupport{
        
        private String uid;
        
        public String login(){
            
            Map<String , Object> request = (Map<String, Object>) ActionContext.getContext().get("request");
            
            Map<String , Object> session = ActionContext.getContext().getSession();
            
            Map<String , Object> application = ActionContext.getContext().getApplication();
            
            if (uid != null) {
                
                System.out.println(uid);
                session.put("uid" , uid);
            }
            
            return SUCCESS;
        }
        
        public String getUid() {
            return uid;
        }
    
        public void setUid(String uid) {
            this.uid = uid;
        }
    }

    第二种方式:

    通过实现RequestAware,SessionAware,ApplicationAware接口来获取,这种方式获取到的依然是Map类型

    package cn.orlion.actions;
    
    import java.util.Map;
    
    import org.apache.struts2.interceptor.ApplicationAware;
    import org.apache.struts2.interceptor.RequestAware;
    import org.apache.struts2.interceptor.SessionAware;
    import com.opensymphony.xwork2.ActionSupport;
    
    public class UserAction extends ActionSupport implements RequestAware,SessionAware,ApplicationAware{
        
        private String uid;
        
        private Map<String, Object> request;
        
        private Map<String, Object> session;
        
        private Map<String, Object> application;
        
        public String login(){
            
            if (uid != null) {
                
                System.out.println(uid);
                session.put("uid" , uid);
            }
            
            return SUCCESS;
        }
        
        public String getUid() {
            return uid;
        }
    
        public void setUid(String uid) {
            this.uid = uid;
        }
    
        @Override
        public void setSession(Map<String, Object> arg0) {
            // TODO Auto-generated method stub
            this.session = arg0;
        }
    
        @Override
        public void setRequest(Map<String, Object> arg0) {
            // TODO Auto-generated method stub
            this.request = arg0;
        }
    
        @Override
        public void setApplication(Map<String, Object> arg0) {
            // TODO Auto-generated method stub
            this.application = arg0;
        }
    }

    第三种方式:

    通过实现ServletRequestAware接口获取到request,然后用request去获取session和application,这种方式获取到的是HttpServletRequest ,HttpSession ,ServletContext类型。

    package cn.orlion.actions;
    
    import javax.servlet.ServletContext;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpSession;
    import org.apache.struts2.interceptor.ServletRequestAware;
    
    import com.opensymphony.xwork2.ActionSupport;
    
    public class UserAction extends ActionSupport implements ServletRequestAware{
        
        private String uid;
        
        private HttpServletRequest request;
        
        private HttpSession session;
        
        private ServletContext application;
        
        public String login(){
            
            if (uid != null) {
                
                session = request.getSession();
                
                application = session.getServletContext();
                
                System.out.println(uid);
           // HttpSession类型,所以用setAttribute方法 session.setAttribute(
    "uid" , uid); } return SUCCESS; } public String getUid() { return uid; } public void setUid(String uid) { this.uid = uid; } @Override public void setServletRequest(HttpServletRequest arg0) { // TODO Auto-generated method stub this.request = arg0; } }

    第四种方式:

    用ServletActionContext得到request再得到sesion和application,这种获取到的类型与第二种相同

    package cn.orlion.actions;
    
    import javax.servlet.ServletContext;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpSession;
    
    import org.apache.struts2.ServletActionContext;
    
    import com.opensymphony.xwork2.ActionSupport;
    
    public class UserAction extends ActionSupport{
        
        private String uid;
        
        private HttpServletRequest request;
        
        private HttpSession session;
        
        private ServletContext application;
        
        public String login(){
            
            if (uid != null) {
                
                request = ServletActionContext.getRequest();
                
                session = request.getSession();
                
                application = session.getServletContext();
                
                System.out.println(uid);
                session.setAttribute("uid" , uid);
            }
            
            return SUCCESS;
        }
        
        public String getUid() {
            
            return uid;
        }
    
        public void setUid(String uid) {
            this.uid = uid;
        }
    }

    二、JSP中获取

    通过session.setAttribute()方式设置的值不能通过<s:property value="#session.uid" />方式获取到。

    <%@ page language="java" contentType="text/html; charset=UTF-8"
        pageEncoding="UTF-8"%>
    <%@ taglib uri="/struts-tags" prefix="s" %>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>success</title>
    </head>
    <body>
        uid为:<s:property value="#session.uid" />|||<br/>
           ${ sessionScope.uid }
           
           <s:debug></s:debug>
    </body>
    </html>
  • 相关阅读:
    ASP.NET 页面间传递参数的方法
    Javascript 检查字符串是否是数字的几种方法
    周鸿祎教你写商业计划书
    提高网站程序性能的十条建议
    启动IIS6下Gzip功能详细操作步骤
    IT创业前要深思的八大问题
    学了php才知道MVC是怎么回事
    不能访问windows installer 服务 的解决方法
    SNS 相关
    【技术】Ubuntu上位机配置Blackfin开发环境手记
  • 原文地址:https://www.cnblogs.com/orlion/p/5017009.html
Copyright © 2020-2023  润新知