Sturts2中:
前后台数据的通讯,不仅需要Struts2标签 ognl表达式 还需要我们对request session application等等,这些栈上下文做一些处理,才能保证前后台通讯的顺畅!
我们可以通过以下几种办法来获取这些对象进行处理,以便进行前后台之间的通讯。
方法一:取得Map类型的request、session、application:(该方式有两种方式)
方式A
public class TestAction1 extends ActionSupport {
//步骤一:在Action类中定义private Map request;private Map session;private Map application;
private Map request;
private Map session;
private Map application;
//步骤二:初始化各个属性
public void init(){
this.request=(Map) ActionContext.getContext().get("request");
this.session=(Map)ActionContext.getContext().getSession();
this.application=(Map)ActionContext.getContext().getApplication();
}
//步骤三:在execute()或者是自己定义的方法中使用这些对象进行设值。
public String execute() {
init();
request.put("jkqiang", "亮亮");
session.put("hank", "伟伟");
application.put("john","强强");
return SUCCESS;
}
}
在前台可以通过
其中xx表示你自己在request、session、application中设定的key。
<%@ page pageEncoding="utf-8"%>
<%@taglib uri="/struts-tags" prefix="s"%>
Test
request.jkqiang"/>
==========显示效果
[Debug]
Object | Property Name | Property Value |
---|---|---|
com.jkqiang.tts.action.TestAction1 | texts | null |
actionErrors | [] | |
errors | {} | |
fieldErrors | {} | |
errorMessages | [] | |
locale | zh_CN | |
actionMessages | [] | |
com.opensymphony.xwork2.DefaultTextProvider | texts | null |
Key | Value |
---|---|
com.opensymphony.xwork2.dispatcher.HttpServletRequest | org.apache.struts2.dispatcher.StrutsRequestWrapper@8ebb5a |
com.opensymphony.xwork2.ActionContext.locale | zh_CN |
com.opensymphony.xwork2.dispatcher.HttpServletResponse | org.apache.catalina.connector.ResponseFacade@18eb00c |
com.opensymphony.xwork2.ActionContext.name | mystruts1 |
com.opensymphony.xwork2.ActionContext.application | {javax.servlet.context.tempdir=E:\apache-tomcat-6.0.18\work\Catalina\localhost\Test, org.apache.catalina.WELCOME_FILES=[Ljava.lang.String;@32ef2d, org.apache.AnnotationProcessor=org.apache.catalina.util.DefaultAnnotationProcessor@6a60d5, org.apache.jasper.runtime.JspApplicationContextImpl=org.apache.jasper.runtime.JspApplicationContextImpl@183e895, org.apache.catalina.jsp_classpath=/E:/apache-tomcat-6.0.18/webapps/Test/WEB-INF/classes/;/E:/apache-tomcat-6.0.18/webapps/Test/WEB-INF/lib/commons-fileupload-1.2.1.jar;/E:/apache-tomcat-6.0.18/webapps/Test/WEB-INF/lib/commons-logging-1.0.4.jar;/E:/apache-tomcat-6.0.18/webapps/Test/WEB-INF/lib/freemarker-2.3.15.jar;/E:/apache-tomcat-6.0.18/webapps/Test/WEB-INF/lib/javax.servlet.jsp.jstl.jar;/E:/apache-tomcat-6.0.18/webapps/Test/WEB-INF/lib/jsf-api.jar;/E:/apache-tomcat-6.0.18/webapps/Test/WEB-INF/lib/jsf-impl.jar;/E:/apache-tomcat-6.0.18/webapps/Test/WEB-INF/lib/jstl-impl.jar;/E:/apache-tomcat-6.0.18/webapps/Test/WEB-INF/lib/ognl-2.7.3.jar;/E:/apache-tomcat-6.0.18/webapps/Test/WEB-INF/lib/struts2-core-2.1.8.jar;/E:/apache-tomcat-6.0.18/webapps/Test/WEB-INF/lib/xwork-core-2.1.6.jar;/E:/apache-tomcat-6.0.18/lib/;/E:/apache-tomcat-6.0.18/lib/annotations-api.jar;/E:/apache-tomcat-6.0.18/lib/catalina-ant.jar;/E:/apache-tomcat-6.0.18/lib/catalina-ha.jar;/E:/apache-tomcat-6.0.18/lib/catalina-tribes.jar;/E:/apache-tomcat-6.0.18/lib/catalina.jar;/E:/apache-tomcat-6.0.18/lib/el-api.jar;/E:/apache-tomcat-6.0.18/lib/jasper-el.jar;/E:/apache-tomcat-6.0.18/lib/jasper-jdt.jar;/E:/apache-tomcat-6.0.18/lib/jasper.jar;/E:/apache-tomcat-6.0.18/lib/jsp-api.jar;/E:/apache-tomcat-6.0.18/lib/servlet-api.jar;/E:/apache-tomcat-6.0.18/lib/tomcat-coyote.jar;/E:/apache-tomcat-6.0.18/lib/tomcat-dbcp.jar;/E:/apache-tomcat-6.0.18/lib/tomcat-i18n-es.jar;/E:/apache-tomcat-6.0.18/lib/tomcat-i18n-fr.jar;/E:/apache-tomcat-6.0.18/lib/tomcat-i18n-ja.jar;/E:/apache-tomcat-6.0.18/bin/bootstrap.jar;/C:/jdk1.6.0_30/lib/tools.jar;/C:/jdk1.6.0_30/jre/lib/ext/dnsns.jar;/C:/jdk1.6.0_30/jre/lib/ext/localedata.jar;/C:/jdk1.6.0_30/jre/lib/ext/sunjce_provider.jar;/C:/jdk1.6.0_30/jre/lib/ext/sunmscapi.jar;/C:/jdk1.6.0_30/jre/lib/ext/sunpkcs11.jar, freemarker.Configuration=freemarker.template.Configuration@1f49969, john=强强, org.apache.catalina.resources=org.apache.naming.resources.ProxyDirContext@11aa58b} |
com.opensymphony.xwork2.ActionContext.conversionErrors | {} |
__component_stack | [org.apache.struts2.components.Debug@91e143] |
last.property.accessed | null |
com.opensymphony.xwork2.dispatcher.ServletContext | org.apache.catalina.core.ApplicationContextFacade@168f248 |
com.opensymphony.xwork2.dispatcher.PageContext | org.apache.jasper.runtime.PageContextImpl@32fb80 |
__requestWrapper.getAttribute | false |
request | {jkqiang=亮亮, .freemarker.Request=freemarker.ext.servlet.HttpRequestHashModel@116880a, javax.servlet.forward.context_path=/Test, javax.servlet.forward.request_uri=/Test/hello/mystruts1, struts.request_uri=/Test/hello/mystruts1, __cleanup_recursion_counter=1, javax.servlet.forward.servlet_path=/hello/mystruts1, struts.actionMapping=org.apache.struts2.dispatcher.mapper.ActionMapping@3d5149, .freemarker.RequestParameters=freemarker.ext.servlet.HttpRequestParametersHashModel@1a3bff5, struts.view_uri=/WEB-INF/jsp/test1.jsp, struts.valueStack=com.opensymphony.xwork2.ognl.OgnlValueStack@18f73cb} |
last.bean.accessed | null |
action | com.jkqiang.tts.action.TestAction1@16ca3d5 |
parameters | {} |
__link | [Ljava.lang.Object;@1905a42 |
application | {javax.servlet.context.tempdir=E:\apache-tomcat-6.0.18\work\Catalina\localhost\Test, org.apache.catalina.WELCOME_FILES=[Ljava.lang.String;@32ef2d, org.apache.AnnotationProcessor=org.apache.catalina.util.DefaultAnnotationProcessor@6a60d5, org.apache.jasper.runtime.JspApplicationContextImpl=org.apache.jasper.runtime.JspApplicationContextImpl@183e895, org.apache.catalina.jsp_classpath=/E:/apache-tomcat-6.0.18/webapps/Test/WEB-INF/classes/;/E:/apache-tomcat-6.0.18/webapps/Test/WEB-INF/lib/commons-fileupload-1.2.1.jar;/E:/apache-tomcat-6.0.18/webapps/Test/WEB-INF/lib/commons-logging-1.0.4.jar;/E:/apache-tomcat-6.0.18/webapps/Test/WEB-INF/lib/freemarker-2.3.15.jar;/E:/apache-tomcat-6.0.18/webapps/Test/WEB-INF/lib/javax.servlet.jsp.jstl.jar;/E:/apache-tomcat-6.0.18/webapps/Test/WEB-INF/lib/jsf-api.jar;/E:/apache-tomcat-6.0.18/webapps/Test/WEB-INF/lib/jsf-impl.jar;/E:/apache-tomcat-6.0.18/webapps/Test/WEB-INF/lib/jstl-impl.jar;/E:/apache-tomcat-6.0.18/webapps/Test/WEB-INF/lib/ognl-2.7.3.jar;/E:/apache-tomcat-6.0.18/webapps/Test/WEB-INF/lib/struts2-core-2.1.8.jar;/E:/apache-tomcat-6.0.18/webapps/Test/WEB-INF/lib/xwork-core-2.1.6.jar;/E:/apache-tomcat-6.0.18/lib/;/E:/apache-tomcat-6.0.18/lib/annotations-api.jar;/E:/apache-tomcat-6.0.18/lib/catalina-ant.jar;/E:/apache-tomcat-6.0.18/lib/catalina-ha.jar;/E:/apache-tomcat-6.0.18/lib/catalina-tribes.jar;/E:/apache-tomcat-6.0.18/lib/catalina.jar;/E:/apache-tomcat-6.0.18/lib/el-api.jar;/E:/apache-tomcat-6.0.18/lib/jasper-el.jar;/E:/apache-tomcat-6.0.18/lib/jasper-jdt.jar;/E:/apache-tomcat-6.0.18/lib/jasper.jar;/E:/apache-tomcat-6.0.18/lib/jsp-api.jar;/E:/apache-tomcat-6.0.18/lib/servlet-api.jar;/E:/apache-tomcat-6.0.18/lib/tomcat-coyote.jar;/E:/apache-tomcat-6.0.18/lib/tomcat-dbcp.jar;/E:/apache-tomcat-6.0.18/lib/tomcat-i18n-es.jar;/E:/apache-tomcat-6.0.18/lib/tomcat-i18n-fr.jar;/E:/apache-tomcat-6.0.18/lib/tomcat-i18n-ja.jar;/E:/apache-tomcat-6.0.18/bin/bootstrap.jar;/C:/jdk1.6.0_30/lib/tools.jar;/C:/jdk1.6.0_30/jre/lib/ext/dnsns.jar;/C:/jdk1.6.0_30/jre/lib/ext/localedata.jar;/C:/jdk1.6.0_30/jre/lib/ext/sunjce_provider.jar;/C:/jdk1.6.0_30/jre/lib/ext/sunmscapi.jar;/C:/jdk1.6.0_30/jre/lib/ext/sunpkcs11.jar, freemarker.Configuration=freemarker.template.Configuration@1f49969, john=强强, org.apache.catalina.resources=org.apache.naming.resources.ProxyDirContext@11aa58b} |
xwork.NullHandler.createNullObjects | false |
current.property.path | null |
attr | org.apache.struts2.util.AttributeMap@1033fb5 |
com.opensymphony.xwork2.ActionContext.container | com.opensymphony.xwork2.inject.ContainerImpl@117140b |
com.opensymphony.xwork2.ActionContext.session | {hank=伟伟} |
com.opensymphony.xwork2.ActionContext.actionInvocation | com.opensymphony.xwork2.DefaultActionInvocation@6e7127 |
com.opensymphony.xwork2.util.ValueStack.ValueStack | com.opensymphony.xwork2.ognl.OgnlValueStack@18f73cb |
session | {hank=伟伟} |
report.conversion.errors | false |
xwork.MethodAccessor.denyMethodExecution | false |
struts.actionMapping | org.apache.struts2.dispatcher.mapper.ActionMapping@3d5149 |
com.opensymphony.xwork2.ActionContext.parameters | {} |
方式B
第一步:让Action类实现RequestAware、SessionAware、ApplicationAware接口
public class TestAction2 extends ActionSupport implements RequestAware , SessionAware, ApplicationAware {
private static final long serialVersionUID = 1L;
//第二步:在Action类中定义private Map request;private Map session;private Map application;
private Map request;
private Map session;
private Map application;
//第四步:在execute()或者是自己定义的方法中使用这些对象进行设值;
public String execute() {
request.put("book", "钢铁是怎么炼成的!");
session.put("song", "义勇军进行曲!");
application.put("word","世界真美好!");
return SUCCESS;
}
//第三步:覆盖接口中的setRequest(Map
setSession(Map
setApplication(Map
public void setRequest(Map arg0) {
this.request=arg0;
}
public void setSession(Map arg0) {
this.session=arg0;
}
public void setApplication(Map arg0) {
this.application=arg0;
}
}
=======================================================================================================================
方法二:取得真实类型的request、session、application:(同样也有两种方式)
方式A
public class TestAction3 extends ActionSupport {
private static final long serialVersionUID = 1L;
//步骤一:在Action类中定义private HttpServletRequest request;
private HttpSession session;
private ServletContext application;
private HttpServletRequest request;
private HttpSession session;
private ServletContext application;
//初始化
public void init(){
this.request=ServletActionContext.getRequest();
this.session=request.getSession();
this.application=session.getServletContext();
}
//执行的时候,后台存值进去
public String execute() {
init();
request.setAttribute("dog", "我们家的狗狗!");
session.setAttribute("pig", "我们家的猪猪!");
application.setAttribute("bed","我们家的床!");
return SUCCESS;
}
}
方式B
//.Action类实现ServletRequestAware接口
public class TestAction4 extends ActionSupport implements ServletRequestAware{
private static final long serialVersionUID = 1L;
//在Action类中定义private HttpServletRequest request;private HttpSession session;private ServletContext application;
private HttpServletRequest request;
private HttpSession session;
private ServletContext application;
//.覆盖ServletRequestAware接口的方法
public void setServletRequest(HttpServletRequest request) {
this.request = request;
this.session= request.getSession();
this.application = session.getServletContext();
}
//执行的时候,后台存值进去
public String execute() {
request.setAttribute("apple", "我好爱苹果!");
session.setAttribute("orange", "我也吃橘子");
application.setAttribute("pea","我更喜欢梨!");
return SUCCESS;
}
}