Struts2获取web对象的4种方式

Struts2获取web对象的4种方式

Struts2是如何获取web中 request、session、application的了,Struts2有4种方式获取web对象

1.通过ServletActionContext取HttpServletRequest对象

2.通过ServletRequestAware,(ServletResponseAware )来注入HttpServletRequest、(HttpServletResponse)对象

3.通过ActionContext取Map

4.通过RequestAware,SessionAware,ApplicationAware接口,来注入  三个Map

下面的例子就用以上4种方式来实现如何获取web对象

首先建立一个实体类,接收从客户端传过来的参数,通过ModelDriven,如果对传递参数不明白的可以参看我上篇文章三种对象传参和ModelDriven的原理

一个Student实体类

package com.yc.web.actions;

import java.io.Serializable;

public class Student implements Serializable {
	private static final long serialVersionUID = -6646865822958505769L;
	private String name;
	private Integer age;
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public Integer getAge() {
		return age;
	}
	public void setAge(Integer age) {
		this.age = age;
	}
}

一、通过ServletActionContext取HttpServletRequest对象

package com.yc.web.actions;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

public class StudentAction extends ActionSupport implements ModelDriven<Student>{
	private static final long serialVersionUID = 1213446993015542840L;
	private Student student;
	private HttpServletRequest request;
	private HttpSession session;
	private ServletContext application;
	private HttpServletResponse response;

	public  StudentAction() {
		//获取web对象
		request=ServletActionContext.getRequest();
		session=request.getSession();
		application=session.getServletContext();
	}
	public String execute(){
		//赋值
		request.setAttribute("student", student);
		session.setAttribute("student", student);
		application.setAttribute("student", student);
		return Action.SUCCESS;
	}
	@Override
	public Student getModel() {
		student =new Student();
		return student;
	}
}

二、通过ServletRequestAware(ServletResponseAware )来注入HttpServletRequest(HttpServletResponse)对象

package com.yc.web.actions;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;

import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

public class StudentAction2 extends ActionSupport implements
ModelDriven<Student> ,ServletRequestAware,ServletResponseAware{
	private static final long serialVersionUID = 1213446993015542840L;
	private Student student;
	private HttpServletRequest request;
	private HttpSession session;
	private ServletContext application;
	private HttpServletResponse response;

	@Override
	public Student getModel() {
		student =new Student();
		return student;
	}
	@Override
	public void setServletResponse(HttpServletResponse arg0) {
		//获取response对象
		response=arg0;
	}

	@Override
	public void setServletRequest(HttpServletRequest arg0) {
		//获取web对象
		request=arg0;
		session=request.getSession();
		application=session.getServletContext();
	}

	public String execute(){
		//赋值
		request.setAttribute("student", student);
		session.setAttribute("student", student);
		application.setAttribute("student", student);
		return Action.SUCCESS;

	}
}

三、通过ActionContext取Map

package com.yc.web.actions;

import java.util.Map;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;

import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

public class StudentAction3 extends ActionSupport implements ModelDriven<Student>{
	private static final long serialVersionUID = 1213446993015542840L;
	private Student student;
	private Map<String ,Object> request;
	private Map<String ,Object> session;
	private Map<String ,Object> application;
	public  StudentAction3() {
		//获取web对象
		request=(Map<String, Object>) ActionContext.getContext().get("request");
		session=ActionContext.getContext().getSession();
		application=ActionContext.getContext().getApplication();
	}
	public String execute(){
		//赋值
		request.put("student", student);
		session.put("student", student);
		application.put("student", student);
		return Action.SUCCESS;

	}
	@Override
	public Student getModel() {
		student =new Student();
		return student;
	}
}

四、通过RequestAware,SessionAware,ApplicationAware接口,来注入  三个Map

package com.yc.web.actions;

import java.util.Map;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
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.ServletResponseAware;
import org.apache.struts2.interceptor.SessionAware;

import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

import freemarker.template.utility.Execute;

public class StudentAction4 extends ActionSupport implements
ModelDriven<Student> ,RequestAware,SessionAware,ApplicationAware{
	private static final long serialVersionUID = 1213446993015542840L;
	private Student student;
	private Map<String ,Object> request;
	private Map<String ,Object> session;
	private Map<String ,Object> application;

	public String execute(){
		//赋值
		request.put("student", student);
		session.put("student", student);
		application.put("student", student);
		return Action.SUCCESS;

	}

	@Override
	public Student getModel() {
		student =new Student();
		return student;
	}
	//获取web对象
	@Override
	public void setApplication(Map<String, Object> arg0) {
		application=arg0;
	}
	@Override
	public void setSession(Map<String, Object> arg0) {
		session=arg0;

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

下面是访问页面 index,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>

</body>
	<a href="student.action?name=tg&age=20">学生 </a>
	<a href="student2.action?name=tg&age=20">学生 </a>
	<a href="student3.action?name=tg&age=20">学生 </a>
	<a href="student4.action?name=tg&age=20">学生 </a>
</html>

下面struts.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
	"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
	"http://struts.apache.org/dtds/struts-2.3.dtd">

<struts>

    <constant name="struts.enable.DynamicMethodInvocation" value="false" />
    <constant name="struts.devMode" value="true" />
    <constant name="struts.action.extension" value="action,do,,"/>

    <package name="default" namespace="/" extends="struts-default">

      <action name="student" class="com.yc.web.actions.StudentAction">
      	<result name="success">/show.jsp</result>
      </action>

       <action name="student2" class="com.yc.web.actions.StudentAction2">
      	<result name="success">/show.jsp</result>
      </action>

       <action name="student3" class="com.yc.web.actions.StudentAction3">
      	<result name="success">/show.jsp</result>
      </action>

       <action name="student4" class="com.yc.web.actions.StudentAction4">
      	<result name="success">/show.jsp</result>
      </action>

      <action name="student4" class="com.yc.web.actions.StudentAction4">
      	<result name="success">/show.jsp</result>
      </action>
    </package>

</struts>

访问成功显示页面show.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>
	${requestScope.student.name}   -${requestScope.student.age}  <br/>
	${sessionScope.student.name}   -${sessionScope.student.age} <br/>
	${applicationScope.student.name} -${applicationScope.student.age}  <br/>
</body>
</html>
时间: 2024-11-08 22:15:18

Struts2获取web对象的4种方式的相关文章

JS获取html对象的几种方式介绍

 这篇文章主要介绍了JS获取html对象的几种方式,有需要的朋友可以参考一下 document.getElementById("zx");   通过ID获取html元素对象,ID号在html文档当中应该是唯一的.返回的是唯一element对象.并且所有浏览器都兼容.   document.getElementsByTagName("span")[0];   通过标签查找html对象,由于html标签在一个页面中可能重复很多次,所以当前页面返回的是一个数组.可以根据标

JS获取html对象的几种方式介绍_javascript技巧

document.getElementById("zx"); 通过ID获取html元素对象,ID号在html文档当中应该是唯一的.返回的是唯一element对象.并且所有浏览器都兼容. document.getElementsByTagName("span")[0]; 通过标签查找html对象,由于html标签在一个页面中可能重复很多次,所以当前页面返回的是一个数组.可以根据标签出现的位置定位元素的对象.所有浏览器都兼容. document.getElementsB

javaScript中定义类或对象的五种方式总结_javascript技巧

第一种方式: 工厂方法 能创建并返回特定类型的对象的工厂函数(factory function). function createCar(sColor){ var oTempCar = new Object; oTempCar.color = sColor; oTempCar.showColor = function (){ alert(this.color); }; return oTempCar; } var oCar1 = createCar(); var oCar2 = createCa

详解springmvc 接收json对象的两种方式_javascript技巧

最近学习了springmvc 接收json对象的两种方式,现在整理出来,具体如下:  1.以实体类方式接收 前端 ajax 提交数据: function fAddObj() { var obj = {}; obj['objname'] = "obj"; obj['pid'] = 1 ; $.ajax({ url: 'admin/Obj/addObj.do', method: 'post', contentType: 'application/json', // 这句不加出现415错误:

Action获取请求参数的三种方式_Android

方式一:Action本身作为Model对象,通过属性注入(Setter)方法讲参数数据封装到Action中 具体为:在Action中,提供和参数名相同的几个属性,并为其提供set方法,那么,该参数会被自动封装 到该几个属性中. 方式二:创建独立的Model对象,还是通过属性注入的方法将请求数据封装到Model对象中 具体为:另外创建一个专门的类,并在其中添加几个和请求参数名相同的属性,,此时,在表单中,需要讲参数名, 写成Model的一个具体对象名.参数名的方式,然后在Action中,需要定义一

Action获取请求参数的三种方式

方式一:Action本身作为Model对象,通过属性注入(Setter)方法讲参数数据封装到Action中 具体为:在Action中,提供和参数名相同的几个属性,并为其提供set方法,那么,该参数会被自动封装 到该几个属性中. 方式二:创建独立的Model对象,还是通过属性注入的方法将请求数据封装到Model对象中 具体为:另外创建一个专门的类,并在其中添加几个和请求参数名相同的属性,,此时,在表单中,需要讲参数名, 写成Model的一个具体对象名.参数名的方式,然后在Action中,需要定义一

javascript面向对象——定义类或对象的几种方式

从语言的视角看,面向对象的编程和面向对象的javascript绝对不是什么现代的东西:从刚开始javascript就被全面的设计为面向对象的语言 下面呢,就介绍一下,定义类或对象的几种方式: 1 工厂模式 代码如下 function createcar(iColor,iDoors,iMpg) { varoTemCar = new Object; oTemperCar.color = iColor; oTemperCar.doors = iDoors; oTempCar.mpg = iMpg; o

WPF:将HTML RGB颜色值转化为Color对象的两种方式

原文:WPF:将HTML RGB颜色值转化为Color对象的两种方式  (1)方式一: Color color1 = (Color)System.Windows.Media.ColorConverter.ConvertFromString("#E0E0E0"); (2)方式二: Color color2 = ConvertToColor("#E0E0E0"); public static System.Windows.Media.Color ConvertToCol

javascript获取表单值的7种方式

见代码: <!doctype html> <html lang="en"> <head> <meta charset="UTF-8"> <title>表单对象--获取表单值的7种方式</title> </head> <body> <form action="" name="myform"> <input type=