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>




目录
相关文章
|
存储 IDE 前端开发
SpringBoot2.x系列教程02--新纪元之SpringBoot创建Web项目的常用方式
前言 在上一章节中,壹哥 给大家介绍了SpringBoot的优缺点,并重点介绍了其”约定大于配置“的思想,你现在还能记得吗? 而且上文中,壹哥 说我们创建SpringBoot项目有3种方式,我们已经学习了第一种创建项目的方式了,接下来还有另外两种创建项目的方式,这两种方式该怎么创建项目呢?今天 壹哥 就把剩余的两种方式也一股脑都抖搂给大家吧。 一. 以官网模板方式创建Web项目(了解) 首先 壹哥 给大家介绍第2种创建Web项目的方式,对于这种方式大家仅做了解即可,其原理与第一种以Spring Initializr创建项目的方式一样。 1. 在spring.io官网下载模板构建项目 首先我们
342 0
|
17天前
|
开发框架 .NET API
ASP.NET Core Web中使用AutoMapper进行对象映射
ASP.NET Core Web中使用AutoMapper进行对象映射
|
1月前
|
JavaScript 前端开发 索引
【Web 前端】JQ对象和DOM元素之间如何转换?
【4月更文挑战第22天】【Web 前端】JQ对象和DOM元素之间如何转换?
|
安全 Java 编译器
【Java Web编程 八】深入理解Servlet常用对象
【Java Web编程 八】深入理解Servlet常用对象
101 1
|
存储 测试技术 网络架构
python|web应用框架|增加响应对象
python|web应用框架|增加响应对象
102 0
|
前端开发 JavaScript 安全
web前端-JavaScript中的原型对象
web前端-JavaScript中的原型对象
55 0
|
存储 JavaScript 前端开发
web前端-JavaScript中的对象(Object)
web前端-JavaScript中的对象(Object)
92 0
|
Java Maven
IDEA2022版本创建maven web项目(两种方式)
创建maven web项目有两种方式,一种是使用骨架方式,一种是不使用骨架的方式
748 1
IDEA2022版本创建maven web项目(两种方式)
|
测试技术 iOS开发
Flutter Web网站之最简方式实现暗黑主题无缝切换
Flutter Web网站之最简方式实现暗黑主题无缝切换
282 0
Flutter Web网站之最简方式实现暗黑主题无缝切换
|
存储 Java Apache
Java Web之Struts2高级技术的应用(OGNL、标签库、拦截器、数据验证)
Java Web之Struts2高级技术的应用(OGNL、标签库、拦截器、数据验证)
168 0
Java Web之Struts2高级技术的应用(OGNL、标签库、拦截器、数据验证)