Struts Action 控制器

栏目: Struts · 发布时间: 5年前

内容简介:即,mvc模型的控制器模型,用于接收数据,传递给视图层,和模型层默认使用execute方法查看com.opensymphony.xwork2下的Action接口

控制器

即,mvc模型的控制器模型,用于接收数据,传递给视图层,和模型层

默认使用execute方法

查看相关接口

查看com.opensymphony.xwork2下的Action接口

文件如下

/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you 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.
 * </p>
 */
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";

    /**
     * <p>
     * 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>
     *
     * <p>
     * This result is also used if the given input
     * params are invalid, meaning the user
     * should try providing input again.
     * </p>
     */
    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;

}

大概翻译一下

*
 *获得Apache软件基金会(ASF)的许可
 *或更多贡献者许可协议。请参阅NOTICE文件
 *与此工作一起分发以获取更多信息
 *关于版权所有权。 ASF许可此文件
 *根据Apache许可证2.0版(
 * “执照”);除非符合规定,否则您不得使用此文件
 *使用许可证。您可以在以下位置获取许可证副本
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 *除非适用法律要求或书面同意,
 *根据许可证分发的软件分发在
 *“按原样”基础,不提供任何保证或条件
 * KIND,无论是明示的还是暗示的。请参阅许可证
 *管理权限和限制的特定语言
 *根据许可证。
 * /
package com.opensymphony.xwork2;

/ **
 *所有动作<b>可能</ b>实现此接口,该接口公开<code> execute()</ code>方法。
 * <p>
 *但是,从XWork 1.1开始,这<b>不</ b>是必需的,仅用于帮助用户。您可以自由创建POJO
 *遵守此接口定义的相同合同而不实际实现接口。
 * </ p>
 * /
public interface Action {

    / **
     *行动执行成功。显示结果
     *查看最终用户。
     * /
    public static final String SUCCESS =“success”;

    / **
     *行动执行成功但没有
     *显示一个视图。这对于有效的操作很有用
     *以重定向等其他方式处理视图。
     * /
    public static final String NONE =“none”;

    / **
     *行动执行失败。
     *显示错误视图,可能会询问
     *用户重试输入数据。
     * /
    public static final String ERROR =“error”;

    / **
     * <p>
     *动作执行需要更多输入
     *为了成功。
     *此结果通常用于表格
     *处理行动已经执行
     *提供表单的默认值。该
     *与处理程序关联的表单应该是
     *向最终用户显示。
     * </ p>
     *
     * <p>
     *如果给定输入,也会使用此结果
     *参数无效,意味着用户
     *应该尝试再次提供输入。
     * </ p>
     * /
    public static final String INPUT =“input”;

    / **
     *行动无法执行,因为
     *用户最多未登录。登录视图
     *应该显示。
     * /
    public static final String LOGIN =“login”;


    / **
     *执行动作的逻辑。
     *
     * @return表示执行逻辑结果的字符串。
     *有关标准结果值的列表,请参阅此界面中的常量。
     * @throws如果发生系统级异常,则抛出异常。
     * <b>注意:</ b>应通过返回来处理应用程序级异常
     *错误值,例如<code> Action.ERROR </ code>。
     * /
    public String execute()抛出异常;

}

可以看到,定义了几个常量一个接口,其中默认执行execute方法,其中几个常量为执行结果的常量

扩展实现Action接口的ActionSupport类

/**
 * Provides a default implementation for the most common actions.
 * See the documentation for all the interfaces this class implements for more detailed information.
 */
public class ActionSupport implements Action, Validateable, ValidationAware, TextProvider, LocaleProvider, Serializable

大概翻译一下

*为最常见的操作提供默认实现。
  *有关更多详细信息,请参阅此类实现的所有接口的文档。
 */

所以直接扩展该类即可

重新扩展HelloWorldAction

package com.ming;

import com.opensymphony.xwork2.ActionSupport;

public class HelloWorldAction extends ActionSupport {
    private String name;

    @Override
    public String execute() throws Exception {
        return "success";
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

可以在execute中书写业务逻辑

重新更改如下

package com.ming;

import com.opensymphony.xwork2.ActionSupport;

public class HelloWorldAction extends ActionSupport {
    private String name;

    @Override
    public String execute() throws Exception {
        if(SUCCESS.equals(name)){
            // 此时返回SUCCESS
            return SUCCESS;
        }else{
            // 其余内容返回error
            return ERROR;
        }
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

在上方,根据name的值,完成了一个业务逻辑,返回是 or 否

编写配置文件

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE struts PUBLIC
        "-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
        "http://struts.apache.org/dtds/struts-2.5.dtd">

<struts>
    <!-- 定义调试 -->
    <constant name="struts.devMode" value="true" />
    <!-- 定义数据包 -->
    <package name="helloworld" extends="struts-default">
        <!-- 定义处理逻辑 name为指定处理的名称 class 处理的包文件 method 处理将会调用的方法-->
        <action name="hello"
                class="com.ming.HelloWorldAction"
                method="execute">
            <!-- 成功返回页面 -->
            <result name="success">/HelloWorld.jsp</result>
            <result name="error">/error.html</result>
        </action>
    </package>
</struts>

效果如下

Struts Action 控制器

Struts Action 控制器


以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

The Joy of X

The Joy of X

Niall Mansfield / UIT Cambridge Ltd. / 2010-7-1 / USD 14.95

Aimed at those new to the system seeking an overall understanding first, and written in a clear, uncomplicated style, this reprint of the much-cited 1993 classic describes the standard windowing syste......一起来看看 《The Joy of X》 这本书的介绍吧!

CSS 压缩/解压工具
CSS 压缩/解压工具

在线压缩/解压 CSS 代码

html转js在线工具
html转js在线工具

html转js在线工具

HSV CMYK 转换工具
HSV CMYK 转换工具

HSV CMYK互换工具