当前位置: 首页 > news >正文

Action类的三种编写方式(七)

1. Action类就是一个POJO类
什么是POJO类,POJO(Plain Ordinary Java Object)简单的Java对象.简单记:没有继承某个类,没有实现接口,就是POJO的类。2. Action类可以实现Action接口
	* Action接口中定义了5个常量,5个常量的值对应的是5个逻辑视图跳转页面(跳转的页面还是需要自己来配置),还定义了一个方法,execute方法。
	* 大家需要掌握5个逻辑视图的常量
		* SUCCESS		-- 成功.	* INPUT			-- 用于数据表单校验.如果校验失败,跳转INPUT视图.	* LOGIN			-- 登录.	* ERROR			-- 错误.	* NONE			-- 页面不转向.3. Action类可以去继承ActionSupport类(开发中这种方式使用最多)* 设置错误信息


supportAction里面提供了一些属性的方法供我们使用。


举例:

1.POJO

package com.ken.action;public class Demo1Action {/*** execute是默认方法* return null; 不会进行跳转* @return*/public String execute(){System.out.println("Demo1Action就是POJO类...");return null;}
}
<?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><package name="default" namespace="/" extends="struts-default"><!-- 配置Action --><action name="hello" class="com.ken.action.HelloAction" method="sayHello"><!-- 配置跳转的页面,路径的写法:在struts2框架中,不管是转发还是重定向都不用写项目名 --><!-- name中的ok叫做逻辑视图名称 --><result name="ok">/demo1/success.jsp</result></action><!-- POJO --><action name="demo1Action" class="com.ken.action.Demo1Action"></action></package>
</struts>
执行结果:



2.实现Action接口

/** Copyright 2002-2007,2009 The Apache Software Foundation.* * Licensed under the Apache License, Version 2.0 (the "License");* you may not use this file except in compliance with the License.* You may obtain a copy of the License at* *      http://www.apache.org/licenses/LICENSE-2.0* * Unless required by applicable law or agreed to in writing, software* distributed under the License is distributed on an "AS IS" BASIS,* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.* See the License for the specific language governing permissions and* limitations under the License.*/
package com.opensymphony.xwork2;/*** All actions <b>may</b> implement this interface, which exposes the <code>execute()</code> method.* <p/>* However, as of XWork 1.1, this is <b>not</b> required and is only here to assist users. You are free to create POJOs* that honor the same contract defined by this interface without actually implementing the interface.*/
public interface Action {/*** The action execution was successful. Show result* view to the end user.*/public static final String SUCCESS = "success";/*** The action execution was successful but do not* show a 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 a 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 been executed so as* to provide defaults for a 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.*/public static final String INPUT = "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 result values.* @throws Exception thrown if a system level exception occurs.*                   <b>Note:</b> Application level exceptions should be handled by returning*                   an error value, such as <code>Action.ERROR</code>.*/public String execute() throws Exception;}
这是Action的源码,其实,和自己定义一个接口没什么区别。
package com.ken.action;import com.opensymphony.xwork2.Action;/*** 实现Action接口,Action是框架提供的接口*/
public class Demo2Action implements Action {@Overridepublic String execute() throws Exception {System.out.println("实现了Action接口");return SUCCESS;}
}
<?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><package name="default" namespace="/" extends="struts-default"><!-- 配置Action --><action name="hello" class="com.ken.action.HelloAction" method="sayHello"><!-- 配置跳转的页面,路径的写法:在struts2框架中,不管是转发还是重定向都不用写项目名 --><!-- name中的ok叫做逻辑视图名称 --><result name="ok">/demo1/success.jsp</result></action><!-- POJO --><action name="demo1Action" class="com.ken.action.Demo1Action"></action><!-- 实现Action接口 --><action name="demo2Action" class="com.ken.action.Demo2Action"><result name="success">/demo1/success.jsp</result></action></package>
</struts>
运行效果:



三、Action类继承ActionSupport

package com.ken.action;import com.opensymphony.xwork2.ActionSupport;/*** 编写Action类继承ActionSupport,ActionSupport已经实现了Action和一些其他的类**/
public class Demo3Action extends ActionSupport {private static final long serialVersionUID = -1796731178213647676L;@Overridepublic String execute() throws Exception {System.out.println("Demo3Action继承了ActionSupport类");return NONE;}
}
<?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><package name="default" namespace="/" extends="struts-default"><!-- 配置Action --><action name="hello" class="com.ken.action.HelloAction" method="sayHello"><!-- 配置跳转的页面,路径的写法:在struts2框架中,不管是转发还是重定向都不用写项目名 --><!-- name中的ok叫做逻辑视图名称 --><result name="ok">/demo1/success.jsp</result></action><!-- POJO --><action name="demo1Action" class="com.ken.action.Demo1Action"></action><!-- 实现Action接口 --><action name="demo2Action" class="com.ken.action.Demo2Action"><result name="success">/demo1/success.jsp</result></action><!-- Action类继承ActionSupport --><action name="demo3Action" class="com.ken.action.Demo3Action"></action></package>
</struts>
运行效果:



源码下载


http://www.xdnf.cn/news/821737.html

相关文章:

  • 《圣女之歌2:撒雷母天使》超强全攻略2
  • conn.execute、rs.open、command.execute方法用法大大不同
  • 原神4.7私服制作教程(含下载)
  • 基于javaweb+mysql的ssm校园社团门户网站管理系统(前台、后台)(java+jsp+ssm+mysql)
  • ggggxc学习笔记----ES6----Babel
  • Ubuntu Hardy(8.04) 版本 源列表
  • php 致命错误提示_PHP中的错误提示
  • 2023年打工人一直在纠结:逃离北上广,还是逃回北上广?
  • php cms建站,phpcms建站系统介绍以及phpcms建站流程
  • 黑客零基础自学路线(超详细),学完即可进去“包吃包住”
  • 关于 创建网络套接字失败 10106
  • 电商平台订单项目分析订单数据分析||电商数据分析项目总结!
  • IT技术网站汇总
  • 利用picasa2简单制作超炫壁画
  • 撕衣服小游戏原理
  • 非诚勿扰24灯全灭php,收二手货小伙上非诚勿扰,24盏灯全灭还遭羞辱,最后才知道是收二手豪车身价上亿...
  • 零基础C入门到深入简出
  • RPMforge(Repoforge)源
  • 遗传算法求解TSP问题
  • 冒险岛无敌挂小思路
  • dwr
  • WBSC垒球世界杯规则·野球1号位
  • python图书销售管理系统(案例分析)
  • C#:读取数据DataReader
  • shell(7):四则运算
  • RDCMan之DPI 和 Screen Resolution设置
  • browser插入数据 db_SQLite Database Browser数据库查看器图文使用教程
  • ckplayer.js视频播放插件
  • sql中in、exists和not exists的用法
  • 基于PHP的编程类MOOC网站设计与实现