通过ActionContext获取request session application 以及ActionContext的简单解析「建议收藏」

通过ActionContext获取request session application 以及ActionContext的简单解析「建议收藏」ActionContext:action的上下文对象。获取application://获取ActionContext对象是action的上下文对象ActionContextactionContext=ActionContext.getContext();//获取到application对应的map并且向其中添加一个属性Map<String,Obj…

大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。

Jetbrains全系列IDE使用 1年只要46元 售后保障 童叟无欺

ActionContext:action的上下文对象。

获取application :

// 获取ActionContext对象    是action的上下文对象
ActionContext actionContext = ActionContext.getContext();

// 获取到application对应的map 并且向其中添加一个属性
Map<String, Object> map = actionContext.getApplication();
map.put("applicationKey", "applicationValue");

获取session:

// session
Map<String, Object> _map = actionContext.getSession();
_map.put("sessionKey", "sessionValue");

获取request:

     疑问:但是我们去编写程序时却发现   ActionContext并没有提供像获取session,application的方法  那我们是否可以通过session来获取呢  答案:不能。那这样的我们就没办法了。这个时候我们需要看下ActionContext的源码。

  1.  先看一下getSession的这个方法吧

 

// ActionContext方法调用getSession返回一个map  内部调用了get方法  参数是个SESSION
// SESSION:"com.opensymphony.xwork2.ActionContext.session";
public Map<String, Object> getSession() {
        return (Map<String, Object>) get(SESSION);
}

    2. 再看一下getApplication方法

// 也是内部调用了get()  参数APPLICATION
// APPLICATION:"com.opensymphony.xwork2.ActionContext.application";
public Map<String, Object> getApplication() {
        return (Map<String, Object>) get(APPLICATION);
}

以上源码:有一些规律  都是调用get方法  传了一个参数   那么 获取request对象 是否 也是需要调用get方法然后参数是个常量呢 ,但是最终找了一遍,确实没有遵循传入常量的符合方法  但是:

 // 可以看出ActionContext对象是个map
 public Object get(String key) {
        return context.get(key);
 }

结论: 也就说ActionContext对象的内部结构是map嵌套map, session等对象是放在actionContext的value中.那么我们可以通过debug来获取ActionContext对象吧  通过我们也要根据actionContext的session结构  差不多分析出来 request在actionContext的存放   应该是类似。

通过ActionContext获取request session application 以及ActionContext的简单解析「建议收藏」

{com.opensymphony.xwork2.ActionContext.locale=zh_CN, request={struts.actionMapping=ActionMapping{name='index', namespace='/', method='null', extension='action', params=null, result=null}, __cleanup_recursion_counter=1, struts.valueStack=com.opensymphony.xwork2.ognl.OgnlValueStack@ef74f84}, struts.actionMapping=ActionMapping{name='index', namespace='/', method='null', extension='action', params=null, result=null}, com.opensymphony.xwork2.ActionContext.actionInvocation=com.opensymphony.xwork2.DefaultActionInvocation@742c6377, session={}, xwork.NullHandler.createNullObjects=false, com.opensymphony.xwork2.util.ValueStack.ValueStack=com.opensymphony.xwork2.ognl.OgnlValueStack@ef74f84, com.opensymphony.xwork2.dispatcher.HttpServletRequest=org.apache.struts2.dispatcher.StrutsRequestWrapper@5f2d6a4a, com.opensymphony.xwork2.dispatcher.HttpServletResponse=org.apache.catalina.connector.ResponseFacade@4cb71783, com.opensymphony.xwork2.ActionContext.container=com.opensymphony.xwork2.inject.ContainerImpl@7888eafe, com.opensymphony.xwork2.ActionContext.parameters={}, com.opensymphony.xwork2.dispatcher.ServletContext=org.apache.catalina.core.ApplicationContextFacade@6f280943, com.opensymphony.xwork2.ActionContext.application={org.apache.jasper.compiler.TldCache=org.apache.jasper.compiler.TldCache@669792bb, javax.servlet.context.tempdir=F:\stsWorkSpace1\.metadata\.plugins\org.eclipse.wst.server.core\tmp1\work\Catalina\localhost\ROOT, freemarker.Configuration=freemarker.template.Configuration@7e1df4ad, org.apache.jasper.runtime.JspApplicationContextImpl=org.apache.jasper.runtime.JspApplicationContextImpl@5a3887e2, org.apache.catalina.jsp_classpath=/F:/开发专用/tomcat/apache-tomcat-8.5.12/lib/;/F:/开发专用/tomcat/apache-tomcat-8.5.12/lib/annotations-api.jar;/F:/开发专用/tomcat/apache-tomcat-8.5.12/lib/catalina-ant.jar;/F:/开发专用/tomcat/apache-tomcat-8.5.12/lib/catalina-ha.jar;/F:/开发专用/tomcat/apache-tomcat-8.5.12/lib/catalina-storeconfig.jar;/F:/开发专用/tomcat/apache-tomcat-8.5.12/lib/catalina-tribes.jar;/F:/开发专用/tomcat/apache-tomcat-8.5.12/lib/catalina.jar;/F:/开发专用/tomcat/apache-tomcat-8.5.12/lib/ecj-4.6.1.jar;/F:/开发专用/tomcat/apache-tomcat-8.5.12/lib/el-api.jar;/F:/开发专用/tomcat/apache-tomcat-8.5.12/lib/jasper-el.jar;/F:/开发专用/tomcat/apache-tomcat-8.5.12/lib/jasper.jar;/F:/开发专用/tomcat/apache-tomcat-8.5.12/lib/jaspic-api.jar;/F:/开发专用/tomcat/apache-tomcat-8.5.12/lib/jsp-api.jar;/F:/开发专用/tomcat/apache-tomcat-8.5.12/lib/servlet-api.jar;/F:/开发专用/tomcat/apache-tomcat-8.5.12/lib/tomcat-api.jar;/F:/开发专用/tomcat/apache-tomcat-8.5.12/lib/tomcat-coyote.jar;/F:/开发专用/tomcat/apache-tomcat-8.5.12/lib/tomcat-dbcp.jar;/F:/开发专用/tomcat/apache-tomcat-8.5.12/lib/tomcat-i18n-es.jar;/F:/开发专用/tomcat/apache-tomcat-8.5.12/lib/tomcat-i18n-fr.jar;/F:/开发专用/tomcat/apache-tomcat-8.5.12/lib/tomcat-i18n-ja.jar;/F:/开发专用/tomcat/apache-tomcat-8.5.12/lib/tomcat-jdbc.jar;/F:/开发专用/tomcat/apache-tomcat-8.5.12/lib/tomcat-jni.jar;/F:/开发专用/tomcat/apache-tomcat-8.5.12/lib/tomcat-util-scan.jar;/F:/开发专用/tomcat/apache-tomcat-8.5.12/lib/tomcat-util.jar;/F:/开发专用/tomcat/apache-tomcat-8.5.12/lib/tomcat-websocket.jar;/F:/开发专用/tomcat/apache-tomcat-8.5.12/lib/websocket-api.jar;/C:/Program Files/Java/jdk1.8.0_181/jre/lib/resources.jar;/C:/Program Files/Java/jdk1.8.0_181/jre/lib/rt.jar;/C:/Program Files/Java/jdk1.8.0_181/jre/lib/jsse.jar;/C:/Program Files/Java/jdk1.8.0_181/jre/lib/jce.jar;/C:/Program Files/Java/jdk1.8.0_181/jre/lib/charsets.jar;/C:/Program Files/Java/jdk1.8.0_181/jre/lib/jfr.jar;/F:/开发专用/tomcat/apache-tomcat-8.5.12/bin/bootstrap.jar;/F:/开发专用/tomcat/apache-tomcat-8.5.12/bin/tomcat-juli.jar;/C:/Program Files/Java/jdk1.8.0_181/lib/tools.jar;/F:/开发专用/sts1/sts-bundle/sts-3.9.5.RELEASE/configuration/org.eclipse.osgi/316/0/.cp/lib/javaagent-shaded.jar;/C:/Program Files/Java/jdk1.8.0_181/jre/lib/ext/access-bridge-64.jar;/C:/Program Files/Java/jdk1.8.0_181/jre/lib/ext/cldrdata.jar;/C:/Program Files/Java/jdk1.8.0_181/jre/lib/ext/dnsns.jar;/C:/Program Files/Java/jdk1.8.0_181/jre/lib/ext/jaccess.jar;/C:/Program Files/Java/jdk1.8.0_181/jre/lib/ext/jfxrt.jar;/C:/Program Files/Java/jdk1.8.0_181/jre/lib/ext/localedata.jar;/C:/Program Files/Java/jdk1.8.0_181/jre/lib/ext/mysql-connector-java-5.1.6.jar;/C:/Program Files/Java/jdk1.8.0_181/jre/lib/ext/nashorn.jar;/C:/Program Files/Java/jdk1.8.0_181/jre/lib/ext/sunec.jar;/C:/Program Files/Java/jdk1.8.0_181/jre/lib/ext/sunjce_provider.jar;/C:/Program Files/Java/jdk1.8.0_181/jre/lib/ext/sunmscapi.jar;/C:/Program Files/Java/jdk1.8.0_181/jre/lib/ext/sunpkcs11.jar;/C:/Program Files/Java/jdk1.8.0_181/jre/lib/ext/zipfs.jar, javax.websocket.server.ServerContainer=org.apache.tomcat.websocket.server.WsServerContainer@474475e6, org.apache.tomcat.InstanceManager=org.apache.catalina.core.DefaultInstanceManager@333d78a6, applicationKey=applicationValue, org.apache.jasper.compiler.ELInterpreter=org.apache.jasper.compiler.ELInterpreterFactory$DefaultELInterpreter@773eb86d, org.apache.catalina.resources=org.apache.catalina.webresources.StandardRoot@3f31ff7c, org.apache.tomcat.JarScanner=org.apache.tomcat.util.scan.StandardJarScanner@3e372e87}, com.opensymphony.xwork2.ActionContext.session={}, application={org.apache.jasper.compiler.TldCache=org.apache.jasper.compiler.TldCache@669792bb, javax.servlet.context.tempdir=F:\stsWorkSpace1\.metadata\.plugins\org.eclipse.wst.server.core\tmp1\work\Catalina\localhost\ROOT, freemarker.Configuration=freemarker.template.Configuration@7e1df4ad, org.apache.jasper.runtime.JspApplicationContextImpl=org.apache.jasper.runtime.JspApplicationContextImpl@5a3887e2, org.apache.catalina.jsp_classpath=/F:/开发专用/tomcat/apache-tomcat-8.5.12/lib/;/F:/开发专用/tomcat/apache-tomcat-8.5.12/lib/annotations-api.jar;/F:/开发专用/tomcat/apache-tomcat-8.5.12/lib/catalina-ant.jar;/F:/开发专用/tomcat/apache-tomcat-8.5.12/lib/catalina-ha.jar;/F:/开发专用/tomcat/apache-tomcat-8.5.12/lib/catalina-storeconfig.jar;/F:/开发专用/tomcat/apache-tomcat-8.5.12/lib/catalina-tribes.jar;/F:/开发专用/tomcat/apache-tomcat-8.5.12/lib/catalina.jar;/F:/开发专用/tomcat/apache-tomcat-8.5.12/lib/ecj-4.6.1.jar;/F:/开发专用/tomcat/apache-tomcat-8.5.12/lib/el-api.jar;/F:/开发专用/tomcat/apache-tomcat-8.5.12/lib/jasper-el.jar;/F:/开发专用/tomcat/apache-tomcat-8.5.12/lib/jasper.jar;/F:/开发专用/tomcat/apache-tomcat-8.5.12/lib/jaspic-api.jar;/F:/开发专用/tomcat/apache-tomcat-8.5.12/lib/jsp-api.jar;/F:/开发专用/tomcat/apache-tomcat-8.5.12/lib/servlet-api.jar;/F:/开发专用/tomcat/apache-tomcat-8.5.12/lib/tomcat-api.jar;/F:/开发专用/tomcat/apache-tomcat-8.5.12/lib/tomcat-coyote.jar;/F:/开发专用/tomcat/apache-tomcat-8.5.12/lib/tomcat-dbcp.jar;/F:/开发专用/tomcat/apache-tomcat-8.5.12/lib/tomcat-i18n-es.jar;/F:/开发专用/tomcat/apache-tomcat-8.5.12/lib/tomcat-i18n-fr.jar;/F:/开发专用/tomcat/apache-tomcat-8.5.12/lib/tomcat-i18n-ja.jar;/F:/开发专用/tomcat/apache-tomcat-8.5.12/lib/tomcat-jdbc.jar;/F:/开发专用/tomcat/apache-tomcat-8.5.12/lib/tomcat-jni.jar;/F:/开发专用/tomcat/apache-tomcat-8.5.12/lib/tomcat-util-scan.jar;/F:/开发专用/tomcat/apache-tomcat-8.5.12/lib/tomcat-util.jar;/F:/开发专用/tomcat/apache-tomcat-8.5.12/lib/tomcat-websocket.jar;/F:/开发专用/tomcat/apache-tomcat-8.5.12/lib/websocket-api.jar;/C:/Program Files/Java/jdk1.8.0_181/jre/lib/resources.jar;/C:/Program Files/Java/jdk1.8.0_181/jre/lib/rt.jar;/C:/Program Files/Java/jdk1.8.0_181/jre/lib/jsse.jar;/C:/Program Files/Java/jdk1.8.0_181/jre/lib/jce.jar;/C:/Program Files/Java/jdk1.8.0_181/jre/lib/charsets.jar;/C:/Program Files/Java/jdk1.8.0_181/jre/lib/jfr.jar;/F:/开发专用/tomcat/apache-tomcat-8.5.12/bin/bootstrap.jar;/F:/开发专用/tomcat/apache-tomcat-8.5.12/bin/tomcat-juli.jar;/C:/Program Files/Java/jdk1.8.0_181/lib/tools.jar;/F:/开发专用/sts1/sts-bundle/sts-3.9.5.RELEASE/configuration/org.eclipse.osgi/316/0/.cp/lib/javaagent-shaded.jar;/C:/Program Files/Java/jdk1.8.0_181/jre/lib/ext/access-bridge-64.jar;/C:/Program Files/Java/jdk1.8.0_181/jre/lib/ext/cldrdata.jar;/C:/Program Files/Java/jdk1.8.0_181/jre/lib/ext/dnsns.jar;/C:/Program Files/Java/jdk1.8.0_181/jre/lib/ext/jaccess.jar;/C:/Program Files/Java/jdk1.8.0_181/jre/lib/ext/jfxrt.jar;/C:/Program Files/Java/jdk1.8.0_181/jre/lib/ext/localedata.jar;/C:/Program Files/Java/jdk1.8.0_181/jre/lib/ext/mysql-connector-java-5.1.6.jar;/C:/Program Files/Java/jdk1.8.0_181/jre/lib/ext/nashorn.jar;/C:/Program Files/Java/jdk1.8.0_181/jre/lib/ext/sunec.jar;/C:/Program Files/Java/jdk1.8.0_181/jre/lib/ext/sunjce_provider.jar;/C:/Program Files/Java/jdk1.8.0_181/jre/lib/ext/sunmscapi.jar;/C:/Program Files/Java/jdk1.8.0_181/jre/lib/ext/sunpkcs11.jar;/C:/Program Files/Java/jdk1.8.0_181/jre/lib/ext/zipfs.jar, javax.websocket.server.ServerContainer=org.apache.tomcat.websocket.server.WsServerContainer@474475e6, org.apache.tomcat.InstanceManager=org.apache.catalina.core.DefaultInstanceManager@333d78a6, applicationKey=applicationValue, org.apache.jasper.compiler.ELInterpreter=org.apache.jasper.compiler.ELInterpreterFactory$DefaultELInterpreter@773eb86d, org.apache.catalina.resources=org.apache.catalina.webresources.StandardRoot@3f31ff7c, org.apache.tomcat.JarScanner=org.apache.tomcat.util.scan.StandardJarScanner@3e372e87}, xwork.MethodAccessor.denyMethodExecution=false, com.opensymphony.xwork2.ActionContext.conversionErrors={}, action=com.lfl.webStruts_01.HelloWorldStruts@5847b118, report.conversion.errors=false, com.opensymphony.xwork2.ActionContext.name=index, attr=AttributeMap {request={struts.actionMapping=ActionMapping{name='index', namespace='/', method='null', extension='action', params=null, result=null}, __cleanup_recursion_counter=1, struts.valueStack=com.opensymphony.xwork2.ognl.OgnlValueStack@ef74f84}, session={}, application={org.apache.jasper.compiler.TldCache=org.apache.jasper.compiler.TldCache@669792bb, javax.servlet.context.tempdir=F:\stsWorkSpace1\.metadata\.plugins\org.eclipse.wst.server.core\tmp1\work\Catalina\localhost\ROOT, freemarker.Configuration=freemarker.template.Configuration@7e1df4ad, org.apache.jasper.runtime.JspApplicationContextImpl=org.apache.jasper.runtime.JspApplicationContextImpl@5a3887e2, org.apache.catalina.jsp_classpath=/F:/开发专用/tomcat/apache-tomcat-8.5.12/lib/;/F:/开发专用/tomcat/apache-t...

 这里面就是ActionContext的所有东西   里面路径有中文不要介意,因为方便我找东西,啧啧。

通过ActionContext获取request session application 以及ActionContext的简单解析「建议收藏」

这个是session在ActionContext对象中的格式  是个map 这样的话  我们就直接搜索request 

通过ActionContext获取request session application 以及ActionContext的简单解析「建议收藏」

到这里的话   应该已经知道了吧

 

通过ActionContext获取request session application 以及ActionContext的简单解析「建议收藏」

我们只需要通过字符串(”request”)key获取request对象就可以了 

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/195628.html原文链接:https://javaforall.cn

【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛

【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...

(0)


相关推荐

发表回复

您的电子邮箱地址不会被公开。

关注全栈程序员社区公众号