Action中取得request,session的四种方式
<!DOCTYPE struts PUBLIC
??? "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
??? "http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
??????? <action name="login" >result.jsp</result>
??????? </action>
???????
??????? <action name="login2" >result.jsp</result>
??????? </action>
???????
??????? <action name="login3" >result.jsp</result>
??????? </action>
???????
??????? <action name="login4" >result.jsp</result>
??????? </action>
??? </package>
???
</struts>
其中的LoginAction,Login2Action,Login3Action,Login4Action分别对应四种方式的Action。
文件名:result.jsp
文件内容:
<%@ page contentType="text/html; charset=gb2312" %>
<%@ taglib uri="/struts-tags" prefix="s"%>
<html>
request 属性是 ${requestScope.attribute}<br>
session 属性是 ${sessionScope.attribute}<br>
application 属性是 ${applicationScope.attribute}<br>
</html>
文件名:getrequest.jsp
文件内容:
<%@ page contentType="text/html; charset=gb2312" %>
<%@ taglib uri="/struts-tags" prefix="s"%>
<html>
<a href="login.action">servlet 不相关的 非IoC 取得Request等对象的方式</a><br>
<a href="login2.action">servlet 不相关的 IoC 取得Request等对象的方式</a><br>
<a href="login3.action">servlet 相关的 非IoC 取得Request等对象的方式</a><br>
<a href="login4.action">servlet 相关的 IoC 取得Request等对象的方式</a><br>
</form>
</html>
然后分别叙述4种方式,
1,servlet 不相关的 非IoC 取得Request等对象的方式
这种方法先取得ActionContext,然后获取Map类型的request等对象
Action文件:
package struts2.login;
import java.util.Map;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class LoginAction? extends ActionSupport{
??? private ActionContext context;
??? private Map request;
??? private Map session;
??? private Map application;
???
??? @Override
??? public String execute() throws Exception {
??? ??? // TODO Auto-generated method stub
??? ??? this.context = ActionContext.getContext();
??? ??? this.request = (Map)this.context.get("request");
??? ??? this.session = this.context.getSession();
??? ??? this.application = this.context.getApplication();
??? ??? this.request.put("attribute", "request value servlet 不相关的 非IoC ");
??? ??? this.session.put("attribute", "session value servlet 不相关的 非IoC ");
??? ??? this.application.put("attribute", "application value servlet 不相关的 非IoC ");
??? ??? return SUCCESS;
??? }
}
2,servlet 不相关的 IoC 取得Request等对象的方式
这种方法,是实现特定的接口,由container来设定request等对象。请注意如下代码示例中红色文字的部分。
Action文件:
package struts2.login;
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 Login2Action? extends ActionSupport
??? implements RequestAware , SessionAware, ApplicationAware{
??? private Map request;
??? private Map session;
??? private Map application;
???
??? @Override
??? public String execute() throws Exception {
??? ??? // TODO Auto-generated method stub
??? ??? this.request.put("attribute", "request value servlet 不相关的 IoC ");
??? ??? this.session.put("attribute", "session value servlet 不相关的 IoC ");
??? ??? this.application.put("attribute", "application value servlet 不相关的 IoC ");
??? ??? return SUCCESS;
??? }
??? @Override
??? public void setRequest(Map<String, Object> arg0) {
??? ??? // TODO Auto-generated method stub
??? ??? this.request = arg0;
??? }
??? @Override
??? public void setSession(Map<String, Object> arg0) {
??? ??? // TODO Auto-generated method stub
??? ??? this.session = arg0;
??? }
??? @Override
??? public void setApplication(Map<String, Object> arg0) {
??? ??? // TODO Auto-generated method stub
??? ??? this.application = arg0;
??? }
}
3,servlet 相关的 非IoC 取得Request等对象的方式
这种方法可以获取servlet相关的request等对象,也就是说这种方式取出的request对象不单单是设定属性的作用,而是可以获取http相关的信息。
取出方法,请参看代码中的蓝字部分。
Action文件:
package struts2.login;
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 Login3Action? extends ActionSupport{
??? private HttpServletRequest request;
??? private HttpSession session;
??? private ServletContext application;
???
??? @Override
??? public String execute() throws Exception {
??? ??? // TODO Auto-generated method stub
??? ??? this.request = ServletActionContext.getRequest();
??? ??? this.session = this.request.getSession();
??? ??? this.application = ServletActionContext.getServletContext();
??? ??? this.request.setAttribute("attribute", "request value servlet 相关的 非IoC ");
??? ??? this.session.setAttribute("attribute", "session value servlet 相关的 非IoC ");
??? ??? this.application.setAttribute("attribute", "application value servlet 相关的 非IoC ");
??? ??? return SUCCESS;
??? }
???
}
4,servlet 相关的 IoC 取得Request等对象的方式
这种方法也可以获取servlet相关的request等对象,也就是说这种方式取出的request对象不单单是设定属性的作用,而是可以获取http相关的信息。
但是取出方法是通过接口进行实现,也就是由struts2的container来设定,请参看代码中的红字部分。
Action文件:
package struts2.login;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.util.ServletContextAware;
import com.opensymphony.xwork2.ActionSupport;
public class Login4Action? extends ActionSupport
??? implements ServletRequestAware , ServletContextAware{
??? private HttpServletRequest request;
??? private HttpSession session;
??? private ServletContext application;
???
??? @Override
??? public String execute() throws Exception {
??? ??? // TODO Auto-generated method stub
??? ??? this.request.setAttribute("attribute", "request value servlet 相关的 IoC ");
??? ??? this.session = this.request.getSession();
??? ??? this.session.setAttribute("attribute", "session value servlet 相关的 IoC ");
??? ??? this.application.setAttribute("attribute", "application value servlet 相关的 IoC ");
??? ??? return SUCCESS;
??? }
??? @Override
??? public void setServletRequest(HttpServletRequest arg0) {
??? ??? // TODO Auto-generated method stub
??? ??? this.request = arg0;
??? }
??? @Override
??? public void setServletContext(ServletContext arg0) {
??? ??? // TODO Auto-generated method stub
??? ??? this.application = arg0;
??? }
}
好,把执行结果的图上来一看,一目了然,不知道你对action中取request等对象的这几种方式,了解了多少呢?
转自:http://www.cnblogs.com/lxJack/archive/2011/04/03/2004782.html