struts2中在Action中如何获取servlet的api?

1.通过ActionContext类(拿到的不是真正的servlet api,而是一个map)

  ActionContext context = ActionContext.getContext();

  context.getSession();

  context.getApplication();

2.通过ServletActionContext类(拿到的是真正的servlet api)

  ServletActionContext.getRequest();

  ServletActionContext.getResponse();

  ServletActionContext.getPageContext();

3.通过注入方式获取(拿到的是真正的servlet api)

  * ServletContextAware、ServletRequestAware、ServletResponseAware接口

  (1) 实现对应的接口:implements ServletRequestAware

  (2) 声明私有属性例如 private HttpServletRequest request;

  (3) 复写接口方法setsetServletRequest

  public void setServletRequest(HttpServletRequest request) {
    this.request = request;
  }

时间: 2024-08-07 17:42:36

struts2中在Action中如何获取servlet的api?的相关文章

struts2中把action中的值传递到jsp页面的例子

例子: RegistAction的代码: package com.wss.action; import javax.servlet.http.HttpServletRequest; import org.apache.struts2.ServletActionContext; import com.opensymphony.xwork2.ActionContext; import com.opensymphony.xwork2.ActionSupport; import com.wss.Dao.

实现J2EE中后台action中控制前台javascript弹出对话框

1.后台Action中: request.setAttribute("message", "此用户名或密码错误,请从新输入!"); 2.前台jsp页面中: 2.1.在<body>标签中增加onload方法,比如:<BODY  topmargin="0" leftmargin="0" onload="checkForm()"> 2.2.在本jsp页面中加入js代码块,如下: <!

Struts2:在 Action 中访问 WEB 资源

1.什么是 WEB 资源? HttpServletRequest.HttpSession.ServletContext 等原生的 Servlet API. 2.为什么访问 WEB 资源? B/S 的应用的 Controller 中必然需要访问 WEB 资源(向域对象中读写属性.读写Cookie.获取 realPath 等) 3.如何访问? 1).和Servlet API 解耦的方式:只能访问有限的 Servlet API 对象,且只能访问有限的方法.(使用 ActionContext .实现 X

struts2使用注解--ACTION中的应用

1.在类中指定包:@ParentPackage("system").其中system是在struts.xml中定义的包名.2.配置文件--->注解2.1配置文件方式(返回json):<action name="loadUserTree" class="userAction" method="loadUserTree">  <result type="json"><para

struts2获取servlet的api

public class Demo1Action extends ActionSupport {public String execute(){ServletContext application = ServletActionContext.getServletContext();HttpServletRequest request = ServletActionContext.getRequest();HttpServletResponse response = ServletActionC

在Struts2框架中使用Servlet的API

1. 在Action类中也可以获取到Servlet一些常用的API * 需求:提供JSP的表单页面的数据,在Action中使用Servlet的API接收到,然后保存到三个域对象中,最后再显示到JSP的页面上. * 提供JSP注册的页面,演示下面这三种方式 <h3>注册页面</h3> <form action="${ pageContext.request.contextPath }/xxx.action" method="post"&g

struts2对action中的方法进行输入校验

有时我们需要对Action中注入的字段进行校验,这时我们就需要用到invidate()方法了. 首先需要Action实现ActionSupport,然后重写invidate()方法,在其中对字段进行校验. 如果校验合法,则执行action中的相应方法(一般为execute),请求转发到相应的jsp: 如果校验失败,可以通过addFieldError()方法将错误信息添加到FieldErrors中, 此时action中的相应方法(一般为execute)不会执行,struts2框架默认返回"inpu

在Action中获取servlet API

Struts2的Action组件是不依赖servlet API 的.那么当你在action中的业务需要处理HttpServletRequest和HttpServletResponse的时候(比如要对响应做处理写cookie,生成验证码)怎么办呢? 有3种办法可以实现action中获取servlet api 1.使用ServletActionContext的静态方法   Struts2使用ServletActionContext对象维护Servlet api 对象(像request,respons

Struts2中获取servlet API的几种方式

struts2是一个全新的MVC框架,如今被广大的企业和开发者所使用,它的功能非常强大.这给我们在使用servlet 纯java代码写项目的时候带来了福音.但是一般来说,我们的项目不到一定规模并不需要框架的.通常功能模块和系统架构复杂的时候会少不了框架的,如果没有框架,我们写的项目和代码会复杂很多,而且扩展性也会大大降低,代码审查效率也会降低.如下代码供大家参考,主要是说明在使用struts2的时候如何获取servlet API,大家可以试着对比一下不用struts2的时候的所写的servelt