Struts2获取web对象的4种方式-阿里云开发者社区

开发者社区> 开发与运维> 正文

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>




版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。

分享:
开发与运维
使用钉钉扫一扫加入圈子
+ 订阅

集结各类场景实战经验,助你开发运维畅行无忧

其他文章