action 的三种实现方式 | 学习笔记

简介: 快速学习 action 的三种实现方式,介绍了 action 的三种实现方式系统机制, 以及在实际应用过程中如何使用。

开发者学堂课程【Java Web开发系列课程 - Struts2框架入门action 的三种实现方式】学习笔记,与课程紧密联系,让用户快速学习知识。

课程地址:https://developer.aliyun.com/learning/course/537/detail/7297


action 的三种实现方式

 

目录

一、定义一个 pojo 类

二、实现 Action 接口

三、继承 ActionSupport 类


一、 定义一个 pojo 类

写一个项目,搭建环境,项目名字为04struts2_action,拷贝所需要的 jar 包,去关注Web xml 上面的配置文件,然后打开。

然后需要 struts 的配置文件,实现一下 action,名字为 hello action,只要实现了public string execute 就会实行 pojo action。

< ?xml version="1." encoding="UTF-8"?>

<IDOCTYPE struts PUBLIC

-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"

"http: //struts. apache. org/dtds/str.3.dtd">

<struts>

<!-extends 必须写,直接或者间接继承 struts-default- name 自定义->

<package name="helLo" extends"struts-default">

I<!--name 是请求名称,不要写/; classactio 对应完全限定名=包名+类名--

<action name="helLo" class="cn. sxt. action. HeLLoAction">

<- result 是结果集 name 和对 action 应中的方法的返回值匹配,默认是 success

<result name="success">/index. jsp</result>

</action>

</package>

/struts>

访问一下。

public class HelloAction

public String execute(){

System.out. printIn(" action");

return "success";

}

好处:自定义一个普通的 Java 类即可,不具有侵入性。

更改非常容易,测试方便。

侵入性:继承或者实现框架的一些类和接口。

不具有侵入性的话,把 struts2拿掉,这个类还可以用,改一下配置文件即可。


二、 实现 Action 接口

这个 struts2默认情况下执行 execute 方法,有的时候会敲错一个字母,一旦敲错了,便执行不起来。

struts 提供一个 hello1Action,一个普通的类,千万要改,name 为 pojo。请求pojoaction,执行成功。

实现一个接口:

有一个接口名字为 interface action,实现即可。用 execute 方法,新版1.8有很多以前的技术被颠覆,不要用1.8。

这个 Execute 方法不容易写错,写错会有提示。用接口更加规范,看一下 Action 源码,点一下 attach source,jar 包形式。

这个接口里面定义一些常量,返回 success,比较规范直接写 SUCCESS,定义常见东西是为了规范。

用 action 点 SUCCESS 也是可以的,定义了五个常量。

public interface Action

/

**

* The action execution was successful. Show result

* view to the end user.

*/I

public static final String SUCCESS ="success";

/

**

The action execution was successful but do not

* show view. This is useful for actions that are

handling the view in another fashion like redirect

*/

public static final String NONE="none";

/**

* The action execution was failure.

* Show an error view, possibly asking the

user to retry entering data.

*/

public static final String ERROR "error";

/

**

The action execution require more input

* in order to succeed.

This result is typically used if a form

*handling action has heen evecuted so as

* to provide defaults for form.The

form associated with the handler should be

shown to the end user.

<p/

This result is also used if the given input

* params are invalid, meaning the user

should try providing input again.

*/

nuhlic static final Strino TMPIIT "input"

/**

* The action could not execute, since the

* user most was not logged in. The login view

should be shown.

*/

public static final String LOGIN ="login";

/**

*

* Where the logic of the action is executed.

@return a string representing the logical result of the execution

See constants in this interface for a list of standard re

* athroue Excention  throun if svstem level exception occurs

<b>Note: </b> Application level exceptions shoul

an error value, such as <codeAction. ERROR</code

public String execute() throws Exception;

需要再次输入的情况下就需要用 input,使得 action 里面的方法更加规范一些。

配置一下,测试 Interface,成功。

好处:使得我们编写的代码更加规范

public class InterfaceAction implements Action

public String execute() throws Exception

System.out. println("interface action");

return SUCCESS

 

三、继承  ActionSupport 类

名字 ExtendsAction,继承 ActionSupport,配置一下,然后测试,重新启动。Extends.Action是正确的,有一个 execute 方法。ActionSupport 实现接口,找execute,因为实现了接口,必然有 execute,成功。

可以覆盖,写 SUCCESS,再来测试一下,刷新,Extend Action。

继承类好处:

可以默认实现,除了 execute 方法,还提供了一些其他方法,包括验证一些东西。

好处:

可以继承一些 ActionSupport 实现功能,如,验证。

官方推荐使用第三种,如果不用这个验证,推荐第一个。

验证主要是后台验证,没有通过验证,就会进去 input。对安全要求级别高,可以做后台验证,前台验证已经很成熟。

相关文章
AutoJs源码---神级大分享
AutoJs源码---神级大分享
435 0
|
Arthas 运维 监控
定位频繁创建对象导致内存溢出风险的思路
定位频繁创建对象导致内存溢出风险的思路
441 1
|
域名解析 弹性计算 网络协议
阿里云轻量应用服务器使用教程(2023新版)
阿里云轻量应用服务器使用教程(2023新版)阿里云轻量应用服务器怎么使用?阿里云轻量应用服务器使用教程:轻量应用服务器购买、重置密码、远程连接、宝塔面板的Web环境搭建、WordPress网站程序安装到网站上线,阿里云服务器网分享轻量应用服务器从购买、配置建站环境、轻量服务器应用服务器远程连接、开端口到网站上线全流程
|
算法 Go 计算机视觉
【YOLO系列】YOLOv8算法(尖端SOTA模型)
Ultralytics YOLOv8 是由 Ultralytics开发的一个前沿 SOTA 模型。它在以前 YOLO 版本的成功基础上,引入了新的功能和改进,进一步提升了性能和灵活性。YOLOv8 基于快速、准确和易于使用的理念设计,使其成为广泛的物体检测、图像分割和图像分类任务的绝佳选择。
3383 0
【YOLO系列】YOLOv8算法(尖端SOTA模型)
|
7月前
|
移动开发 前端开发 JavaScript
从入门到精通:H5游戏源码开发技术全解析与未来趋势洞察
H5游戏凭借其跨平台、易传播和开发成本低的优势,近年来发展迅猛。接下来,让我们深入了解 H5 游戏源码开发的技术教程以及未来的发展趋势。
|
JSON Java fastjson
java小工具util系列3:JSON和实体类转换工具
java小工具util系列3:JSON和实体类转换工具
229 2
|
12月前
|
资源调度 前端开发 JavaScript
前端研发链路之脚手架
本文首发于微信公众号“前端徐徐”。文章介绍了前端开发中脚手架工具的重要性及其工作原理。脚手架工具能够大幅提升开发效率,确保代码质量和项目一致性。文章详细探讨了脚手架的历史、工作原理、常见工具及其优势与潜在问题,并展望了其未来发展方向,帮助开发者更好地理解和应用脚手架工具。
306 4
前端研发链路之脚手架
|
XML JSON Java
使用IDEA+Maven搭建整合一个Struts2+Spring4+Hibernate4项目,混合使用传统Xml与@注解,返回JSP视图或JSON数据,快来给你的SSH老项目翻新一下吧
本文介绍了如何使用IntelliJ IDEA和Maven搭建一个整合了Struts2、Spring4、Hibernate4的J2EE项目,并配置了项目目录结构、web.xml、welcome.jsp以及多个JSP页面,用于刷新和学习传统的SSH框架。
475 0
使用IDEA+Maven搭建整合一个Struts2+Spring4+Hibernate4项目,混合使用传统Xml与@注解,返回JSP视图或JSON数据,快来给你的SSH老项目翻新一下吧
|
消息中间件 存储 Java
快速入门 Kafka 和 Java 搭配使用
快速入门 Kafka 和 Java 搭配使用
431 0
|
JSON 监控 Java
Java中如何解决JsonProcessingException异常?
Java中如何解决JsonProcessingException异常?