zoukankan      html  css  js  c++  java
  • 【SSH】——Struts2中的动态方法调用(一)


    首先我们来看一个简单的调用:

           1、在web.xml中配置拦截器StrutsPrepareAndExecuteFilter。StrutsPrepareAndExecuteFilter实现了filter接口,在执行action之前,利用filter做一些操作。

    <?xml version="1.0" encoding="UTF-8"?>
    <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">
      <filter>
        <filter-name>struts2</filter-name>
        <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
      </filter>
      <filter-mapping>
        <filter-name>struts2</filter-name>
        <url-pattern>/*</url-pattern>
      </filter-mapping>
    </web-app>


           2、提供Struts2的配置文件struts.xml

    <?xml version="1.0" encoding="UTF-8" ?>
    <!DOCTYPE struts PUBLIC
        "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
        "http://struts.apache.org/dtds/struts-2.0.dtd">
    
    <struts>
    	<package name="Struts2_006" extends="struts-default" >
    		<action name="user" class="com.struts2.UserAction">
    			<result>/add_success.jsp</result>
    		</action>
    	</package>
    
    </struts>

    注:<result>标签的默认值是success,此处省略。

           3、页面显示部分。

    index.jsp页面,转向到action中,调用action中的方法。

    <body>
       <a href="user.action">调用</a>
    </body>

    调用完后,跳转到成功页面,并显示message中的消息。

     <body>
       我的操作:${message} <br>
      </body>

           4、编写Action类 UserAction。

    public class UserAction extends ActionSupport{
    
    	//消息字符串,用来显示调用结果
    	private String message;
    		public String getMessage() {
    		return message;
    	}
    	public void setMessage(String message) {
    		this.message = message;
    	}
    	
    	/***
    	 * execute方法
    	 */
    	public String execute() throws Exception{
    		message="执行execute方法";
    		return SUCCESS;
    	}
    }

           注意:这里我们让UserAction继承自ActionSupport类,从源码中可以看到ActionSupport类实现了Action接口。在ActionSupport类中也处理了execute()方法,但他并没有做什么操作,只是返回SUCCESS。因而,如果我们在UserAction中不写execute方法,也不会报错。

    public class ActionSupport implements Action, Validateable, ValidationAware, TextProvider, LocaleProvider, Serializable {
    
        protected static Logger LOG = LoggerFactory.getLogger(ActionSupport.class);
    
        private final ValidationAwareSupport validationAware = new ValidationAwareSupport();
    
        private transient TextProvider textProvider;
        private Container container;
    
    
        /**
         * A default implementation that does nothing an returns "success".
         * <p/>
         * Subclasses should override this method to provide their business logic.
         * <p/>
         * See also {@link com.opensymphony.xwork2.Action#execute()}.
         *
         * @return returns {@link #SUCCESS}
         * @throws Exception can be thrown by subclasses.
         */
        public String execute() throws Exception {
            return SUCCESS;
        }
    }


        如果在UserAction中不写execute方法,message中没有值。




              这篇博客介绍了Struts2的简单的方法调用,下篇博客将继续介绍,当action中有多个方法时,应该如何实现调用。


  • 相关阅读:
    使用pycharm专业版(支持远程调试及运行)如何运行mpi的代码呢???(mpi4py的代码)
    使用MPI时执行代码时运行命令中参见的几种参数设置
    多网卡系统下如何使用tcp协议实现MPI的分布式多机运行(mpi的实现使用openmpi)
    为什么我要弃用华为的软件产品——mindspore从入门到放弃之感想
    网络文件系统nfs服务端配置客户端权限时的demo例子
    【转载】 NFS服务器端的权限设置学习 Linux运维学习
    Ubuntu18.04系统下网络文件系统nfs的安装及简单配置
    【转载】 Sun RPC 编程简介
    【转载】 SUN RPC的传输层实现机制 —— rpcbind(nfs文件系统中的rpc)
    【转载】sun的rpc ——rpcbind(nfs文件系统中的rpc)
  • 原文地址:https://www.cnblogs.com/saixing/p/6730265.html
Copyright © 2011-2022 走看看