HandlerAdapter.java 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. package cn.nosum.framework.mvc.v4.servlet;
  2. import cn.nosum.framework.annotation.RequestParam;
  3. import javax.servlet.http.HttpServletRequest;
  4. import javax.servlet.http.HttpServletResponse;
  5. import java.lang.annotation.Annotation;
  6. import java.util.Arrays;
  7. import java.util.HashMap;
  8. import java.util.Map;
  9. public class HandlerAdapter {
  10. public ModelAndView handler(HttpServletRequest req, HttpServletResponse resp, HandlerMapping handler) throws Exception{
  11. // 保存参数名称和参数的位置
  12. Map<String,Integer> paramIndexMapping = new HashMap<String, Integer>();
  13. // 通过运行时的状态取得方法参数的 annotation
  14. Annotation[] [] pa = handler.getMethod().getParameterAnnotations();
  15. for (int i = 0; i < pa.length ; i ++) {
  16. for(Annotation a : pa[i]){
  17. if(a instanceof RequestParam){
  18. String paramName = ((RequestParam) a).value();
  19. if(!"".equals(paramName.trim())){
  20. paramIndexMapping.put(paramName,i);
  21. }
  22. }
  23. }
  24. }
  25. // 初始化
  26. Class<?> [] paramTypes = handler.getMethod().getParameterTypes();
  27. for (int i = 0; i < paramTypes.length; i++) {
  28. Class<?> paramterType = paramTypes[i];
  29. if(paramterType == HttpServletRequest.class || paramterType == HttpServletResponse.class){
  30. paramIndexMapping.put(paramterType.getName(),i);
  31. }
  32. }
  33. // 拼接实参列表
  34. Map<String,String[]> params = req.getParameterMap();
  35. Object [] paramValues = new Object[paramTypes.length];
  36. for (Map.Entry<String,String[]> param : params.entrySet()) {
  37. String value = Arrays.toString(params.get(param.getKey()))
  38. .replaceAll("\\[|\\]","")
  39. .replaceAll("\\s+",",");
  40. if(!paramIndexMapping.containsKey(param.getKey())){continue;}
  41. int index = paramIndexMapping.get(param.getKey());
  42. // 允许自定义的类型转换器Converter
  43. paramValues[index] = castStringValue(value,paramTypes[index]);
  44. }
  45. if(paramIndexMapping.containsKey(HttpServletRequest.class.getName())){
  46. int index = paramIndexMapping.get(HttpServletRequest.class.getName());
  47. paramValues[index] = req;
  48. }
  49. if(paramIndexMapping.containsKey(HttpServletResponse.class.getName())){
  50. int index = paramIndexMapping.get(HttpServletResponse.class.getName());
  51. paramValues[index] = resp;
  52. }
  53. Object result = handler.getMethod().invoke(handler.getController(),paramValues);
  54. if(result == null || result instanceof Void){return null;}
  55. boolean isModelAndView = handler.getMethod().getReturnType() == ModelAndView.class;
  56. if(isModelAndView){
  57. return (ModelAndView)result;
  58. }
  59. return null;
  60. }
  61. private Object castStringValue(String value, Class<?> paramType) {
  62. if(String.class == paramType){
  63. return value;
  64. }else if(Integer.class == paramType){
  65. return Integer.valueOf(value);
  66. }else if(Double.class == paramType){
  67. return Double.valueOf(value);
  68. }else {
  69. if(value != null){
  70. return value;
  71. }
  72. return null;
  73. }
  74. }
  75. }