温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Struts 2 实现Action的几种方式

发布时间:2020-08-24 19:43:06 来源:脚本之家 阅读:147 作者:谢随安 栏目:编程语言

Action用于处理用户的请求,因此也被称为业务控制器。每个Action类就是一个工作单元,Struts 2框架负责将用户的请求与相应的Action匹配,如果匹配成功,则调用该Action类对用户请求进行处理,而匹配规则需要在Struts 2的配置文件中声明。

Struts 2框架下实现Action类有以下三种方式:

  1. 普通的POJO类,该类通常包含一个无参数的execute()方法,返回值为字符串类型。
  2. 实现Action接口
  3. 继承ActionSupport类

POJO实现方式

以用户登录为例,创建LoginAction类。

package com.qst.chapter03.action;

public class LoginAction {
  /* 用户名 */
  private String userName;
  /* 密码 */
  private String password;

  public String getUserName() {
    return userName;
  }

  public void setUserName(String userName) {
    this.userName = userName;
  }

  public String getPassword() {
    return password;
  }

  public void setPassword(String password) {
    this.password = password;
  }

  /**
   * 调用业务逻辑方法,控制业务流程
   */
  public String execute() {
    System.out.println("----登录的用户信息-----");
    System.out.println("用户名:" + userName);
    System.out.println("密码:" + password);
    if (userName.startsWith("qst") && password.length() >= 6) {
      // 返回成功页面
      return "ok";
    } else {
      // 返回失败页面
      return "error";
    }
  }

}

登录页面:

<%@ page language="java" contentType="text/html; charset=UTF-8"
  pageEncoding="UTF-8"%>
<html>
<head>
<title>用户登录</title>
</head>
<body>
  <form action="login.action" method="post" name="logForm">
    <table>
      <tr>
        <td>用户名</td>
        <td><input type="text" name="userName" size="15" /></td>
      </tr>
      <tr>
        <td>密码</td>
        <td><input type="password" name="password" size="15" /></td>
      </tr>
      <tr>
        <td colspan="2"><input type="submit" value="登录"></td>
      </tr>
    </table>
  </form>
</body>
</html>

错误页面:

<%@ page language="java" contentType="text/html; charset=UTF-8"
  pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>

<title>错误页面</title>
</head>
<body>
     登录失败!
</body>
</html>

成功页面:

<%@ page language="java" contentType="text/html; charset=UTF-8"
  pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>

<title>显示用户信息</title>
</head>
<body>
登录成功!欢迎用户${param.userName} !
</body>
</html>

struts.xml:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
  "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
  "http://struts.apache.org/dtds/struts-2.3.dtd">

<struts>
  <!-- 指定Struts2处于开发阶段,可以进行调试 -->
  <constant name="struts.devMode" value="true" />
  <constant name="struts.enable.DynamicMethodInvocation" value="true" />

  <!-- Struts2的Action都必须配置在package里,此处使用默认package -->
  <package name="default" namespace="/" extends="struts-default">
    <!-- 定义一个名为user的Action,实现类为com.qst.chapter03.action.LoginAction -->
    <action name="login" class="com.qst.chapter03.action.LoginAction3">
      <!-- 配置execute()方法返回值与视图资源之间的映射关系 -->
      <result name="ok">/ok.jsp</result>
      <result name="error">/error.jsp</result>
    </action>
  </package>

</struts>

这样就以POJO方式实现了一个Action,当单击“登录按钮时”,表单中的数据会提交给login.action,Struts 2框架将自动调用LoginAction的setter方法将请求参数值封装到对应的属性中,并执行execute()方法。

实现Action接口方式

为了让Action类更规范,使各个开发人员编写的execute()方法返回的字符串风格是一致的,Struts 2提供一个Action接口,该接口定义了Acitoin处理类应该实现的通用规范:

public abstract interface Action {
 
 public static final java.lang.String SUCCESS = "success";
 public static final java.lang.String NONE = "none";
 public static final java.lang.String ERROR = "error";
 public static final java.lang.String INPUT = "input";
 public static final java.lang.String LOGIN = "login";

 public String execute() throws Exception;
}

下面代码使用Action接口来创建Action类:

package com.qst.chapter03.action;

import com.opensymphony.xwork2.Action;

public class LoginAction2 implements Action{
  /* 用户名 */
  private String userName;
  /* 密码 */
  private String password;

  public String getUserName() {
    return userName;
  }

  public void setUserName(String userName) {
    this.userName = userName;
  }

  public String getPassword() {
    return password;
  }

  public void setPassword(String password) {
    this.password = password;
  }

  /**
   * 调用业务逻辑方法,控制业务流程
   */
  public String execute() {
    System.out.println("----登录的用户信息-----");
    System.out.println("用户名:" + userName);
    System.out.println("密码:" + password);
    if (userName.startsWith("qst") && password.length() >= 6) {
      // 返回成功页面
      return SUCCESS;
    } else {
      // 返回失败页面
      return ERROR;
    }
  }

}

struts.xml:

<struts>
  <!-- 指定Struts2处于开发阶段,可以进行调试 -->
  <constant name="struts.devMode" value="true" />
  <constant name="struts.enable.DynamicMethodInvocation" value="true" />

  <!-- Struts2的Action都必须配置在package里,此处使用默认package -->
  <package name="default" namespace="/" extends="struts-default">
    <!-- 定义一个名为user的Action,实现类为com.qst.chapter03.action.LoginAction -->
    <action name="login" class="com.qst.chapter03.action.LoginAction3">
      <!-- 配置execute()方法返回值与视图资源之间的映射关系 -->
      <result name="success">/ok.jsp</result>
      <result name="error">/error.jsp</result>
    </action>
  </package>

</struts>

继承ActionSupport类方式

Struts 2框架为Action接口提供了一个实现类ActionSupport,该类提供了许多默认方法,写Action类时继承ActionSupport类会大大简化Action的开发。ActionSupport类是Struts 2默认的Action处理类,如果配置Action类时没有指定class属性,系统自动默认使用ActionSupport类作为Action的处理类。

下面代码通过继承ActionSupport类来创建Action类,并重写validate()验证方法:

package com.qst.chapter03.action;

import com.opensymphony.xwork2.ActionSupport;

public class LoginAction3 extends ActionSupport {
  /* 用户名 */
  private String userName;
  /* 密码 */
  private String password;

  public String getUserName() {
    return userName;
  }

  public void setUserName(String userName) {
    this.userName = userName;
  }

  public String getPassword() {
    return password;
  }

  public void setPassword(String password) {
    this.password = password;
  }

  /**
   * 调用业务逻辑方法,控制业务流程
   */
  public String execute() {
    System.out.println("----登录的用户信息-----");
    System.out.println("用户名:" + userName);
    System.out.println("密码:" + password);
    if (userName.startsWith("qst") && password.length() >= 6) {
      // 返回成功页面
      return SUCCESS;
    } else {
      // 返回失败页面
      return ERROR;
    }
  }

  // 重写validate()方法
  public void validate() {
    // 简单验证用户输入
    if (this.userName == null || this.userName.equals("")) {
      // 将错误信息写入到Action类的FieldErrors中
      // 此时Struts 2框架自动返回INPUT视图
      this.addFieldError("userName", "用户名不能为空!");
      System.out.println("用户名为空!");
    }
    if (this.password == null || this.password.length() < 6) {
      this.addFieldError("password", "密码不能为空且密码长度不能小于6");
      System.out.println("密码不能为空且密码长度不能小于6!");
    }
  }

}

上述代码增加了一个对表单提交的数据进行验证的validate()方法,该方法会在执行execute()方法之前运行,如果发现表单提交数据不符合要求,则执行addFieldError()方法,将错误信息写入Action类的字段错误列表FieldErrors中,并且将自动返回到INPUT输入视图,让用户重新输入表单并提交。

在struts.xml配置文件中增加INPUT输入视图:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
  "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
  "http://struts.apache.org/dtds/struts-2.3.dtd">

<struts>
  <!-- 指定Struts2处于开发阶段,可以进行调试 -->
  <constant name="struts.devMode" value="true" />
  <constant name="struts.enable.DynamicMethodInvocation" value="true" />

  <!-- Struts2的Action都必须配置在package里,此处使用默认package -->
  <package name="default" namespace="/" extends="struts-default">
    <!-- 定义一个名为user的Action,实现类为com.qst.chapter03.action.LoginAction -->
    <action name="login" class="com.qst.chapter03.action.LoginAction3">
      <!-- 配置execute()方法返回值与视图资源之间的映射关系 -->
      <result name="success">/ok.jsp</result>
      <result name="error">/error.jsp</result>
      <result name="input">/login.jsp</result>
    </action>
  </package>

</struts>

当表单提交的数据验证不通过时,则会返回到输入页面,程序会依然“停留”(看起来是这样,但其实是一个新的输入页面)在输入页面login.jsp。

访问ActionContext

相对于Struts1,Struts 2的一个重要改进是使Action不再和任何Servlet API耦合,但有些时候Action类不访问Servlet API是不能实现业务逻辑的(例如跟踪HTTP Session的状态)。此时Action就需要访问Servlet API中的HttpSession。

Struts 2提供了一种更加轻松的方式来访问Servlet API。在Struts 2框架中,Action可以通过ActionContext类来访问Servlet API,ActionContext提供了读写Servlet API中的HttpServletRequest、HttpSession和ServletContext中数据的方法。

常用方法如下表所示:

方法 功能描述
Object get(Object key) 获取属性值,与HttpSevletRequest的getAttribute(String name)类似
Map getApplication() 返回一个Map对象,该对象模拟了Web应用对应的ServletContext对象
static ActionContext getContext() 静态方法,用于获取系统的ActionContext对象
Map getParameters() 获取所有的请求参数,类似于调用HttpSevletRequest对象的getParameterMap()方法
Map getSession() 返回一个Map对象,该对象模拟了HttpSession实例
void setApplication(Map application) 直接传入一个Map对象,并将该Map对象中的键/值对转换成application的属性名和属性值
void setSession(Map session) 直接传入一个Map对象,并将该Map对象中的键/值对转换成session的属性名和属性值

下面代码演示Action访问ActionContext的使用

package com.qst.chapter03.action;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class ClickNumAction extends ActionSupport {
  public String execute() {
    // 获取ActionContext对象,通过该对象访问Servlet API
    ActionContext ctx = ActionContext.getContext();
    // 获取ServletContext里的num属性
    Integer num = (Integer) ctx.getApplication().get("num");
    // 如果num属性为null,设置num属性为1
      if (num == null) {
        num = 1;
      } else {
        // 将num加1
        num++;
      }
    // 将加1后的num值保存在application中
    ctx.getApplication().put("num", num);
    return SUCCESS;
  }
}

上面代码先使用ActionContext.getContext()静态方法获取系统的ActionContext对象,再调用ActionContext对象的getApplication()方法获取ServletContext对应的Map对象,然后调用get()/put()方法进行数据的读/写操作。最后将num值保存到ServletContext中。

编写clickNum.jsp 页面,对application进行访问:

<%@ page language="java" contentType="text/html; charset=UTF-8"
  pageEncoding="UTF-8"%>
<html>
<head>
<title>点击次数统计</title>
</head>
<body>
<form action="clicknum.action" method="post">
  <input type="submit" value="点击!" />
</form>
  <!--输出点击次数 -->
  点击按钮,已点击了
  <!-- 通过表达式访问ServletContext对象的属性 -->
  ${empty applicationScope.num?0:applicationScope.num} 次
</body>
</html>

上述代码创建了一个表单,表单的action属性值为clicknum,action,当单击“提交”按钮时会将表单提交给CounterAction进行处理。

在struts.xml配置文件中增加CounterAction的配置:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
  "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
  "http://struts.apache.org/dtds/struts-2.3.dtd">

<struts>
  <!-- 指定Struts2处于开发阶段,可以进行调试 -->
  <constant name="struts.devMode" value="true" />
  <constant name="struts.enable.DynamicMethodInvocation" value="true" />

  <!-- Struts2的Action都必须配置在package里,此处使用默认package -->
  <package name="default" namespace="/" extends="struts-default">
    <!-- 定义一个名为user的Action,实现类为com.qst.chapter03.action.LoginAction -->
    <action name="login" class="com.qst.chapter03.action.LoginAction3">
      <!-- 配置execute()方法返回值与视图资源之间的映射关系 -->
      <result name="success">/ok.jsp</result>
      <result name="error">/error.jsp</result>
      <result name="input">/login.jsp</result>
    </action>
    <action name="clicknum" class="com.qst.chapter03.action.ClickNumAction">
      <result name="success">/clickNum.jsp</result>
    </action>
  </package>

</struts>

访问Servlet API

虽然Struts 2提供了ActionContext来直接访问Servlet API。但有时你需要直接访问Sevlet API。为了在Action中能够直接访问Servlet API,Struts 2还提供了一系列的其他接口,通过实现这些接口,Action可以直接访问Servlet APi。

Struts 2框架提供的访问Servlet API的接口如下表所示:

方法 功能描述
ServletContextAware 实现该接口的Action可以直接访问Web应用的SevletContext实例
ServletRequestAware 实现该接口的Action可以直接访问用户请求的HttpServletRequest实例
ServletResponseAware 实现该接口的Action可以直接访问服务器响应的HttpSevletResponse实例

下面代码以实现SevletRequestAware接口为例,通过获取HttpSession,将登陆成功的用户名保存到Session中:

package com.qst.chapter03.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.struts2.interceptor.ServletRequestAware;

import com.opensymphony.xwork2.ActionSupport;

public class LoginAction4 extends ActionSupport implements ServletRequestAware {
  /* 用户名 */
  private String userName;
  /* 密码 */
  private String password;

  public String getUserName() {
    return userName;
  }

  public void setUserName(String userName) {
    this.userName = userName;
  }

  public String getPassword() {
    return password;
  }

  public void setPassword(String password) {
    this.password = password;
  }

  //声明request对象
  private HttpServletRequest request;
  //重写ServletRequestAware中的方法
  public void setServletRequest(HttpServletRequest request) {
    this.request = request;
  }

  /**
   * 调用业务逻辑方法,控制业务流程
   */
  public String execute() {
    System.out.println("----登录的用户信息-----");
    System.out.println("用户名:" + userName);
    System.out.println("密码:" + password);
    if (userName.startsWith("qst") && password.length() >= 6) {
      // 获得session对象
      HttpSession session = request.getSession();
      //将用户名存放到session中
      session.setAttribute("CurUser", userName);
      // 返回成功页面
      return SUCCESS;
    } else {
      // 返回失败页面
      return ERROR;
    }
  }

  // 重写validate()方法
  public void validate() {
    // 简单验证用户输入
    if (this.userName == null || this.userName.equals("")) {
      // 将错误信息写入到Action类的FieldErrors中
      // 此时Struts 2框架自动返回INPUT视图
      this.addFieldError("userName", "用户名不能为空!");
      System.out.println("用户名为空!");
    }
    if (this.password == null || this.password.length() < 6) {
      this.addFieldError("password", "密码不能为空且密码长度不能小于6");
      System.out.println("密码不能为空且密码长度不能小于6!");
    }
  }

}

上述代码定义的LoginAction4 实现了SevletRequestAware接口,并且重写该接口中setServletRequest()方法,setServletRequest()方法的参数是HttpServletRequest对象,运行Web应用时,Struts 2框架会自动将当前请求对象传入setServletRequest()方法,再将该请求对象赋给LoginAction4的request属性,这样在LoginAction4类的其他方法中就可以访问到request对象了。通过request对象可以获取HttpSession对象,并将当前用户信息保存到Session中。

将login.jsp页面中表单的action属性改成login4.action:

...
<form action="login.action" method="post" name="logForm">
...

创建first.jsp显示用户信息:

<%@ page language="java" contentType="text/html; charset=UTF-8"
  pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>

<title>显示用户信息</title>
</head>
<body>
登录成功!欢迎用户${param.userName} <br/>
当前用户${session.CurUser } <br/>
<a href="second.jsp" rel="external nofollow" >下一页</a>
</body>
</html>

再创建一个second.jsp:

<%@ page language="java" contentType="text/html; charset=UTF-8"
  pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>

<title>显示用户信息</title>
</head>
<body>
请求中的用户信息:${param.userName} <br/>
Session中的用户信息:${session.CurUser }
</body>
</html>

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持亿速云。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI