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. 使用hive增量更新

    目录 1.增量更新 2.对第一种情况 2.1.准备工作 2.2.更新数据 3.对第二种情况 3.1.准备工作 3.2.方法1 3.3.方法2 参考文末文章,加上自己的理解. 1.增量更新 有一个 ba ...

  2. JS相关基础

    1. ES5和ES6继承方式区别 ES5定义类以函数形式, 以prototype来实现继承 ES6以class形式定义类, 以extend形式继承 2. Generator了解 ES6 提供的一种异步 ...

  3. 处理ios键盘弹出按钮点击click失效

    用ontouchstart绑定事件即可,然后用 document.activeElement.blur();让键盘收起 this.value = value.replace(/\s/g, " ...

  4. 鸿蒙的js开发部模式16:鸿蒙布局Grid网格布局的应用一

    鸿蒙入门指南,小白速来!从萌新到高手,怎样快速掌握鸿蒙开发?[课程入口]目录:1.Grid简介2.使用Grid布局实现的效果3.grid-row-gap和grid-colunm-gap属性4.< ...

  5. 如何使用irealtime.js实现一个基于websocket的同步画板

    同步画板演示 同时打开2个tab,分别在画布上写下任意内容,观察演示结果,同时可设置画笔颜色及线条宽度.演示地址 初始化画布 <canvas id="drawBoard" w ...

  6. close() 和fluse()区别

    1.close()默认包含了一次flush()操作,关闭之后,就不能再写入了. 2.flush()刷新,flush()之后,可以接着写入. 3.缓冲区默认大小是8192字节,如果小于8192字节,不会 ...

  7. 【不在混淆的C】指针函数、函数指针、回调函数

    一.指针函数 函数的返回值是指针类型. int* fun(int a,int b); 指针函数使用: 返回字符串 这里要注意,"1234567890abc"是字符串常量,*p指向的 ...

  8. MYSQL bin_log 开启及数据恢复

    参考博客: A:https://www.jianshu.com/p/55b0d52edca2 B:https://www.cnblogs.com/martinzhang/p/3454358.html ...

  9. JUnit5学习之八:综合进阶(终篇)

    欢迎访问我的GitHub https://github.com/zq2599/blog_demos 内容:所有原创文章分类汇总及配套源码,涉及Java.Docker.Kubernetes.DevOPS ...

  10. 检查字符串是否包含另一串字符串(c++)

    在c++中检查字符串是否包含另一串字符串,这个本来是我做过的一个算法题,不过最近刚好有个需求让我想到了这个题,就在此记录一下! 使用std::string::findfunction string s ...