java reflection - 리플렉션한 메소드의 파라미터값 주입
This commit is contained in:
4
.gitignore
vendored
4
.gitignore
vendored
@@ -26,6 +26,10 @@ out/
|
|||||||
!**/src/main/**/out/
|
!**/src/main/**/out/
|
||||||
!**/src/test/**/out/
|
!**/src/test/**/out/
|
||||||
|
|
||||||
|
# Compliled files
|
||||||
|
/target/
|
||||||
|
**/target
|
||||||
|
|
||||||
### NetBeans ###
|
### NetBeans ###
|
||||||
/nbproject/private/
|
/nbproject/private/
|
||||||
/nbbuild/
|
/nbbuild/
|
||||||
|
|||||||
@@ -9,12 +9,14 @@ public class UserController {
|
|||||||
@RequestMapping("/user/join")
|
@RequestMapping("/user/join")
|
||||||
public String join(JoinDto dto){
|
public String join(JoinDto dto){
|
||||||
System.out.println("join() 함수 호출");
|
System.out.println("join() 함수 호출");
|
||||||
|
System.out.println(dto);
|
||||||
return "/";
|
return "/";
|
||||||
}
|
}
|
||||||
|
|
||||||
@RequestMapping("/user/login")
|
@RequestMapping("/user/login")
|
||||||
public String login(LoginDto dto){
|
public String login(LoginDto dto){
|
||||||
System.out.println("login()함수 호출");
|
System.out.println("login()함수 호출");
|
||||||
|
System.out.println(dto);
|
||||||
return "/";
|
return "/";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -7,6 +7,7 @@ import javax.servlet.*;
|
|||||||
import javax.servlet.http.HttpServletRequest;
|
import javax.servlet.http.HttpServletRequest;
|
||||||
import javax.servlet.http.HttpServletResponse;
|
import javax.servlet.http.HttpServletResponse;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
import java.io.PrintWriter;
|
||||||
import java.lang.annotation.Annotation;
|
import java.lang.annotation.Annotation;
|
||||||
import java.lang.reflect.InvocationTargetException;
|
import java.lang.reflect.InvocationTargetException;
|
||||||
import java.lang.reflect.Method;
|
import java.lang.reflect.Method;
|
||||||
@@ -15,6 +16,8 @@ import java.util.Enumeration;
|
|||||||
|
|
||||||
public class Dispatcher implements Filter {
|
public class Dispatcher implements Filter {
|
||||||
|
|
||||||
|
private boolean isMatching = false;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain)
|
public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain)
|
||||||
throws ServletException, IOException {
|
throws ServletException, IOException {
|
||||||
@@ -55,6 +58,7 @@ public class Dispatcher implements Filter {
|
|||||||
// System.out.println(requestMapping.value());
|
// System.out.println(requestMapping.value());
|
||||||
|
|
||||||
if(requestMapping.value().equals(endPoint)) {
|
if(requestMapping.value().equals(endPoint)) {
|
||||||
|
isMatching = true;
|
||||||
try {
|
try {
|
||||||
Parameter[] parameters = method.getParameters();
|
Parameter[] parameters = method.getParameters();
|
||||||
String path = null;
|
String path = null;
|
||||||
@@ -64,10 +68,10 @@ public class Dispatcher implements Filter {
|
|||||||
// String username = request.getParameter("username");
|
// String username = request.getParameter("username");
|
||||||
// String password = request.getParameter("password");
|
// String password = request.getParameter("password");
|
||||||
// System.out.println(username + ", " + password);
|
// System.out.println(username + ", " + password);
|
||||||
Enumeration<String> keys = request.getParameterNames(); // username, password
|
|
||||||
// keys 값을 parameterName -> setParameterName 으로 변경
|
|
||||||
|
|
||||||
path = "/";
|
// keys 값을 parameterName -> setParameterName 으로 변경
|
||||||
|
setData(dtoInstance, request);
|
||||||
|
path = (String) method.invoke(userController, dtoInstance);
|
||||||
} else {
|
} else {
|
||||||
path = (String) method.invoke(userController);
|
path = (String) method.invoke(userController);
|
||||||
}
|
}
|
||||||
@@ -80,5 +84,39 @@ public class Dispatcher implements Filter {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if(!isMatching) {
|
||||||
|
response.setContentType("text/html; charset=utf-8");
|
||||||
|
PrintWriter out = response.getWriter();
|
||||||
|
out.println("잘못된 주소 요청입니다. 404 에러");
|
||||||
|
out.flush();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private <T> void setData(T dtoInstance, HttpServletRequest request) {
|
||||||
|
Enumeration<String> keys = request.getParameterNames();
|
||||||
|
while(keys.hasMoreElements()) {
|
||||||
|
String key = (String) keys.nextElement();
|
||||||
|
String methodKey = keyToMethodKey(key);
|
||||||
|
|
||||||
|
Method[] methods = dtoInstance.getClass().getDeclaredMethods();
|
||||||
|
for (Method method : methods) {
|
||||||
|
if(method.getName().equals(methodKey)) {
|
||||||
|
try {
|
||||||
|
method.invoke(dtoInstance, request.getParameter(key));
|
||||||
|
} catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
} finally {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private String keyToMethodKey(String key) {
|
||||||
|
String firstKey = "set";
|
||||||
|
String upperKey = key.substring(0,1).toUpperCase();
|
||||||
|
String remainKey = key.substring(1);
|
||||||
|
return firstKey + upperKey + remainKey;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
17
reflect/src/test/java/SetNamingTest.java
Normal file
17
reflect/src/test/java/SetNamingTest.java
Normal file
@@ -0,0 +1,17 @@
|
|||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
public class SetNamingTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void 키값을세터로바꾸기() {
|
||||||
|
String key = "username";
|
||||||
|
|
||||||
|
String firstKey = "set";
|
||||||
|
String upperkey = key.substring(0,1).toUpperCase();
|
||||||
|
String remainKey = key.substring(1);
|
||||||
|
|
||||||
|
System.out.println(firstKey);
|
||||||
|
System.out.println(upperkey);
|
||||||
|
System.out.println(remainKey);
|
||||||
|
}
|
||||||
|
}
|
||||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Reference in New Issue
Block a user