zoukankan      html  css  js  c++  java
  • struts2在action中获取request、session、application,并传递数据

    假设仅仅是通过request、session、application传递数据,则不须要获取对应的对象也能够传递数据,代码例如以下:

    ScopeAction.java:

    package com.itheima.action;
    
    import com.opensymphony.xwork2.ActionContext;
    
    public class ScopeAction {
    
    	public String execute() {
    		/*
    		 * 我们这里不通过字段(加入setXXX、getXXX方法)传递数据,我们想着通过域(request、session、application)
    		 * 传递数据。怎么样才干做到呢?struts2採用低侵入式,action与servlet没有不论什么关联,怎么才干获取域并传递数据呢,
    		 * 这时就用到了ActionContext类
    		 */
    		ActionContext context = ActionContext.getContext();
    		context.getApplication().put("application", "application域");//通过ServletContext域传递数据
    		context.getSession().put("session", "session域");//通过Session域传递数据
    		context.put("request", "request域");//通过Request域传递数据
    		return "success";
    	}
    }
    
    struts2.xml:

    <action name="scopeAction_*" class="com.itheima.action.ScopeAction" method="{1}">
         <result name="success">/msg.jsp</result>
    </action>
    
    msg.jsp:

    <%@ page language="java" contentType="text/html; charset=utf-8"
        pageEncoding="utf-8"%>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <title>Insert title here</title>
    </head>
    <body>
    	${applicationScope.application }<br/>
    	${sessionScope.session }<br/>
    	${requestScope.request }<br/>
    </body>
    </html>
    地址栏:


    =============================================================================================

    上述方式没有获取到域对象。这里我们也能够通过其它方法获取域对象:

    在上述的ScopeAction中加入例如以下方法:

    public String doexe() {
    		HttpServletRequest request = ServletActionContext.getRequest();
    		request.setAttribute("request", "请求域");
    		ServletContext context = ServletActionContext.getServletContext();
    		context.setAttribute("application", "应用域");
    		HttpSession session = request.getSession();
    		session.setAttribute("session", "会话域");
    //		HttpServletResponse response = ServletActionContext.getResponse();
    		return "success";
    	}

    其它和上述不变。然后地址栏中输入:

    ===============================================================================================

    另外另一种方式就是通过框架注入:

    Scope2Action.java:

    package com.itheima.action;
    
    import javax.servlet.ServletContext;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    import org.apache.struts2.interceptor.ServletRequestAware;
    import org.apache.struts2.interceptor.ServletResponseAware;
    import org.apache.struts2.util.ServletContextAware;
    
    public class Scope2Action implements ServletRequestAware, ServletResponseAware, ServletContextAware{
    
    	private HttpServletRequest request;
    	private HttpServletResponse response;
    	private ServletContext servletContext;
    	@Override
    	public void setServletContext(ServletContext arg0) {
    		this.servletContext = arg0;
    	}
    
    	@Override
    	public void setServletResponse(HttpServletResponse arg0) {
    		this.response = arg0;
    	}
    
    	@Override
    	public void setServletRequest(HttpServletRequest arg0) {
    		this.request = arg0;
    	}
    
    	public String execute() {
    		request.setAttribute("request", "请 求 域");
    		request.getSession().setAttribute("session", "会 话 域");
    		servletContext.setAttribute("application", "应 用 域");
    		return "success";
    	}
    	
    }
    
    struts2.xml:

    <action name="scope2Action" class="com.itheima.action.Scope2Action">
    	<result name="success">/msg.jsp</result>
    </action>



  • 相关阅读:
    git apply、git am打补丁.diff 和 .patch【转】
    RK3288 GPIO 输出问题【转】
    [RK3288][Android6.0] 调试笔记 --- 通用GPIO驱动控制LED【转】
    [RK3288][Android6.0] 调试笔记 --- 系统识别不同硬件版本方法【转】
    Android驱动开发之earlysuspend睡眠模式--实现代码【转】
    触摸屏唤醒实现【转】
    强加密RNGCryptoServiceProvider
    java中有类似C#里ref或out的功能吗?
    mysql中char与varchar的区别分析
    JVM再了解了解
  • 原文地址:https://www.cnblogs.com/blfbuaa/p/6905403.html
Copyright © 2011-2022 走看看