This repository has been archived by the owner on Apr 16, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
mvc: Example for SimpleController interface, SimpleHandlerAdapter.
- Loading branch information
Showing
5 changed files
with
109 additions
and
0 deletions.
There are no files selected for viewing
54 changes: 54 additions & 0 deletions
54
mvc/src/main/java/com/dongho/config/mvc/adapter/SimpleHandlerAdapter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package com.dongho.config.mvc.adapter; | ||
|
||
import com.dongho.config.mvc.annotation.RequiredParams; | ||
import com.dongho.config.mvc.annotation.ViewName; | ||
import com.dongho.config.mvc.controller.SimpleController; | ||
import org.springframework.core.annotation.AnnotationUtils; | ||
import org.springframework.stereotype.Component; | ||
import org.springframework.util.ReflectionUtils; | ||
import org.springframework.web.servlet.HandlerAdapter; | ||
import org.springframework.web.servlet.ModelAndView; | ||
|
||
import javax.servlet.http.HttpServletRequest; | ||
import javax.servlet.http.HttpServletResponse; | ||
import java.lang.reflect.Method; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
@Component | ||
public class SimpleHandlerAdapter implements HandlerAdapter { | ||
|
||
@Override | ||
public boolean supports(Object handler) { | ||
return handler instanceof SimpleController; | ||
} | ||
|
||
@Override | ||
public ModelAndView handle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { | ||
Method m = ReflectionUtils.findMethod(handler.getClass(), "control", Map.class, Map.class); | ||
ViewName viewName = AnnotationUtils.getAnnotation(m, ViewName.class); | ||
RequiredParams requiredParams = AnnotationUtils.getAnnotation(m, RequiredParams.class); | ||
|
||
Map<String, String> params = new HashMap<>(); | ||
for (String param : requiredParams.value()) { | ||
String value = request.getParameter(param); | ||
|
||
if (value == null) { | ||
throw new IllegalStateException(); | ||
} | ||
|
||
params.put(param, value); | ||
} | ||
|
||
Map<String, Object> model = new HashMap<>(); | ||
SimpleController.class.cast(handler).control(params, model); | ||
|
||
return new ModelAndView(viewName.value(), model); | ||
} | ||
|
||
@Override | ||
public long getLastModified(HttpServletRequest httpServletRequest, Object o) { | ||
return -1; | ||
} | ||
|
||
} |
13 changes: 13 additions & 0 deletions
13
mvc/src/main/java/com/dongho/config/mvc/annotation/RequiredParams.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package com.dongho.config.mvc.annotation; | ||
|
||
import java.lang.annotation.Inherited; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
|
||
@Retention(RetentionPolicy.RUNTIME) | ||
@Inherited | ||
public @interface RequiredParams { | ||
|
||
String[] value(); | ||
|
||
} |
13 changes: 13 additions & 0 deletions
13
mvc/src/main/java/com/dongho/config/mvc/annotation/ViewName.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package com.dongho.config.mvc.annotation; | ||
|
||
import java.lang.annotation.Inherited; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
|
||
@Retention(RetentionPolicy.RUNTIME) | ||
@Inherited | ||
public @interface ViewName { | ||
|
||
String value(); | ||
|
||
} |
9 changes: 9 additions & 0 deletions
9
mvc/src/main/java/com/dongho/config/mvc/controller/SimpleController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package com.dongho.config.mvc.controller; | ||
|
||
import java.util.Map; | ||
|
||
public interface SimpleController { | ||
|
||
void control(Map<String, String> params, Map<String, Object> model); | ||
|
||
} |
20 changes: 20 additions & 0 deletions
20
mvc/src/main/java/com/dongho/web/HelloSimpleController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package com.dongho.web; | ||
|
||
import com.dongho.config.mvc.annotation.RequiredParams; | ||
import com.dongho.config.mvc.annotation.ViewName; | ||
import com.dongho.config.mvc.controller.SimpleController; | ||
import org.springframework.stereotype.Component; | ||
|
||
import java.util.Map; | ||
|
||
@Component("/simple-hello") | ||
public class HelloSimpleController implements SimpleController { | ||
|
||
@ViewName("/simple-hello") | ||
@RequiredParams({"name"}) | ||
@Override | ||
public void control(Map<String, String> params, Map<String, Object> model) { | ||
model.put("message", "Hello " + params.get("name")); | ||
} | ||
|
||
} |