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>




目录
相关文章
|
3月前
|
前端开发 开发者
new操作符背后的秘密:揭开Web前端对象创建的神秘面纱!
【8月更文挑战第23天】在Web前端开发中,`new`操作符是创建对象实例的核心。本文以`Person`构造函数为例,通过四个步骤解析`new`操作符的工作原理:创建空对象、设置新对象原型、执行构造函数并调整`this`指向、判断并返回最终对象。了解这些有助于开发者更好地理解对象实例化过程,从而编写出更规范、易维护的代码。
40 0
|
3月前
|
XML JavaScript 测试技术
Web自动化测试框架(基础篇)--HTML页面元素和DOM对象
本文为Web自动化测试入门指南,介绍了HTML页面元素和DOM对象的基础知识,以及如何使用Python中的Selenium WebDriver进行元素定位、操作和等待机制,旨在帮助初学者理解Web自动化测试中的关键概念和操作技巧。
54 1
|
3月前
|
开发者 Java Spring
【绝技揭秘】掌握Vaadin数据绑定:一键同步Java对象,告别手动数据烦恼,轻松玩转Web应用开发!
【8月更文挑战第31天】Vaadin不仅是一个功能丰富的Java Web应用框架,还提供了强大的数据绑定机制,使开发者能轻松连接UI组件与后端Java对象,简化Web应用开发流程。本文通过创建一个简单的用户信息表单示例,详细介绍了如何使用Vaadin的`Binder`类实现数据绑定,包括字段与模型属性的双向绑定及数据验证。通过这个示例,开发者可以更专注于业务逻辑而非繁琐的数据同步工作,提高开发效率和应用可维护性。
87 0
|
4月前
|
开发框架 JSON 前端开发
利用查询条件对象,在Asp.net Web API中实现对业务数据的分页查询处理
利用查询条件对象,在Asp.net Web API中实现对业务数据的分页查询处理
|
5月前
|
开发框架 .NET API
ASP.NET Core Web中使用AutoMapper进行对象映射
ASP.NET Core Web中使用AutoMapper进行对象映射
|
6月前
|
JavaScript 前端开发 索引
【Web 前端】JQ对象和DOM元素之间如何转换?
【4月更文挑战第22天】【Web 前端】JQ对象和DOM元素之间如何转换?
|
安全 Java 编译器
【Java Web编程 八】深入理解Servlet常用对象
【Java Web编程 八】深入理解Servlet常用对象
126 1
|
存储 测试技术 网络架构
python|web应用框架|增加响应对象
python|web应用框架|增加响应对象
121 0
|
Java Maven
IDEA2022版本创建maven web项目(两种方式)
创建maven web项目有两种方式,一种是使用骨架方式,一种是不使用骨架的方式
847 2
IDEA2022版本创建maven web项目(两种方式)
|
前端开发 JavaScript 安全
web前端-JavaScript中的原型对象
web前端-JavaScript中的原型对象
71 0
下一篇
无影云桌面