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。对安全要求级别高,可以做后台验证,前台验证已经很成熟。

相关文章
|
2月前
|
存储 安全 Java
《C++ Concurrencyin Action》第9章--高级线程管理
《C++ Concurrencyin Action》第9章--高级线程管理
|
JavaScript 开发工具 git
GitHub Actions:从使用action操作到自定义action操作
GitHub Actions:从使用action操作到自定义action操作
198 0
|
XML 前端开发 数据格式
Struts2里面灵活action跳转
Struts2里面灵活action跳转
192 0
【TP5】form表单的action地址怎么写
【TP5】form表单的action地址怎么写
278 0
【TP5】form表单的action地址怎么写
|
前端开发 数据库 JavaScript
ODOO中的Action原理?
ODOO 中的 Action原理?
1448 0