Skip to content

Commit

Permalink
Merge pull request #39 from eloiseSJTU/dev
Browse files Browse the repository at this point in the history
update Struts
  • Loading branch information
Rong Huang authored Mar 5, 2017
2 parents 1652393 + f503465 commit c0c1e08
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,8 @@ public void setPassword(String password) {
public String getMessage() {
return this.message;
}

public void setMessage(String message) {
this.message = message;
}
}
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -31,8 +32,8 @@ public static View runAction(String actionName, Map<String, String> parameters)
// ("name"="test", "password"="1234"),那就应该调用
// setName和setPassword方法
for (Map.Entry<String, String> 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方法,并获得返回值
Expand All @@ -44,8 +45,8 @@ public static View runAction(String actionName, Map<String, String> parameters)
Map<String, Object> 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);
}
Expand Down Expand Up @@ -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();
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit c0c1e08

Please sign in to comment.