struts2总结五: Strut2 访问Servlet API

一、间接访问

public String execute() throws Exception {
this.message="hello,this is put into application!";
this.message2="hello,this is put into session!";
this.message3="hello,this is put into request!";
ActionContext ac=ActionContext.getContext();
//获取request级别的Map对象,把相关数据写入该对象中。
((Map<String ,Object>)ac.get("request")).put("message3", message3);
//获取Application级别的Map对象,把相关数据写入该对象中。
ac.getApplication().put("message", message);
//获取sessiont级别的Map对象,把相关数据写入该对象中。
ac.getSession().put("message2", message2);
return SUCCESS;
}

二、直接访问(Ioc方式)

package com.bochy.struts2.action;
import java.util.Map;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.apache.struts2.interceptor.ApplicationAware;
import org.apache.struts2.interceptor.RequestAware;
import org.apache.struts2.interceptor.ServletRequestAware;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class Hello extends ActionSupport implements ServletRequestAware{
/**
*
*/
private static final long serialVersionUID = 1L;
private HttpServletRequest request;
private HttpSession session;
private ServletContext application;
private String message;
private String message3;
public String getMessage3() {
return message3;
} public void setMessage3(String message3) {
this.message3 = message3;
} public String getMessage2() {
return message2;
} public void setMessage2(String message2) {
this.message2 = message2;
} private String message2;
public String getMessage() {
return message;
} public void setMessage(String message) {
this.message = message;
} @Override
public String execute() throws Exception {
this.message="hello,this is put into application!";
this.message2="hello,this is put into session!";
this.message3="hello,this is put into request!";
this.request.setAttribute("message3", message3);
this.session.setAttribute("message2", message2);
this.application.setAttribute("message", message);
return SUCCESS;
}
@Override
public void setServletRequest(HttpServletRequest request) {
this.request=request;
this.session=this.request.getSession();
this.application=this.session.getServletContext();
} }

三、 直接访问

package com.bochy.struts2.action;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class Hello extends ActionSupport {
private static final long serialVersionUID = 1L;
private String message;
private String message3;
public String getMessage3() {
return message3;
} public void setMessage3(String message3) {
this.message3 = message3;
} public String getMessage2() {
return message2;
} public void setMessage2(String message2) {
this.message2 = message2;
} private String message2;
public String getMessage() {
return message;
} public void setMessage(String message) {
this.message = message;
}
@Override
public String execute() throws Exception {
this.message="hello,this is put into application!";
this.message2="hello,this is put into session!";
this.message3="hello,this is put into request!";
//调用ServletActionContext的getServletContext()获取Application对象
ServletContext application=ServletActionContext.getServletContext();
//调用ServletActionContext的getRequest()获取request对象
HttpServletRequest request=ServletActionContext.getRequest();
//调用ServletActionContext.getPageContext().getSession()获取session对象
HttpSession session=ServletActionContext.getPageContext().getSession();
// HttpSession session=request.getSession();
request.setAttribute("message3", message3);
session.setAttribute("message2", message2);
application.setAttribute("message", message);
return SUCCESS;
}
}
上一篇:浏览器中显示PPT的展示效果


下一篇:Spring-Cloud-Config学习笔记(一):使用本地存储