博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
struts2总结五: Strut2 访问Servlet API
阅读量:5244 次
发布时间:2019-06-14

本文共 4279 字,大约阅读时间需要 14 分钟。

一、间接访问

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
)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;    }}

 

转载于:https://www.cnblogs.com/longshiyVip/p/4827360.html

你可能感兴趣的文章
PS 滤镜— — sparkle 效果
查看>>
snmpwalk命令常用方法总结
查看>>
网站产品设计
查看>>
代理ARP
查看>>
go 学习笔记(4) ---项目结构
查看>>
java中静态代码块的用法 static用法详解
查看>>
Java线程面试题
查看>>
Paper Reading: Relation Networks for Object Detection
查看>>
day22 01 初识面向对象----简单的人狗大战小游戏
查看>>
mybatis源代码分析:深入了解mybatis延迟加载机制
查看>>
Flask三剑客
查看>>
Hibernate-缓存
查看>>
【BZOJ4516】生成魔咒(后缀自动机)
查看>>
提高PHP性能的10条建议
查看>>
svn“Previous operation has not finished; run 'cleanup' if it was interrupted“报错的解决方法...
查看>>
熟用TableView
查看>>
Java大数——a^b + b^a
查看>>
poj 3164 最小树形图(朱刘算法)
查看>>
服务器内存泄露 , 重启后恢复问题解决方案
查看>>
android一些细节问题
查看>>