struts2 中同一个action的实现中对应多个input的处理方法

在struts2中可能会遇到如下的情形:
public class MyAction extends Action{
public String method1(){
return SUCCESS;
}
public String method2(){
return SUCCESS;
}
}

上面这段代码包含两个方法method1, method2,并且这两个方法都会受到validate interceptor的影响,会存在校验失败的情况存在,因为struts2中如果校验失败的话会默认的返回到INPUT指定的视图,但情况是我们可能根据不同的方法返回到不同的视图,这种情况怎么处理呢?在校验的XML配置中我还真是没有找到处理方法,但可以通过annotation来进行解决。
1、解决办法如下:
public class MyAction extends Action{
@InputConfig(resultName="InputresultName1")
public String method1(){
return SUCCESS;
}
// 下面这个的最终结果类似@InputConfig(resultName="method2_input")
@InputConfig(methodName="inputMethodName2")
public String method2(){
return SUCCESS;
}
public String inputMethodName2(){
return "method2_input";
}
}

2、原理
之所以通过上面的方式可以解决,主要如下的几个实现
com.opensymphony.xwork2.interceptor.annotations.InputConfig
com.opensymphony.xwork2.interceptor.DefaultWorkflowInterceptor#doIntercept

3、解决办法一成立的前提
因为1中的例子是继承了Action的实现,所以其实现了ValidationAware接口,而没有实现com.opensymphony.xwork2.interceptor.ValidationWorkflowAware接口,如果一个action实现了ValidationWorkflowAware接口,则只能通过实现getInputResultName()方法来完成上述不同的input之间的选择问题了;

4、对其实现原理的一个演示实现;

annotation的定义:
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
*@author anwx<a href="mailto:xxx@xxx.xxx.xx">An Weixiao</a>
*@version
*/
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD})
public @interface InputConfig {
String methodName() default "";
String resultName() default "input";
}

简单的演示:
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

import com.snow.jconcept.basic.annotations.InputConfig;

/**
*@author anwx<a href="mailto:xxx@xxxxxx.xxx.xx">An Weixiao</a>
*@version $Id$
*/
public class UseInputConfig {
@InputConfig(resultName="someInputResultName")
public String someMethod(){
return "success";
}
@InputConfig(methodName="inputMethod", resultName="someInputResultName")
public String otherMethod(){
return "success";
}
public String inputMethod(){
return "inputMethod";
}
public static void main(String args[]) throws Exception{
String resultName = null;
UseInputConfig config = new UseInputConfig();
resultName = getResultName(config, "someMethod");
System.out.println(resultName);

resultName = getResultName(config, "otherMethod");
System.out.println(resultName);
}

public static String getResultName(UseInputConfig config, String methodname) throws SecurityException, NoSuchMethodException, IllegalArgumentException, IllegalAccessException, InvocationTargetException{
String resultName = null;
//throws SecurityException, NoSuchMethodException
InputConfig annotation = config.getClass().getMethod(methodname, new Class[0]).getAnnotation(InputConfig.class);
if (annotation != null) {
if (!annotation.methodName().equals("")) {
Method method = config.getClass().getMethod(annotation.methodName());
// throws IllegalArgumentException, IllegalAccessException, InvocationTargetException
resultName = (String) method.invoke(config);
} else {
resultName = annotation.resultName();
}
}
return resultName;
}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值