Skip to content
This repository has been archived by the owner on Apr 16, 2024. It is now read-only.

Commit

Permalink
mvc: Example for SimpleController interface, SimpleHandlerAdapter.
Browse files Browse the repository at this point in the history
  • Loading branch information
dhsim86 committed Dec 22, 2018
1 parent 48f2326 commit 8eccfb8
Show file tree
Hide file tree
Showing 5 changed files with 109 additions and 0 deletions.
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;
}

}
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 mvc/src/main/java/com/dongho/config/mvc/annotation/ViewName.java
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();

}
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 mvc/src/main/java/com/dongho/web/HelloSimpleController.java
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"));
}

}

0 comments on commit 8eccfb8

Please sign in to comment.