`
左手边
  • 浏览: 94469 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

struts2根据action中的方法参数值来动态调用方法

 
阅读更多

struts2 根据action中的方法参数值来动态调用方法,例如http://localhost:8080/login.do?method=dynamicMethod

根据method参数来调用action中的dynamicMethod 方法,主要是自定义拦截器来实现,代码如下

package com.tedonl.interceptor;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;

public class DynamicMethodInterceptor extends AbstractInterceptor {

	private static final long serialVersionUID = -2341874790105032740L;

	@Override
	public String intercept(ActionInvocation invocation) throws Exception {
		HttpServletRequest request = ServletActionContext.getRequest();
		String myMehtod = request.getParameter("mehtod");
		if (myMehtod != null) {
			try {
				java.lang.reflect.Method method = invocation.getAction().getClass().getMethod(myMehtod);
				String result = (String) method.invoke(invocation.getAction());
				if (result != null) {
					return result;
				} else {
					return null;
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return invocation.invoke();
	}

}

 然后在web.xml中配置上自己的拦截器即可

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
	<package name="login" namespace="/login" extends="json-default">
		<interceptors>
			<interceptor name="MyInterceptor" class="com.tedonl.interceptor.DynamicMethodInterceptor"></interceptor>
			<interceptor-stack name="MyDefaultStack">
				<interceptor-ref name="MyInterceptor" />
				<interceptor-ref name="defaultStack"></interceptor-ref>
			</interceptor-stack>
		</interceptors>
		<default-interceptor-ref name="MyDefaultStack"></default-interceptor-ref>
		<action name="login" class="loginAction">
			<result name="success">/index.jsp</result>
			<result name="index">/index.jsp</result>
			<result name="conmpanyTrain">/companyTrain/sec_companyTrain.jsp</result>
		</action>

		<action name="register_*" class="registerAction" method="{1}">
			<result name="success" type="json" />
		</action>
	</package>
</struts>
 
1
1
分享到:
评论

相关推荐

    struts 2 action 动态调用

    struts 1框架的动态调用也许你会,但是struts2 的动态调用更经典,这个文档让你玩转struts 2 动态调用.......

    一个Action多方法调用的Struts 2的应用程序

    利用Struts 2框架创建一个web项目chap2_e22,实现用户登录过程。具体要求是在loginAction类中分别用login()和registered()处理用户登录和注册的过程,分别创建login.jsp和register.jsp两个页面实现登录和注册的...

    struts2利用通配符调用同一个Action里面不同的方法

    struts2利用通配符调用同一个Action里面不同的方法,在struts.xml配置文件中将请求方法的不相同部分抽象成“*".

    JS调用Struts中的Action

    JS调用Struts中的ActionJS调用Struts中的ActionJS调用Struts中的Action

    struts2笔记之动态调用Action指定方法及默认Action

    详细讲解struts2中单个action中多个处理逻辑的配置方法, 以及默认Action的配置.

    struts2动态调用之通配符

    struts2动态调用之通配符,相当于是改进的method动态调用方法,减少对同一action不同method的多次配置

    struts2动态访问调用-method方法

    struts动态访问调用之一,采用method属性,同一个Action内的不同方法来响应用户请求

    Struts2 in action中文版

    6.7.2 Struts 2中常用的表达式语言特性 131 6.7.3 表达式语言的高级特性 135 6.8 小结 137 第7章 UI组件标签 139 7.1 为什么需要UI组件标签 139 7.2 标签、模板和主题 144 7.2.1 标签 146 7.2.2 模板 146 7.2.3 ...

    Struts1与Struts2本质区别

    1 在Action实现类方面的对比:Struts 1要求Action类继承一个抽象基类;Struts 1的一个具体问题是使用抽象类编程而不是接口。Struts 2 Action类可以实现一...Struts 2 Action可以通过初始化、设置属性、调用方法来测试。

    ajaxt json 调用struts2 action的实例(myeclipse 直接导入运行)

    ajaxt json 调用struts2 action的实例(myeclipse 直接导入运行) 学习点: 1;怎样在页面用ajax调用struts2的action 2;怎样对struts进行配置 3;ajax的运行历程 最简单明了的实例,清晰的帮你弄清上述概念,运行...

    struts1和struts2的区别

    另外,按照惯例,在Struts1.x中只有“execute”方法能调用Action, 但在Struts2中并非必要,任何声明为public String methodName() 方法,都能通过配置来调用Action。 最后,和Struts1.x最大的革命性的不同是,...

    一个struts的action跳转大全

    取得form实例以后,调用其 reset ()方法,然后将表单中的参数放入form,如果validate属性不为false,调用validate()方法;如果validate ()返回非空的ActionErrors,将会被转到input属性指定的URI,如果返回空的...

    配置Struts 2开发环境,了解和熟悉Struts 2的开发流程、了解和熟悉Struts2标签的使用方法.rar

    4. 掌握在视图中多方法调用同一个Action的方法 5.掌握常用标签textfield、radio、paaaword、checkboxlist、select、data的使用方法,实现页面与Action的交互操作 6.要求在创建过程中,包的名称要体现班级和学号,...

    struts2例子中的action类

    很经典的struts2开发实例,其中的action类的写法可以教你很清楚的了解具体的调用过程回给你开发带来很大的帮助

    struts2 详解文档

    动态方法调用和使用通配符定义action 请求参数接收 自定义类型转换器 全局类型转换器 访问或添加几个属性 文件上传 多文件上传 自定义拦截器 对Action中所有方法进行输入校验 对Action指定方法进行校验 ...

    Struts2属性文件详解

    该属性设置Struts 2是否支持动态方法调用,该属性的默认值是true.如果需要关闭动态方法调用,则可设置该属性为false. struts.enable.SlashesInActionNames 该属性设置Struts 2是否允许在Action名中使用斜线,该属性的...

    详解Struts2动态方法调用

    主要介绍了详解Struts2动态方法调用,涉及调用方法的代码,具有一定参考价值,需要的朋友可以了解下。

    Struts2全解Struts2全解

    Namespace、自定义Action、路径问题、通配符、包含模块配置文件、默认Action、接受用户输入、服务器跳转、Action中访问web元素、简单数据校验、调用Action的自定义方法 5struts2国际化 ......... 6 struts2输入校验...

    用js模拟struts2的多action调用示例

    最近修了几个struts2.1升级到2.3后动态方法调用失效的bug,深有感悟, 但是我那种原始方法有一个局限,就是在submit那里写下的action不起作用,就算启动了动态方法调用也不行(我想应该是struts2.3的一个bug),所以...

    struts2流程与流程图

    一个请求在Struts 2框架中的处理大概分为以下几个步骤。...Struts 2的核心控制器是FilterDispatcher,有3个重要的方法:destroy()、doFilter()和Init(),可以在Struts 2的下载文件夹中找到源代码,如代码1所示。

Global site tag (gtag.js) - Google Analytics