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中有多个方法时,应该如何实现调用。


  • 相关阅读:
    Qt中的 Size Hints 和 Size Policies
    __declspec,__cdecl,__stdcall区别和作用
    深入理解DLL文件
    TCP/IP TIME_WAIT状态原理
    Linux 网络编程 高级套接字
    OpenCV 图像处理学习笔记(一)
    C++运算符重载的规则
    WinSock异步IO模型之Select
    ASSER、VERIFY、TRACE详解
    VC++ 网络编程总结(二)
  • 原文地址:https://www.cnblogs.com/saixing/p/6730265.html
Copyright © 2011-2022 走看看