读书人

Struts2替父Action自动赋值

发布时间: 2012-09-02 21:00:34 作者: rapoo

Struts2为父Action自动赋值
在使用Struts2框架做项目的时候,大多数情况下,我们都会定义一个Action继承ActionSupport作为我们的父Action。在父Action中我们会把通用的一些属性提出来,但是怎么为这些属性赋值呢?

1.先看一下利用java反射,我们能从子类中获取父类的哪些属性和方法。

import java.lang.reflect.Field;import java.lang.reflect.Method;/** * 测试类 * 测试Child的getDeclaredMethods,getMethods能获取哪些Method * 测试Child的getDeclaredFields,getFields能获取哪些Field */public class Test {public static void main(String[] args) {// 输出""{Method[] methods = Child.class.getDeclaredMethods();for (Method method : methods) {System.out.println(method.getName());}}System.out.println("-----------------------------------");// 输出// getLove// wait// wait// wait// equals// toString// hashCode// getClass// notify// notifyAll{Method[] methods = Child.class.getMethods();for (Method method : methods) {System.out.println(method.getName());}}System.out.println("-----------------------------------");// 输出""{Field[] fields = Child.class.getDeclaredFields();for (Field field : fields) {System.out.println(field.getName());}}System.out.println("-----------------------------------");// 输出// love{Field[] fields = Child.class.getFields();for (Field field : fields) {System.out.println(field.getName());}}}}

经过测试,用反射反射只能从子类中获取父类的public的属性和方法。

2.经测试在Struts2访问Action能通过父Action的public方法给属性赋值。

读书人网 >互联网

热点推荐