Struts2之获取ServletAPI

Posted 姜文文

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Struts2之获取ServletAPI相关的知识,希望对你有一定的参考价值。

1.通过ServletActionContext类

//获取request对象
HttpServletRequest request = ServletActionContext.getRequest();
//保存值到application中
ServletActionContext.getActionContext(request).setApplication(new HashMap<>());

2.通过ServletRequestAware 接口

    public class UserAction extends ActionSupport implements ServletRequestAware {

    private String username;

    private String userpwd;

    private HttpServletRequest request;


    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getUserpwd() {
        return userpwd;
    }

    public void setUserpwd(String userpwd) {
        this.userpwd = userpwd;
    }

    @Override
    public void setServletRequest(HttpServletRequest arg0) {

        this.request = arg0;
    }

    public void executeAjax() throws IOException {

        // 处理Ajax请求

        String name = request.getParameter("username");
        
        String pwd = request.getParameter("userpwd");

        HttpServletResponse response = ServletActionContext.getResponse();
        
        response.getWriter().print("hello"+username+"hello"+userpwd);
        
    }

    }

以上是关于Struts2之获取ServletAPI的主要内容,如果未能解决你的问题,请参考以下文章

5.Struts2框架中的ServletAPI如何获取

struts2框架学习笔记3:获取servletAPI

struts2学习笔记---Action中訪问ServletAPI获取Map类型的Servlet元素

5.struts2中Action类中获取ServletAPI的三种方式

struts2学习笔记---Action中訪问ServletAPI获取真实类型的Servlet元素

Struts2 ModelDriven接口使用