加入收藏 | 设为首页 | 会员中心 | 我要投稿 安卓应用网 (https://www.0791zz.com/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

Struts 2 实现Action的几种方式

发布时间:2020-05-23 11:42:19 所属栏目:Java 来源:互联网
导读:Action用于处理用户的请求,因此也被称为业务控制器。每个Action类就是一个工作单元,Struts2框架负责将用户的请求与相应的Action匹配,如果匹配成功,则调用该Action类对用户请求进行处理,而匹配规则需要在Struts2

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!");
    }
  }

}

(编辑:安卓应用网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读