欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

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

程序员文章站 2022-05-07 21:53:03
...
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类(开发中这种方式使用最多)
		* 设置错误信息

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

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>
执行结果:

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

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

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 {

	@Override
	public 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类的三种编写方式(七)
Action类的三种编写方式(七)

三、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;

	@Override
	public 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>
运行效果:

Action类的三种编写方式(七)
Action类的三种编写方式(七)
源码下载


相关标签: struts2.0