Struts2中Action访问Servlet API的两种方法

news/2024/7/7 11:36:27 标签: struts, servlet, action, api, string, exception

 Struts2的Action并未直接与任何Servlet  API偶合,这也是Struts2的一个改良的地方。但如何进行访问?


方法一:.[一般推荐使用](只能获得request,而response则得不到)
Struts2提供了一个ActionContext类,Struts2中的Action可以通过它进行访问。
其方法有:get(),getApplication(),getContext(),getParameters(),getSession(),setApplication(),setSession()


/**
 * @作者 Jcuckoo
 * @创建日期 2008-12-02
 * @版本 V 1.0
 */


public class LoginAction implements Action
{
    private String username;
    private String password;
    public String getPassword()
    {
        return password;
    }
    public void setPassword(String password)
    {
        this.password = password;
    }
    public String getUsername()
    {
        return username;
    }
    public void setUsername(String username)
    {
        this.username = username;
    }
    public String execute() throws Exception
    {
        //获取静态方法,获取系统的ActionContext实例
            ActionContext ctx = ActionContext.getContext();
        //获取servletContext里的counter属性
        Integer counter = (Integer)ctx.getApplication().get("counter");
        if (counter == null)
        {
            counter = 1;
        }
        else
        {
            counter = counter + 1;
        }
        //将增加1后的counter值设置成counter属性
        ctx.getApplication().put("counter" , counter);
        ctx.getSession().put("user" , getUsername());
        if (getUsername().equals("scott")&& getPassword().equals("tiger") )
        {
        //直接设置HttpServletRequest属性
            ctx.put("tip" , "服务器提示:您已经成功的登陆");
                    return SUCCESS;
        }else{
        //直接设置HttpServletRequest属性
            ctx.put("tip" , "服务器提示:登陆失败");
                 return ERROR;
        }
    }
}

方法二:[不推荐](麻烦,与servlet API 耦合大).

虽然Struts2提供了ActionContext来访问Servlet API,但这种访问毕竟不能直接获得Servlet API,为了在Action中直接访问Servlet API,Struts2还提供了一下接口:ServletContextAware,ServletRequestAware,ServletResponseAware
下面以ServletResponseAware为例。

/**
 * @作者 Jcuckoo
 * @创建日期 2008-12-02
 * @版本 V 1.0
 */

//实现ServletResponseAware
public class LoginAction implements Action , ServletResponseAware
{
//需要访问的HttpServletResponse对象
    private HttpServletResponse response;
    private String username;
    private String password;
    public String getPassword(){
        return password;
    }
    public void setPassword(String password){
        this.password = password;
    }
    public String getUsername(){
        return username;
    }
    public void setUsername(String username){
        this.username = username;
    }
//实现ServletResponseAware接口必须实现的方法
    public void setServletResponse(HttpServletResponse response) {
    this.response = response;
  }
    public String execute() throws Exception{
        Cookie c = new Cookie("user" , getUsername());
        c.setMaxAge(60 * 60);
        response.addCookie(c);
        return SUCCESS;
    }
}
方法三:强烈推荐使用

Struts2还提供了一个ServletActionContext,其静态方法有:getPageContext(),getRequest(),getResponse(),getServletContext()

HttpServletRequest request = ServletActionContext.getRequest(); 
HttpServletResponse response = ServletActionContext.getResponse(); 
request.getSession().setAttribute("username","admin"); 
request.setAttribute("password", "1234");


本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/guoquanyou/archive/2008/12/02/3431164.aspx


http://www.niftyadmin.cn/n/1051020.html

相关文章

分布式协议学习笔记(二) 日志复制

一旦Leader选定之后,我们将复制所有的操作到每个节点上。 这个是通过心跳包中的Append Entries message结构体来进行实现的 客户端发送一个操作到Leader中(即使客户端发送操作到Follower,操作也会被转发到Leader处理) Leader将操作附加到自己的Log日志中…

七牛云 上传图片 https 修改Nginx 注意事项

仅在这记录下,今天的事情。 问题出自于Nginx 设置http 强制跳转 https设置 1.上午,出于某些需求,我将服务器Nginx 设置http 强行跳转 https server {listen 80;server_name www.server_name.com;rewrite ^(.*)$ https://$host$1 perm…

oracle 11g即时客户端配置详解

1. 从OTN下载几个压缩包 下载地址为: http://www.oracle.com/technology/software/tech/oci/instantclient/index.html 然后根据你的平台,选择不同的下载,我是安装在rhel5下,所以选择了linux x86 并下载了下面2个包: instantcl…

Struts 2中实现文件上传

前一阵子有些朋友在电子邮件中问关于Struts 2实现文件上传的问题, 所以今天我们就来讨论一下这个问题。 实现原理 Struts 2是通过Commons FileUpload文件上传。Commons FileUpload通过将HTTP的数据保存到临时文件夹,然后Struts使用fileUpload拦截器将文…

Spring和ActiveMq消息队列整合详解

Spring和ActiveMq消息队列整合详解 官方主页 Spring ActiveMq 一、概述 消息中间件利用高效可靠的消息传递机制进行平台无关的数据交流,并基于数据通信来进行分布式系统的集成。通过提供消息传递和消息排队模型,它可以在分布式环境下扩展进程间的通…

How does a single thread handle asynchronous code in JavaScript?

原文: https://www.quora.com/How-does-a-single-thread-handle-asynchronous-code-in-JavaScript -------------------------------------------------------------------------------- Well, arguably its not true that Javascript is single threaded if you se…

java数据库设计中的14个技巧(zhuang SCDN)

下述十四个技巧,是许多人在大量的数据库分析与设计实践中,逐步总结出来的。对于这些经验的运用,读者不能生帮硬套,死记硬背,而要消化理解,实事求是,灵活掌握。并逐步做到:在应用中发…

SpringBoot入门建站全系列(五)使用Spring-data-jpa操作数据库CRUD

SpringBoot入门建站全系列(五)使用Spring-data-jpa操作数据库 SpringBoot操作数据库有多种方式,如 JDBC直接操作:太古老了,没人愿意这样玩 Mybatis插件:比较时髦,比较适合sql复杂,或者对性能…