本文共 5082 字,大约阅读时间需要 16 分钟。
通常开发时需要访问 Servlet API 中的 HttpServletRequest、HttpSession 和 ServletContext 三个接口,它们分别对应 JSP 内置对象 request、session 和 application。
在 Struts2 中,访问 Servlet API 通常采用两种方式,分别是通过 ActionContext 访问和通过 ServletActionContext 访问。
ActionContext 是 Action 执行的上下文对象,在 ActionContext 中保存了 Action 执行所需要的所有对象,包括 request、session 和 application 等。ActionContext 类访问 Servlet API 的几个常用方法如下表所示。
方法声明 | 功能描述 |
---|---|
void put(String key, Object value) | 将 key-value 键值对放入 ActionContext 中,模拟 Servlet API 中的 HttpServletRequest 的 setAttribute() 方法 |
Object get(String key) | 通过参数 key 查找当前 ActionContext 中的值 |
Map<String, Object> get Application() | 返回一个 Application 级的 Map 对象 |
static ActionContext getContext() | 获取当前线程的 ActionContext 对象 |
Map<String, Object> getParameters() | 返回一个包含所有 HttpServletRequest 参数信息的 Map 对象 |
Map<String, Object> getSession() | 返回一个 Map 类型的 HttpSession 对象 |
示例1:
ActionContext context = ActionContext.getContext();context.put("name","mengma");context.getApplication().put("name","mengma");context.getSession().put("name","mengma");
在上述示例代码中,通过 ActionContext 类中的方法调用,分别在 request、application 和 session 中放入了(“name”,“mengma”)键值对。通过代码可以看到,ActionContext 类可以非常简单地访问 JSP 内置对象的属性。
示例2:
1.创建项目 在 MyEclipse 中创建一个名称为 struts2Demo02 的 Web 项目,将 Struts2 所需的 JAR 包复制到项目的 lib 目录中,并发布到类路径下。 2.创建页面文件 在 WebContent 目录下分别创建登录页面 login.jsp、登录成功页面 success.jsp 和错误显示页面 error.jsp,如 login.jsp、success.jsp 和 error.jsp 所示。 login.jsp:<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>登录页面
success.jsp:
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>登录成功页面 ${success }
用户登录信息
用户名:${username } 密码:${password }
error.jsp
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>登录失败页面 ${error }
3.配置核心过滤器
struts2 org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter struts2 /*
4.创建Action
package com.mengma.action;import com.opensymphony.xwork2.ActionContext;import com.opensymphony.xwork2.ActionSupport;public class LoginAction extends ActionSupport { private String username; // 用户名 private String password; // 密码 // username的getter和setter方法 public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } // password的getter和setter方法 public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } @Override public String execute() throws Exception { // 获取ActionContext对象 ActionContext context = ActionContext.getContext(); if ("admin".equals(username) && "123456".equals(password)) { // 将用户名和密码信息放入context对象中 context.put("username", username); context.put("password", password); context.put("success", "用户登录成功!"); return SUCCESS; } else { // 定义登录失败的错误信息 context.put("error", "用户名或密码错误,请重新登录!"); return ERROR; } }}
5.创建配置文件
/success.jsp /error.jsp
6.运行程序并查看结果
启动 Tomcat 服务器,在浏览器的地址栏中输入地址 http://localhost:8080/struts2Demo02/login.jsp 访问 login.jsp 页面,浏览器的显示结果如下图所示。除了通过 ActionContext 类访问 Servlet API 以外,Struts2 框架还提供了 ServletActionContext 类访问 Servlet API,该类中的方法都是静态方法,其常见方法如下表所示。
方法声明 | 功能描述 |
---|---|
static PageContext getPageContext() | 获取 Web 应用的 PageContext 对象 |
static HttpServletRequest getRequest() | 获取 Web 应用的 HttpServletRequest 对象 |
static HttpServletResponse getResponse() | 获取 Web 应用的 HttpServletResponse 对象 |
static ServletContext getServletContext() | 获取 Web 应用的 ServletContext 对象 |
示例:
1.创建MessageAction类import org.apache.struts2.ServletActionContext;import com.opensymphony.xwork2.ActionSupport;public class MessageAction extends ActionSupport{ public String execute() throws Exception{ ServletActionContext.getRequest().setAttribute("message","通过ServletActionContext类访问Servlet API"); return SUCCESS; }}
2.配置struts.xml
/message.jsp
3.创建message.jsp页面
在 WebContent 目录下创建一个名称为 message.jsp 的页面,通过 EL 表达式访问存放在 Request 对象中键值为 message 的值,其页面主体部分代码如下所示:${requestScope.message }
4.运行程序并查看结果
启动 struts2Demo02 项目,在浏览器的地址栏中输入地址 http://localhost:8080/struts2Demo02/message 后,浏览器的显示结果如下图所示。转载地址:http://kasqb.baihongyu.com/