在Action中获取:request,session,application。
共四种方式,示例在下面给出,其中第二种常用。
【注意1】:我们需要知道前两种方法得到的是Map<String,Object>,而后两种方式得到的才是真正的request等对象。而Map就是把request对象中的属性取出做成了键值对而已。
【注意2】:另外如果就是为了在action和jsp传递参数的话,只需要在action中定义成员,然后Jsp中利用struts标签<s:property value="name"/>就能够访问到数据,而这些内容都是被保存在了value stack中。关于value stack 和 stack context 会在后面得内容涉及。
?
方法一:
public class LoginAction1 extends ActionSupport {
private Map request;
private Map session;
private Map application;
public LoginAction1() {
request = (Map)ActionContext.getContext().get("request");
session = ActionContext.getContext().getSession();
application = ActionContext.getContext().getApplication();
}
public String execute() {
request.put("r1", "r1");
session.put("s1", "s1");
application.put("a1", "a1");
return SUCCESS;
}
}
方法二:
public class LoginAction2 extends ActionSupport implements RequestAware,SessionAware, ApplicationAware {
private Map<String, Object> request;
private Map<String, Object> session;
private Map<String, Object> application;
//DI dependency injection
//IoC inverse of control
public String execute() {
request.put("r1", "r1");
session.put("s1", "s1");
application.put("a1", "a1");
return SUCCESS;
}
? @Override
public void setRequest(Map<String, Object> request) {
this.request = request;
}
@Override
public void setSession(Map<String, Object> session) {
this.session = session;
}
@Override
public void setApplication(Map<String, Object> application) {
this.application = application;
}
}
方法三:
public class LoginAction3 extends ActionSupport {
private HttpServletRequest request;
private HttpSession session;
private ServletContext application;
public LoginAction3() {
request = ServletActionContext.getRequest();
session = request.getSession();
application = session.getServletContext();
}
public String execute() {
request.setAttribute("r1", "r1");
session.setAttribute("s1", "s1");
application.setAttribute("a1", "a1");
return SUCCESS;
}
}
方法四:
public class LoginAction4 extends ActionSupport implements ServletRequestAware {
private HttpServletRequest request;
private HttpSession session;
private ServletContext application;
public String execute() {
request.setAttribute("r1", "r1");
session.setAttribute("s1", "s1");
application.setAttribute("a1", "a1");
return SUCCESS;
}
@Override
public void setServletRequest(HttpServletRequest request) {
this.request = request;
this.session = request.getSession();
this.application = session.getServletContext();
}
}
相关推荐
ActionDispatch::RequestId Rails::Rack::Logger ActionDispatch::ShowExceptions ActionDispatch::DebugExceptions ActionDispatch::RemoteIp ActionDispatch::Reloader ActionDispatch::Callbacks ActiveRecord::...
Struts1只是在第一次请求的时候创建一个action实例,以后每次相同的请求都直接从内存中去读取,它是单例模式,安全性较差。 Struts2是如何实现MVC模式的? 答:在Struts2里面是将每次页面的请求进行处理,然后将请求...
redirect就是服务端根据逻辑,发送一个状态码,告诉浏览器重新去请求那个地址,一般来说浏览器会用刚才请求的所有参数重新请求,所以session,request参数都可以获取。 23、EJB与JAVA BEAN的区别? Java Bean 是可...
redirect就是服务端根据逻辑,发送一个状态码,告诉浏览器重新去请求那个地址,一般来说浏览器会用刚才请求的所有参数重新请求,所以session,request参数都可以获取。 20、EJB与JAVA BEAN的区别? Java Bean 是可复用...
XWork处理用户请求参数数据时存在漏洞,远程攻击者可以利用此漏洞在系统上执行任意命令。 Struts2中WebWork框架使用XWork基于HTTP参数名执行操作和调用,将每个HTTP参数名处理为OGNL(对象图形导航语言)语句,而...
Controlling Web Application Behavior with web.xml Section 2.1. Purpose of the Deployment Descriptor Section 2.2. Defining the Header and the Root Element Section 2.3. The Elements of web.xml ...
Controlling Web Application Behavior with web.xml Section 2.1. Purpose of the Deployment Descriptor Section 2.2. Defining the Header and the Root Element Section 2.3. The Elements of web.xml ...
' 判 断 客 户 是 是 否 在 聊 天 界 面 中 Session("nick")=Request.Form("nick") End If '------------------------ '客 户 聊 天 界 面 '------------------------ %> <form method="POST" action=...
主要实现了用户的登录系统..并且验证了struts中的跳转..还有跳转到另一个action中的类型..跳转另一个action后显示出书籍信息......并且从request域中,session域和application整个应用程序域中获取信息..
ActionForm maintains the session state for the Web application. ActionForm is an abstract class that is sub-classed for each input form model. When I say input form model, I am saying ActionForm ...
8.7.2 使用M印类型的request、session、application 8.8 疑难解惑 8.8.1 Struts Prepare And Execute Filter过滤器 8.8.2 struts.xml文件配置出错 8.9 精彩回顾 第9章 庖丁解牛 ——揭密Struts2高级技术 9.1 本章...
• session 范围中的对象是跟会话( session )绑定在一起的,每次会话结束会销毁这些对象,而 新的会话中又会重新创建。 HTTP 协议本身是无状态的,服务器和客户端要实现会话的管理,只 能借助于一些辅助的手段,如...
此外lazy="true"说明地返回整个Tfile对象时,并不返回fileContent这个字段的数据,只有在显式调用tfile.getFileContent()方法时才真正从数据库中获取fileContent的数据。这是Hibernate3引入的新特性,对于包含重量...
request_order ; Default Value: None ; Development Value: "GP" ; Production Value: "GP" ; session.gc_divisor ; Default Value: 100 ; Development Value: 1000 ; Production Value: 1000 ; session.hash_...
3.4.1. Working with request and session scope 3.4.2. Declaring scoped proxies in XML 3.5. Runtime value injection 3.5.1. Injecting external values 3.5.2. Wiring with the Spring Expression Language 3.6...
11.2、在Tomcat中使用数据库连接池 11.3、查找数据源 11.4、本章摘要 第12章 JSP标签编程 12.1、标签编程简介 12.2、定义一个简单的标签 —— 空标签 12.3、定义有属性的标签 12.4、TagSupport类 12.5...
11.2、在Tomcat中使用数据库连接池 11.3、查找数据源 11.4、本章摘要 第12章 JSP标签编程 12.1、标签编程简介 12.2、定义一个简单的标签 —— 空标签 12.3、定义有属性的标签 12.4、TagSupport类 12.5、定义...
11.2、在Tomcat中使用数据库连接池 11.3、查找数据源 11.4、本章摘要 第12章 JSP标签编程 12.1、标签编程简介 12.2、定义一个简单的标签 —— 空标签 12.3、定义有属性的标签 12.4、TagSupport类 12.5...
11.2、在Tomcat中使用数据库连接池 11.3、查找数据源 11.4、本章摘要 第12章 JSP标签编程 12.1、标签编程简介 12.2、定义一个简单的标签 —— 空标签 12.3、定义有属性的标签 12.4、TagSupport类 12.5、定义...
11.2、在Tomcat中使用数据库连接池 11.3、查找数据源 11.4、本章摘要 第12章 JSP标签编程 12.1、标签编程简介 12.2、定义一个简单的标签 —— 空标签 12.3、定义有属性的标签 12.4、TagSupport类 12.5、定义...