之前自己写的FLEAMVC框架虽然实现了0配置的问题,但是在MVC的实现上问题颇多,最近看了下元数据编程.也理清了思路...
对于控制器 它不是自己调用的,而是由core核心来调用的 , 这里贴出一个刚刚写的雏形代码...
package cn.iamsese.www.webdev.annotation;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
@Retention(value=RetentionPolicy.RUNTIME)
public @interface ToView {
public String url() default "http://iamsese.cn/index.do" ;
}
package cn.iamsese.www.webdev.annotation;
public class MC {
@ToView(url="http://iamsese.cn/iamsese.do")
public void listAction(){
System.out.println("操作list方法");
}
}
package cn.iamsese.www.webdev.annotation;
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
public class Test {
public void interop(Method met){
Annotation[] ann = met.getAnnotations();
if (met.isAnnotationPresent(ToView.class)){
ToView annserv = (ToView)
met.getAnnotation(ToView.class);
String url = annserv.url();
String metname = met.getName();
System.out.println("[" + metname + "/" + url + "]");
}
}
public void _do(String ctr , String act) throws Exception{
Class <?> cls = Class.forName("cn.iamsese.www.webdev.annotation." + ctr) ;
Method met1 = cls.getMethod(act + "Action");
met1.invoke(cls.newInstance(), null);
this.interop(met1);
}
public static void main(String[] args) throws Exception {
(new Test())._do("MC", "list");
}
}

Spring 2.5似乎完成了这种功能,尚未接触,这里将所需文件上传至此,嘿嘿

本文介绍了一个自定义的FLEAMVC框架中使用元数据编程改进控制器调用方式的方法。通过定义注解@ToView,实现了根据注解信息自动映射到特定URL的功能,并通过反射机制实现了控制器方法的调用。
443

被折叠的 条评论
为什么被折叠?



