Struts2学习笔记----访问Servlet API
2011-07-06 15:18
337 查看
在struts2框架中,Action与Servlet Api相互分离,这种底耦合性给开发带来了便利。但是很多时候,Action不访问Servlet API,将不能够实现业务逻辑处理。例如,得到Session和Application等。
struts2为我们提供了两种途径来访问ServletAPI,一种是间接访问;还有一种是直接访问。其中直接访问有区分为IOC和非IOC两种方式。下面我们来一一介绍:
1:间接访问(ActionContext)
Action运行期间所用到的数据都保存在ActionContext中,例如session会话,客户端提交的参数等信息。所以ActionContext是Action的一个上下文对象。
语法:ActionContext actionContext = ActionContext.getContext();
例子:
addBook.java:
package action;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class AddBook extends ActionSupport {
private String bookName;
private double bookPrice;
private String bookPress;
//省去get和set方法
@Override
public String execute() throws Exception {
ActionContext actionContext = ActionContext.getContext();
actionContext.getSession().put("bookName", this.bookName);
actionContext.getSession().put("bookPrice", this.bookPrice);
actionContext.getSession().put("bookPress", this.bookPress);
return SUCCESS;
}
}
------------------------------------------------------------------------------------------------------------------
addBook.jsp
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
</head>
<body>
<center>
<s:form action="addBook" method="post">
<s:textfield name="bookName" label="书名"></s:textfield>
<s:textfield name="bookPrice" label="价格"></s:textfield>
<s:textfield name="bookPress" label="出版社"></s:textfield>
<s:submit value="提交 " />
</s:form>
</center>
</body>
</html>
------------------------------------------------------------------------------------------------------------------
addSuccess.jsp
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
</head>
<body>
读取session中的内容,您添加的图书信息为:<br />
书名:<s:property value="#session.bookName" /><br />
价格:<s:property value="#session.bookPrice" /><br />
出版社:<s:property value="#session.bookPress" /><br />
</body>
</html>
------------------------------------------------------------------------------------------------------------------
2.直接访问
1>.IOC方式
在struts2框架中,通过IOC方式访问ServletAPI,就必须实现Action中相应的接口,这些接口是:
org.apache.struts2.util.ServletContextAware
org.apache.struts2.interceptor.ServletRequestAware
org.apache.struts2.interceptor.ServletResponseAware
org.apache.struts2.interceptor.ServletSessionAware
你可以实现其中一个接口,也可以全部都实现,根据具体情况而定。
例子:
IocAction.java:
package action;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.apache.struts2.interceptor.ServletRequestAware;
import com.opensymphony.xwork2.ActionSupport;
public class IocAction extends ActionSupport implements ServletRequestAware {
private String bookName;
private double bookPrice;
private String bookPress;
private HttpServletRequest request;
//省去get和set方法
public void setServletRequest(HttpServletRequest arg0) {
this.request=arg0;
}
@Override
public String execute() throws Exception {
HttpSession session = request.getSession();
session.setAttribute("bookName", this.bookName);
session.setAttribute("bookPrice", this.bookPrice);
session.setAttribute("bookPress", this.bookPress);
return SUCCESS;
}
}
-----------------------------------------------------------------------------------------------------------------
2>.非IOC方式(ServletActionContext)
在struts2中,提供了ServletActionContext类来获得ServletAPI,ServletActionContext类为我们提供了静态方法,直接调用即可:
例子:
NoloCAction.java
package action;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class NoloCAction extends ActionSupport {
private String bookName;
private double bookPrice;
private String bookPress;
//省去get和set方法
@Override
public String execute() throws Exception {
HttpServletRequest request=ServletActionContext.getRequest();
HttpSession session=request.getSession();
session.setAttribute("bookName", this.bookName);
session.setAttribute("bookPrice", this.bookPrice);
session.setAttribute("bookPress", this.bookPress);
return SUCCESS;
}
}
总结:Action对Servlet API的访问,分别为间接访问和直接访问(IOC和非IOC方式)。对于间接访问,只能得到request和session对象(Map类型的),不能得到response对象。对于IOC方式,使用起来比较麻烦,并且与Servlet API耦合大,在这里不推荐使用。对于非IOC方式,推荐使用,代码清晰而且又能满足要求。
struts2为我们提供了两种途径来访问ServletAPI,一种是间接访问;还有一种是直接访问。其中直接访问有区分为IOC和非IOC两种方式。下面我们来一一介绍:
1:间接访问(ActionContext)
Action运行期间所用到的数据都保存在ActionContext中,例如session会话,客户端提交的参数等信息。所以ActionContext是Action的一个上下文对象。
语法:ActionContext actionContext = ActionContext.getContext();
例子:
addBook.java:
package action;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class AddBook extends ActionSupport {
private String bookName;
private double bookPrice;
private String bookPress;
//省去get和set方法
@Override
public String execute() throws Exception {
ActionContext actionContext = ActionContext.getContext();
actionContext.getSession().put("bookName", this.bookName);
actionContext.getSession().put("bookPrice", this.bookPrice);
actionContext.getSession().put("bookPress", this.bookPress);
return SUCCESS;
}
}
------------------------------------------------------------------------------------------------------------------
addBook.jsp
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
</head>
<body>
<center>
<s:form action="addBook" method="post">
<s:textfield name="bookName" label="书名"></s:textfield>
<s:textfield name="bookPrice" label="价格"></s:textfield>
<s:textfield name="bookPress" label="出版社"></s:textfield>
<s:submit value="提交 " />
</s:form>
</center>
</body>
</html>
------------------------------------------------------------------------------------------------------------------
addSuccess.jsp
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
</head>
<body>
读取session中的内容,您添加的图书信息为:<br />
书名:<s:property value="#session.bookName" /><br />
价格:<s:property value="#session.bookPrice" /><br />
出版社:<s:property value="#session.bookPress" /><br />
</body>
</html>
------------------------------------------------------------------------------------------------------------------
2.直接访问
1>.IOC方式
在struts2框架中,通过IOC方式访问ServletAPI,就必须实现Action中相应的接口,这些接口是:
org.apache.struts2.util.ServletContextAware
org.apache.struts2.interceptor.ServletRequestAware
org.apache.struts2.interceptor.ServletResponseAware
org.apache.struts2.interceptor.ServletSessionAware
你可以实现其中一个接口,也可以全部都实现,根据具体情况而定。
例子:
IocAction.java:
package action;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.apache.struts2.interceptor.ServletRequestAware;
import com.opensymphony.xwork2.ActionSupport;
public class IocAction extends ActionSupport implements ServletRequestAware {
private String bookName;
private double bookPrice;
private String bookPress;
private HttpServletRequest request;
//省去get和set方法
public void setServletRequest(HttpServletRequest arg0) {
this.request=arg0;
}
@Override
public String execute() throws Exception {
HttpSession session = request.getSession();
session.setAttribute("bookName", this.bookName);
session.setAttribute("bookPrice", this.bookPrice);
session.setAttribute("bookPress", this.bookPress);
return SUCCESS;
}
}
-----------------------------------------------------------------------------------------------------------------
2>.非IOC方式(ServletActionContext)
在struts2中,提供了ServletActionContext类来获得ServletAPI,ServletActionContext类为我们提供了静态方法,直接调用即可:
例子:
NoloCAction.java
package action;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class NoloCAction extends ActionSupport {
private String bookName;
private double bookPrice;
private String bookPress;
//省去get和set方法
@Override
public String execute() throws Exception {
HttpServletRequest request=ServletActionContext.getRequest();
HttpSession session=request.getSession();
session.setAttribute("bookName", this.bookName);
session.setAttribute("bookPrice", this.bookPrice);
session.setAttribute("bookPress", this.bookPress);
return SUCCESS;
}
}
总结:Action对Servlet API的访问,分别为间接访问和直接访问(IOC和非IOC方式)。对于间接访问,只能得到request和session对象(Map类型的),不能得到response对象。对于IOC方式,使用起来比较麻烦,并且与Servlet API耦合大,在这里不推荐使用。对于非IOC方式,推荐使用,代码清晰而且又能满足要求。
相关文章推荐
- Struts2-学习笔记系列(4)-访问servlet api
- Struts2学习笔记之域属性的添加和访问
- Struts2 学习笔记 09 访问Web元素
- Struts2.x学习三(Action中访问Servlet API )
- Struts2学习笔记三——Namespace与访问路径
- 【Struts2学习笔记(8)】访问或添加request/session/application属性获取HttpServletRequest / HttpSession / ServletContex
- [原创]java WEB学习笔记69:Struts2 学习之路-- 消息处理与国际化,概述,配置国际资源文件,访问国际化消息,通过超链接切换语言
- Struts2_学习笔记(五)----访问Web元素、include、默认Action、Action总结
- 【Struts2学习笔记(10)】自定义拦截器管理权限访问
- Struts2学习笔记之域属性的添加和访问
- struts2学习笔记之六(Action访问servlet的API以及复制项目小技巧)
- struts2 笔记01 登录、常用配置参数、Action访问Servlet API 和设置Action中对象的值、命名空间和乱码处理、Action中包含多个方法如何调用
- [原创]java WEB学习笔记55:Struts2学习之路---详解struts2 中 Action,如何访问web 资源,解耦方式(使用 ActionContext,实现 XxxAware 接口),耦合方式(通过ServletActionContext,通过实现 ServletRequestAware, ServletContextAware 等接口的方式)
- Struts2学习笔记一
- 汇编学习笔记---3内存访问
- Python访问数据库——学习笔记
- struts2学习笔记二--准备struts2的学习和开发环境
- struts2学习笔记
- [原创]java WEB学习笔记54:Struts2学习之路---概述,环境的搭建
- 学习笔记2:Struts2 +Spring JDBC