From f503465d53f8b90bd8a33e06d2e97685b428d716 Mon Sep 17 00:00:00 2001 From: eloise <851113375@qq.com> Date: Sun, 5 Mar 2017 16:49:54 +0800 Subject: [PATCH] update Struts --- .../coding2017/litestruts/LoginAction.java | 4 ++++ .../coding2017/litestruts/Struts.java | 17 +++++------------ .../coding2017/litestruts/test/StrutsTest.java | 3 +++ 3 files changed, 12 insertions(+), 12 deletions(-) diff --git a/group02/851113375/src/com/github/eloiseSJTU/coding2017/litestruts/LoginAction.java b/group02/851113375/src/com/github/eloiseSJTU/coding2017/litestruts/LoginAction.java index b798db0fdf..f95e054d95 100644 --- a/group02/851113375/src/com/github/eloiseSJTU/coding2017/litestruts/LoginAction.java +++ b/group02/851113375/src/com/github/eloiseSJTU/coding2017/litestruts/LoginAction.java @@ -39,4 +39,8 @@ public void setPassword(String password) { public String getMessage() { return this.message; } + + public void setMessage(String message) { + this.message = message; + } } diff --git a/group02/851113375/src/com/github/eloiseSJTU/coding2017/litestruts/Struts.java b/group02/851113375/src/com/github/eloiseSJTU/coding2017/litestruts/Struts.java index 5e3c8d9882..11df02b877 100644 --- a/group02/851113375/src/com/github/eloiseSJTU/coding2017/litestruts/Struts.java +++ b/group02/851113375/src/com/github/eloiseSJTU/coding2017/litestruts/Struts.java @@ -1,5 +1,6 @@ package com.github.eloiseSJTU.coding2017.litestruts; +import java.beans.PropertyDescriptor; import java.io.File; import java.lang.reflect.Field; import java.lang.reflect.Method; @@ -31,8 +32,8 @@ public static View runAction(String actionName, Map parameters) // ("name"="test", "password"="1234"),那就应该调用 // setName和setPassword方法 for (Map.Entry entry : parameters.entrySet()) { - String methodName = "set" + toUpperCaseFirstOne(entry.getKey()); - Method method = clazz.getMethod(methodName, String.class); + PropertyDescriptor descriptor = new PropertyDescriptor(entry.getKey(), clazz); + Method method = descriptor.getWriteMethod(); method.invoke(object, entry.getValue()); } // 2. 通过反射调用对象的execute方法,并获得返回值 @@ -44,8 +45,8 @@ public static View runAction(String actionName, Map parameters) Map map = new HashMap<>(); Field[] fields = clazz.getDeclaredFields(); for (Field field : fields) { - String methodName = "get" + toUpperCaseFirstOne(field.getName()); - Method method = clazz.getMethod(methodName); + PropertyDescriptor descriptor = new PropertyDescriptor(field.getName(), clazz); + Method method = descriptor.getReadMethod(); Object value = method.invoke(object); map.put(field.getName(), value); } @@ -77,12 +78,4 @@ private static String getPackagePath() { return path; } - private static String toUpperCaseFirstOne(String s) { - if (Character.isUpperCase(s.charAt(0))) { - return s; - } else { - return (new StringBuilder()).append(Character.toUpperCase(s.charAt(0))).append(s.substring(1)).toString(); - } - } - } diff --git a/group02/851113375/src/com/github/eloiseSJTU/coding2017/litestruts/test/StrutsTest.java b/group02/851113375/src/com/github/eloiseSJTU/coding2017/litestruts/test/StrutsTest.java index f050cf8888..f52b71975f 100644 --- a/group02/851113375/src/com/github/eloiseSJTU/coding2017/litestruts/test/StrutsTest.java +++ b/group02/851113375/src/com/github/eloiseSJTU/coding2017/litestruts/test/StrutsTest.java @@ -6,6 +6,9 @@ import org.junit.Assert; import org.junit.Test; +import com.github.eloiseSJTU.coding2017.litestruts.Struts; +import com.github.eloiseSJTU.coding2017.litestruts.View; + public class StrutsTest { @Test