设为首页 加入收藏

TOP

Spring MVC底层的简单实现(一)
2014-11-24 01:43:05 来源: 作者: 【 】 浏览:0
Tags:Spring MVC 底层 简单 实现

Spring的主要功能是揉和struts和hibernate,使他们更好的配合,发挥更大的作用。
首先要实现struts对方法的调用,先写一个实体类
public class Student {
private String username="haha";
private int age=89;
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
//此方法便于后面的测试
public void test(){
System.out.println("hello world");
}
}


写一个action,并且和实体类建立以来关系。
public class UserAction {
private Student stu;
public Student getStu() {
return stu;
}
public void setStu(Student stu) {
System.out.println("set方法: "+stu);
this.stu = stu;
}
public String register(){
System.out.println(stu.getUsername());
System.out.println(stu.getAge());
System.out.println("我是register");
return "success";
}
}


再写一个strutsaction的测试类,获取方法名,便于读取方法
import java.lang.reflect.Field;
import java.lang.reflect.Method;
public class Struts2ActionTest {

public static void main(String[] args)throws Exception {
//类的全路径
String actionClass="service.UserAction";
//将要执行的方法名称
String method="register";
//加载Action类并动态创建一个对象
Object action=Class.forName(actionClass).newInstance();
//通过这个对象得到自己的Class信息对象
Class cls=action.getClass();
//得到这个类的属性变量
Field[] fields=cls.getDeclaredFields();
for(Field field:fields){
//得到属性的类型
Class fieldClass=field.getType();
//创建这个属性的对象
Object fieldObj=fieldClass.newInstance();
//得到属性名
String fieldName=field.getName();
//构造setter方法
String setMethod="set"+fieldName.substring(0,1).toUpperCase()
+fieldName.substring(1);
System.out.println("setMethod: "+setMethod);
//通过方法名和参数列表得到具体的方法对象
Method prop_method=cls.getDeclaredMethod(setMethod,new Class[]{fieldClass});
//执行这个方法,并且传入fieldObj参数(调用了setter方法进行赋值)
prop_method.invoke(action,fieldObj);
}
//得到Action中具体的执行方法
Method actionMethod=cls.getDeclaredMethod(method);
//执行这个方法
actionMethod.invoke(action);
System.out.println(actionMethod.invoke(action));
}
}


这样就得到了UserAction里的setStu方法,这样就可以进入register方法,并且得到方法返回的“success”。


下面写一个方法调用的类,通过包名+类名+方法名,实现方法的调用:


import java.lang.reflect.Field;
import java.lang.reflect.Method;
public class InvokeHandle {
public static void callMethod(String className,String methodName){
try {
Object obj=Class.forName(className).newInstance();
Class cls=obj.getClass();
System.out.println("父类是:"+cls.getSuperclass().getName());
//得到这个类的属性变量
Field[] fields=cls.getDeclaredFields();
System.out.println("成员变量有:");
for(Field f:fields){
System.out.println(f.getName()+" *** ");
}
System.out.println("=====================");
Method[] methods=cls.getDeclaredMethods();
System.out.print("方法有:");
for(Method method:methods){
System.out.println(method.getName()+" ");
}
System.out.println("=====================");
Method targetMethod=cls.getMethod(methodName,null);
targetMethod.invoke(obj);
} catch (Exception e) {
e.print

首页 上一页 1 2 下一页 尾页 1/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇Android应用程序创建桌面快捷方式 下一篇C指针解析及指针作为参数传递的应..

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: