Struts2里如何取得request,session,application

第一种:
取得MAP类型的request,session,application
在java文件里写

package com.xjtu.st;
 
import java.util.Map;
 
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
 
public class LoginAction extends ActionSupport{
    private Map request;
    private Map session;
    private Map application;
    public LoginAction(){
        request = (Map)ActionContext.getContext().get("request");
        session = ActionContext.getContext().getSession();
        application = ActionContext.getContext().getApplication();
    }
     
    public String execute(){
        request.put("rs", "rs");
        session.put("ss", "ss");
        application.put("as", "as");
         
        return SUCCESS;
    }
}

 ActionContext.getContext()是获得Action的上下文,然后可以获得MAP类型的request等
在输出页面可获得参数

<%@ page language="java" import="java.util.*" pageEncoding="GBK"%>
<%@ taglib uri="/struts-tags" prefix="s" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
 
 
 
   
     
     
    <title>My JSP ‘MyJsp.<a href="http://www.2cto.com/kf/web/jsp/" target="_blank" class="keylink">jsp</a>‘ starting page</title>
     
    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">    
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">
    <!--
    <link rel="stylesheet" type="text/css" href="styles.css">
    -->
 
   
   
   
   hello  <br>
 <s:property value="#request.rs"> <br><!--  //通过标签取值-->
   <s:property value="#session.ss"> <br>
   <s:property value="#application.as"> <br>
   <%=request.getAttribute("rs") %><br><!-- 用原始的方法取值-->
     <%=session.getAttribute("ss") %><br>
       <%=application.getAttribute("as") %><br>
    
   
<!--html>
</s:property></s:property></s:property>

 第二种:
实现RequestAware,SessionAware,ApplicationAware接口,该方法成为DI(依赖注入)或者IOC(控制反转)。代码如下

package com.xjtu.st;
 
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.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
 
public class LoginAction extends ActionSupport implements RequestAware,SessionAware,ApplicationAware{
    private Map request;
    private Map session;
    private Map application;
     
     
    public String execute(){
        request.put("rs", "rrs");
        session.put("ss", "sss");
        application.put("as", "aas");
         
        return SUCCESS;
    }
 
    public void setRequest(Map<string, object=""> request) {
        // TODO Auto-generated method stub
        this.request=request;
         
    }
 
    public void setSession(Map<string, object=""> session) {
        // TODO Auto-generated method stub
        this.session= session;
    }
 
    public void setApplication(Map<string, object=""> application) {
        // TODO Auto-generated method stub
        this.application = application;
    }
}
</string,></string,></string,>

 前面2种是取得Map类型的,后两种是取得HtteServletRequest,HttpSession,ServletContext类型的办法第三种:

代码如下

package com.xjtu.st;
 
import java.util.Map;
 
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
 
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.ApplicationAware;
import org.apache.struts2.interceptor.RequestAware;
import org.apache.struts2.interceptor.SessionAware;
 
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
 
public class LoginAction extends ActionSupport {
    private HttpServletRequest request;
    private HttpSession session;
    private ServletContext application;
     
    public LoginAction(){
        request= ServletActionContext.getRequest();
        session = request.getSession();
        application = session.getServletContext();
    }
    public String execute(){
        request.setAttribute("rs", "rrrs");
        session.setAttribute("ss", "ssss");
        application.setAttribute("as", "aaas");
         
        return SUCCESS;
    }
 
 
 
     
}

 

 
 
 
该方法不常用第四种:
也是ioc的方式
代码如下
 
package com.xjtu.st;
 
import java.util.Map;
 
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
 
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.ApplicationAware;
import org.apache.struts2.interceptor.RequestAware;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.SessionAware;
 
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
 
public class LoginAction extends ActionSupport implements ServletRequestAware{
    private HttpServletRequest request;
    private HttpSession session;
    private ServletContext application;
     
 
    public String execute(){
        request.setAttribute("rs", "rrrrs");
        session.setAttribute("ss", "sssss");
        application.setAttribute("as", "aaaas");
         
        return SUCCESS;
    }
    public void setServletRequest(HttpServletRequest request) {
        // TODO Auto-generated method stub
        this.request = request;
        session = request.getSession();
        application = session.getServletContext();
         
    }
      
}

 

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。