Struts2中访问web元素的四种方式及前台jsp页面获取后台值的方式

Advertisement

Struts2中访问web元素的四种方式(访问requst、session、application)

?

四种方式:

1. 通过ActionContext来访问request,session,application对象

2. 通过实现RequestAware、SessionAware、ApplicationAware接口来访问request,session,application对象

3. 通过ServletActionContext来访问request,session,application对象

4. 通过实现ServletRequestAware接口来访问request,session,application对象

?

演示代码:

方式一:

view plaincopy to clipboardprint?

public class UserAction1 extends ActionSupport{

private static final long serialVersionUID = 1L;

private Map <String,Object>request;

private Map <String,Object>session;

private Map <String,Object>application;

@SuppressWarnings("unchecked")
@Override?
public String execute(){
System.out.println("通过ActionContext来访问request,session,application对象");
// 初始化
request = (Map<String,Object>)ActionContext.getContext().get("request");
session = ActionContext.getContext().getSession();
application = ActionContext.getContext().getApplication();
// 赋值
request.put("requestKey", "requestValue");
session.put("sessionKey", "sessionValue");
application.put("applicationKey", "applicationValue");
return "success";
}
}?

public class UserAction1 extends ActionSupport{

private static final long serialVersionUID = 1L;

private Map <String,Object>request;

private Map <String,Object>session;

private Map <String,Object>application;

@SuppressWarnings("unchecked")
@Override
public String execute(){
System.out.println("通过ActionContext来访问request,session,application对象");
// 初始化
request = (Map<String,Object>)ActionContext.getContext().get("request");
session = ActionContext.getContext().getSession();
application = ActionContext.getContext().getApplication();
// 赋值
request.put("requestKey", "requestValue");
session.put("sessionKey", "sessionValue");
application.put("applicationKey", "applicationValue");
return "success";
}
}

方式二:

view plaincopy to clipboardprint?

public class UserAction2 extends ActionSupport implements RequestAware,SessionAware,ApplicationAware{

private static final long serialVersionUID = 1L;

private Map <String,Object>request;

private Map <String,Object>session;

private Map <String,Object>application;

@SuppressWarnings("unchecked")
@Override?
public String execute(){
System.out.println("通过实现RequestAware、SessionAware、ApplicationAware接口来访问request,session,application对象");
// 赋值
request.put("requestKey", "requestValue");
session.put("sessionKey", "sessionValue");
application.put("applicationKey", "applicationValue");
return "success";
}

@Override?
public void setRequest(Map<String, Object> request) {
this.request = request;
}

@Override?
public void setApplication(Map<String, Object> application) {
this.application = application;
}

@Override?
public void setSession(Map<String, Object> session) {
this.session = session;
}
}?

public class UserAction2 extends ActionSupport implements RequestAware,SessionAware,ApplicationAware{

private static final long serialVersionUID = 1L;

private Map <String,Object>request;

private Map <String,Object>session;

private Map <String,Object>application;

@SuppressWarnings("unchecked")
@Override
public String execute(){
System.out.println("通过实现RequestAware、SessionAware、ApplicationAware接口来访问request,session,application对象");
// 赋值
request.put("requestKey", "requestValue");
session.put("sessionKey", "sessionValue");
application.put("applicationKey", "applicationValue");
return "success";
}

@Override
public void setRequest(Map<String, Object> request) {
this.request = request;
}

@Override
public void setApplication(Map<String, Object> application) {
this.application = application;
}

@Override
public void setSession(Map<String, Object> session) {
this.session = session;
}
}

方式三:

view plaincopy to clipboardprint?

public class UserAction3 extends ActionSupport{

private static final long serialVersionUID = 1L;

private HttpServletRequest request;

private HttpSession session;

private ServletContext application;

@SuppressWarnings("unchecked")
@Override?
public String execute(){
System.out.println("通过ServletActionContext来访问request,session,application对象");
// 初始化
request = ServletActionContext.getRequest();
session = request.getSession();
application = session.getServletContext();
// 赋值
request.setAttribute("requestKey", "requestValue");
session.setAttribute("sessionKey", "sessionValue");
application.setAttribute("applicationKey", "applicationValue");
return "success";
}
}?

public class UserAction3 extends ActionSupport{

private static final long serialVersionUID = 1L;

private HttpServletRequest request;

private HttpSession session;

private ServletContext application;

@SuppressWarnings("unchecked")
@Override
public String execute(){
System.out.println("通过ServletActionContext来访问request,session,application对象");
// 初始化
request = ServletActionContext.getRequest();
session = request.getSession();
application = session.getServletContext();
// 赋值
request.setAttribute("requestKey", "requestValue");
session.setAttribute("sessionKey", "sessionValue");
application.setAttribute("applicationKey", "applicationValue");
return "success";
}
}

方式四:

view plaincopy to clipboardprint?

public class UserAction4 extends ActionSupport implements ServletRequestAware{

private static final long serialVersionUID = 1L;

private HttpServletRequest request;

private HttpSession session;

private ServletContext application;

@SuppressWarnings("unchecked")
@Override?
public String execute(){
Syste

Similar Posts:

  • STRUTS2 ACTION 访问WEB元素的四种方式

    1: Map IoC  方式 package com.action; 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.Ac

  • 教学笔记-Struts2中访问web元素

    访问Web元素:request,session,application即对应的类HttpServletRequest,HttpSession,ServletContext *方式一: 依赖于容器获取Map类型的request,session,application <>在action中定义:private Map request; private Map session; private Map application; <>可以在构造器里面或者其他地方用一下方法获取这三个元素:

  • Struts2学习笔记-访问web元素的四种方式

    1. 通过ActionContext来访问Map类型的request.session.application对象 private Map<String,Object> request; request=(Map<String,Object>)ActionContext.getContext().get("request"); private Map<String,Object> session; session=ActionContext.getCo

  • Struts2 Action中访问Servlet API的四种方法

    题前语:在一个Web应用中,通常需要访问的Servlet API主要是HttpServletRequest.HttpSession.ServletContext这3个类,分别与JSP内置对象中的request.session.application相对应. 众所周知,在Struts2中,Struts2对Servlet的API进行了封装,以便使Struts2的Action与Servlet API解耦,使程序更易扩展.但是,在某些特定时候,我们却又不得不停下脚步,需要与Servlet API交流,这

  • Struts2在Action类中获得HttpServletResponse对象的四种方法

    Struts2在Action类中获得HttpServletResponse对象的四种方法[转] 在struts1.x Action类的execute方法中,有四个参数,其中两个就是response和request.而在Struts2中,并没有任何参数,因此,就不能简单地从execute方法获得HttpServletResponse或HttpServletRequest对象了. 但在Struts2 Action类中仍然有很多方法可以获得这些对象.下面就列出四种获得这些对象的方法. [方法1]使用S

  • 访问Web元素

    访问Web元素可以有四种方法,下面依次列出 user_login_success.jsp <%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <%@taglib uri="/struts-tags" prefix="s"%> <!DOCTYPE html P

  • 在ASP.NET AJAX 1.0框架中访问Web服务

    一. 简介 如今,微软最新推出的AJAX框架为ASP.NET AJAX 1.0(下载地址为[url]http://ajax.asp.net/downloads/default.aspx[/url],由ASP.NET 2.0 AJAX Extensions 1.0和ASP.NET AJAX Control Toolkit两部分组成).这个框架具有如下优点:  丰富的Web用户体验框架:  与.NET编程模型无缝地集成到一起:  开发者可以自由选择应用程序模型以服务器端为中心还是以客户端为中心

  • 如何在网页中访问WEB数据库

    随着计算机技术的发展,WEB得到了越来越广泛的应用.WWW页面已经由静态网页逐渐发展为动态的交互式的网页,WWW网页与数据库的连接也显得越来越重要.在WWW诞生的初期,就可以通过通用网关接口(CGI)来访问数据库,后来许多厂商不断开发出新的接口技术,使得访问WEB数据库更加简洁.方便和高效.本文将综合介绍几种在WWW中访问数据的方法,重点介绍比较新的ASP技术和IDC技术,对JDBC和CGI将仅做简单介绍.假设数据库服务器的操作系统为Windows NT Server 4.0,数据库是Micro

  • 【转】Struts2中访问HttpServletRequest和HttpSession

    FROM : http://pengfeng.iteye.com/blog/212358 在没有使用Struts2之前,都习惯使用HttpServletRequest和HttpSession对象来操作相关参数,下面介绍一下在Struts2中访问隐藏的HttpServletRequest和HttpSession的两种方法: 1.非Ioc方式 这种方式主要是利用了com.opensymphony.xwork2.ActionContext类以及ora.apache.struts2.ServletAct

  • C#中方法的参数的四种类型

    C#中方法的参数有四种类型: 1. 值参数类型 (不加任何修饰符,是默认的类型) 2. 引用型参数 (以ref 修饰符声明) 3. 输出型参数 (以out 修饰符声明) 4. 数组型参数 (以params 修饰符声明) =================================================== 1. 值传递: 值类型是方法默认的参数类型,采用的是值拷贝的方式.也就是说,如果使用的是值类型,则可以在方法中更改该值,但当控制传递回调用过程时,不会保留更改的值.使用值类型的