Actions是Struts2框架的核心,因为它们适用于任何MVC(Model View Controller)框架。 每个URL映射到特定的action,其提供处理来自用户的请求所需的处理逻辑。
但action还有另外两个重要的功能。 首先,action在将数据从请求传递到视图(无论是JSP还是其他类型的结果)方面起着重要作用。 第二,action必须协助框架确定哪个结果应该呈现在响应请求的视图中。

创建Action

Struts2中actions的唯一要求是必须有一个无参数方法返回String或Result对象,并且必须是POJO。如果没有指定no-argument方法,则默认是使用execute()方法。
你还可以扩展ActionSupport类,该类可实现六个接口,包括Action接口。Action的接口如下:

public interface Action {
public static final String SUCCESS = "success";
public static final String NONE = "none";
public static final String ERROR = "error";
public static final String INPUT = "input";
public static final String LOGIN = "login";
public String execute() throws Exception;
}

让我们来看看在Hello World示例中的action方法:

package cn.w3cschool.struts2;

public class HelloWorldAction{
private String name; public String execute() throws Exception {
return "success";
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
}
}

为了说明action方法控制视图的要点,让我们对execute方法进行以下更改,并扩展ActionSupport类如下:

package cn.w3cschool.struts2;

import com.opensymphony.xwork2.ActionSupport;

public class HelloWorldAction extends ActionSupport{
private String name; public String execute() throws Exception {
if ("SECRET".equals(name))
{
return SUCCESS;
}else{
return ERROR;
}
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
}
}

在这个例子中,我们在execute方法中使用一些逻辑来查看name属性。如果属性等于字符串“SECRET”,我们返回SUCCESS作为结果,否则我们返回ERROR作为结果。因为我们已经扩展了ActionSupport,所以我们可以使用String常量、SUCCESS和ERROR。 现在,让我们修改struts.xml文件如下:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<constant name="struts.devMode" value="true" />
<package name="helloworld" extends="struts-default">
<action name="hello"
class="cn.w3cschool.struts2.HelloWorldAction"
method="execute">
<result name="success">/HelloWorld.jsp</result>
<result name="error">/AccessDenied.jsp</result>
</action>
</package>
</struts>

创建视图

让我们在你的eclipse项目的WebContent文件夹中创建下面的jsp文件HelloWorld.jsp。右键单击项目资源管理器中的WebContent文件夹,然后选择“New”> “JSP File”。如果返回结果是SUCCESS将调用此文件(这个字符串常量“success”是在Action接口中定义的):

<%@ page contentType="text/html; charset=UTF-8" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
<title>Hello World</title>
</head>
<body>
Hello World, <s:property value="name"/>
</body>
</html>

如果action的结果是ERROR,即字符串常量为“error”,下面的文件将被框架调用。 以下是AccessDenied.jsp的内容:

<%@ page contentType="text/html; charset=UTF-8" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
<title>Access Denied</title>
</head>
<body>
You are not authorized to view this page.
</body>
</html>

我们还需要在WebContent文件夹中创建index.jsp文件。此文件将用作初始的action URL,用户可以单击它以命令Struts 2框架调用HelloWorldAction类的execute方法并呈现HelloWorld.jsp视图。

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Hello World</title>
</head>
<body>
<h1>Hello World From Struts2</h1>
<form action="hello">
<label for="name">Please enter your name</label><br/>
<input type="text" name="name"/>
<input type="submit" value="Say Hello"/>
</form>
</body>
</html>

这样,没有改变web.xml文件的需求,所以我们可以使用前面在Hello World示例章节中创建的的web.xml文件。现在,我们准备好使用Struts 2框架运行我们的Hello World应用程序了。

执行应用程序

右键单击项目名称,然后单击“Export”>“WAR File”创建WAR文件。 然后在Tomcat的webapps目录中部署这个WAR文件。最后,启动Tomcat服务器并尝试访问URL http://localhost:8080/HelloWorldStruts2/index.jsp。显示的图示如下:

让我们输入一个单词为“SECRET”,你会看到如下页面:

现在输入除“SECRET”之外的任何词,你会看到如下页面:

创建多个Actions

你会需要频繁定义多个action来处理不同的请求,并为用户提供不同的URL,因此你将如下定义的不同类:

package cn.w3cschool.struts2;
import com.opensymphony.xwork2.ActionSupport; class MyAction extends ActionSupport{
public static String GOOD = SUCCESS;
public static String BAD = ERROR;
} public class HelloWorld extends ActionSupport{
...
public String execute()
{
if ("SECRET".equals(name)) return MyAction.GOOD;
return MyAction.BAD;
}
...
} public class SomeOtherClass extends ActionSupport{
...
public String execute()
{
return MyAction.GOOD;
}
...
}

你将如下在struts.xml文件中的配置这些action:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
struts>
<constant name="struts.devMode" value="true" />
<package name="helloworld" extends="struts-default">
<action name="hello"
class="cn.w3cschool.struts2.HelloWorld"
method="execute">
<result name="success">/HelloWorld.jsp</result>
<result name="error">/AccessDenied.jsp</result>
</action>
<action name="something"
class="cn.w3cschool.struts2.SomeOtherClass"
method="execute">
<result name="success">/Something.jsp</result>
<result name="error">/AccessDenied.jsp</result>
</action>
</package>
</struts>

正如你在上面的例子中看到的,action的结果SUCCESS和ERROR是重复的。为了解决这个问题,建议您创建一个包含结果的类。

(三)Struts2的Action(简单讲解版)的更多相关文章

  1. J2EE进阶(三)struts2 <s:action>标签的用法

    J2EE进阶(三)struts2 <s:action>标签的用法 前言 使用action标签,可以允许在jsp页面中直接调用Action,(类似AJAX页面调用)在调用Action时候,可 ...

  2. (四)Struts2的Action(深入讲解版)

    Struts2的Action 开发者需要提供大量的Action,并在struts.xml中配置Action.Action类里包含了对用户请求的处理逻辑,因为我们也称Action为业务控制器. 一.编写 ...

  3. Struts2大约Action系统培训6大约action的接受三个参数的方法

    我们知道,action在web它在控制器的发展起到了一定作用,通过接收client来到参数,运行不同的模块只实现运营,因此,接收参数是非常重要的组成部分,有接收到的参数的仅前端.操作权限运行数据库后端 ...

  4. struts2的action访问servlet API的三种方法

    学IT技术,就是要学习... 今天无聊看看struts2,发现struts2的action访问servlet API的三种方法: 1.Struts2提供的ActionContext类 Object g ...

  5. Struts2中Action接收参数的方法主要有以下三种:

    Struts2中Action接收参数的方法主要有以下三种: 1.使用Action的属性接收参数(最原始的方式):     a.定义:在Action类中定义属性,创建get和set方法:     b.接 ...

  6. struts2开发action 的三种方法以及通配符、路径匹配原则、常量

    struts2开发action 的三种方法 1.继承ActionSupport public class UserAction extends ActionSupport { // Action中业务 ...

  7. Struts2之Action

    Struts2之Action MVC模式中需要有一个控制器来负责浏览器与服务器之间的通信,实现用户与服务器的交互.在Struts2框架中实现这一功能的是Action,它是整个框架最核心的部分.Acti ...

  8. 简单讲解iOS应用开发中的MD5加密的相关使用

      简单讲解iOS应用开发中的MD5加密的相关使用   作者:文顶顶 字体:[增加 减小] 类型:转载 时间:2015-12-19 我要评论 这篇文章主要介绍了iOS应用开发中的MD5加密的相关使用, ...

  9. [struts2学习笔记] 第五节 编写struts2的action代码

    本文地址:http://blog.csdn.net/sushengmiyan/article/details/40479299 官方文档: http://struts.apache.org/relea ...

随机推荐

  1. Android Studio 3.3.1 向avd模拟器发送本地文件

    "工具栏/View/Tool Windows/Device File Pxplorer" 选择模拟器在找到对应的文件夹upload即可

  2. C++算法代码——质因数分解[NOIP2012普及组]

    题目来自:http://218.5.5.242:9018/JudgeOnline/problem.php?id=1102 题目描述 已知正整数 n 是两个不同的质数的乘积,试求出较大的那个质数. 输入 ...

  3. if __name__ == '__main__':简单粗暴解释

    这个脚本被执行的时候,__name__ 值就是 __main__ ,才会执行 main()函数如果这个脚本是被 import 的话,__name__的值不一样.main()函数就不会被调用.这个句子用 ...

  4. 后端程序员之路 53、A Tour of Go-3

    #method    - Methods        - Go does not have classes. However, you can define methods on types.    ...

  5. 后端程序员之路 21、一个cgi的c++封装

    在"3.fastcgi.fastcgi++"中,我们了解了cgi,也尝试了fastcgi++,这里,再记录一种对fastcgi的封装. 1.cgi接口层    request_t ...

  6. 翻译:《实用的Python编程》03_02_More_functions

    目录 | 上一节 (3.1 脚本) | 下一节 (3.3 错误检查) 3.2 深入函数 尽管函数在早先时候介绍了,但有关函数在更深层次上是如何工作的细节却很少提供.本节旨在填补这些空白,并讨论函数调用 ...

  7. js 前端MD5加密

    1.引入 <script src="https://cdn.bootcss.com/blueimp-md5/2.10.0/js/md5.js"></script& ...

  8. RFC2544吞吐量测试详细步骤-MiniSMB-HurricaneII软件操作演示

    RFC2544吞吐量测试详细步骤-MiniSMB-HurricaneII软件操作演示 关键词:网络性能测试:RFC2544:吞吐量:吞吐率. RFC2544协议是RFC组织提出的用于评测网络互联设备( ...

  9. NET5 ORM 六大新功能 - SqlSugar 5.0.2.7

    介绍 SqlSugar是一款 老牌 .NET 开源ORM框架,并且在第一时间兼容.NET5,由果糖大数据科技团队维护和更新 ,Github star数仅次于EF 和 Dapper 优点: 简单易用.功 ...

  10. ZooKeeper 基本概念并介绍RPC中Netty和Zookeeper的使用

    前言 ZooKeeper 是一个分布式协调服务,可用于服务发现,分布式锁,分布式领导选举,配置管理等.Zookeeper提供一个类似Linux文件系统的属性结构,每个节点可存储少量的内存文件,并提供每 ...