资讯专栏INFORMATION COLUMN

Struts Action 控制器

genedna / 3077人阅读

摘要:请参阅许可证管理权限和限制的特定语言根据许可证。行动执行成功但没有显示一个视图。这对于有效的操作很有用以重定向等其他方式处理视图。表示执行逻辑结果的字符串。如果发生系统级异常,则抛出异常。注意应通过返回来处理应用程序级异常错误值,例如。

控制器

即,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 may implement this interface, which exposes the execute() method.
 * 

* However, as of XWork 1.1, this is not 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. *

* *

* 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. * Note: Application level exceptions should be handled by returning * an error value, such as Action.ERROR. */ 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;

/ **
 *所有动作可能实现此接口,该接口公开 execute()方法。
 * 

 *但是,从XWork 1.1开始,这是必需的,仅用于帮助用户。您可以自由创建POJO  *遵守此接口定义的相同合同而不实际实现接口。  *  * / public interface Action {     / **      *行动执行成功。显示结果      *查看最终用户。      * /     public static final String SUCCESS =“success”;     / **      *行动执行成功但没有      *显示一个视图。这对于有效的操作很有用      *以重定向等其他方式处理视图。      * /     public static final String NONE =“none”;     / **      *行动执行失败。      *显示错误视图,可能会询问      *用户重试输入数据。      * /     public static final String ERROR =“error”;     / **      *

     *动作执行需要更多输入      *为了成功。      *此结果通常用于表格      *处理行动已经执行      *提供表单的默认值。该      *与处理程序关联的表单应该是      *向最终用户显示。      *      *      *

     *如果给定输入,也会使用此结果      *参数无效,意味着用户      *应该尝试再次提供输入。      *      * /     public static final String INPUT =“input”;     / **      *行动无法执行,因为      *用户最多未登录。登录视图      *应该显示。      * /     public static final String LOGIN =“login”;     / **      *执行动作的逻辑。      *      * @return表示执行逻辑结果的字符串。      *有关标准结果值的列表,请参阅此界面中的常量。      * @throws如果发生系统级异常,则抛出异常。      * 注意:应通过返回来处理应用程序级异常      *错误值,例如 Action.ERROR 。      * /     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 否

编写配置文件





    
    
    
    
        
        
            
            /HelloWorld.jsp
            /error.html
        
    
效果如下

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/73929.html

相关文章

  • Struts2 入门 1

    摘要:是的下一代产品,是在和的技术基础上进行了合并的全新的框架。其全新的的体系结构与的体系结构差别巨大。以为核心,采用拦截器的机制来处理用户的请求,这样的设计也使得业务逻辑控制器能够与完全脱离开,所以可以理解为的更新产品。 Struts是什么 概念 Struts2是一个基于MVC设计模式的Web应用框架,它本质上相当于一个servlet,在MVC设计模式中,Struts2作为控制器(Cont...

    tabalt 评论0 收藏0
  • SpringMVC入门就这么简单

    摘要:也就是说映射器就是用于处理什么样的请求提交给处理。这和是一样的提交参数的用户名编号提交配置处理请求注册映射器包框架接收参数设置无参构造器,里边调用方法,传入要封装的对象这里的对象就表示已经封装好的了对象了。 什么是SpringMVC? SpringMVC是Spring家族的一员,Spring是将现在开发中流行的组件进行组合而成的一个框架!它用在基于MVC的表现层开发,类似于struts...

    SKYZACK 评论0 收藏0
  • 面试题:SpringMVC和Struts2的区别

    摘要:的入口是,而是这里要指出,和是不同的。以前认为是的一种特殊,这就导致了二者的机制不同,这里就牵涉到和的区别了。开发效率和性能高于。的实现机制有以自己的机制,用的是独立的方式。 1、Struts2是类级别的拦截, 一个类对应一个request上下文,SpringMVC是方法级别的拦截,一个方法对应一个request上下文,而方法同时又跟一个url对应,所以说从架构本身上SpringMVC...

    isaced 评论0 收藏0
  • Struts2开发流程

    摘要:的开发流程在文件中定义核心拦截用户请求。的最大作用是配置和请求之间的对应关系,并配置逻辑视图名和物理视图资源之间的相对关系,即返回结果和文件的物理位置的关系。实现为了使开发的更规范,提供了一个接口,定义了的处理应该实现的规范。 1.struts2的开发流程 在web.xml文件中定义核心Filter拦截用户请求。 struts2 org.apa...

    Leo_chen 评论0 收藏0
  • Struts2入门这一篇就够了

    摘要:现在,我们使用了的话,那么框架内部就能帮我们封装了。每个中都有和这样的方法,没必要的。我们抽取出来,通过配置文件来把这两个方法替换掉,那么我们的程序就会更加优雅了。于是乎,就应运而生了。因此,学习的时候,不了解是没有任何关系的。 前言 这是Strtus的开山篇,主要是引入struts框架...为什么要引入struts,引入struts的好处是什么,以及对Struts2一个简单的入门.....

    Scliang 评论0 收藏0
  • Struts2开发入门指南

    摘要:作为一个开发框架,它为我们很好的提供了一个开发模板,使用可以减轻开发人员的负担并且可以增强程序的可读性,下面我们来说说如何使用做一个小例子开发所需要的工具开发环境开发的包一个文档模板开发开发分为以下四步完成导入相应的包在文档中配置的核 **Struts2**作为一个开发框架,它为我们很好的提供了一个开发模板,使用**Struts2**可以减轻开发人员的负担并且可以增强程序的可读性,下面...

    notebin 评论0 收藏0

发表评论

0条评论

最新活动
阅读需要支付1元查看
<