这里续写上一章博客(110章博客):   
现在我们来学习一下高级的技术,前面的mvc知识,我们基本可以在67章博客及其后面相关的博客可以学习到,现在开始学习精髓:   
 
 
 
 < packaging> </ packaging> < dependencies> < dependency> < groupId> </ groupId> < artifactId> </ artifactId> < version> </ version> </ dependency> < dependency> < groupId> </ groupId> < artifactId> </ artifactId> < version> </ version> </ dependency> < dependency> < groupId> </ groupId> < artifactId> </ artifactId> < version> </ version> </ dependency> < dependency> < groupId> </ groupId> < artifactId> </ artifactId> < version> </ version> </ dependency> < dependency> < groupId> </ groupId> < artifactId> </ artifactId> < version> </ version> </ dependency> </ dependencies> <?xml version="1.0" encoding="UTF-8"?> 
< web-appxmlns = " http://xmlns.jcp.org/xml/ns/javaee" xmlns: xsi= " http://www.w3.org/2001/XMLSchema-instance" xsi: schemaLocation= " http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" version = " 4.0" > < servlet> < servlet-name> </ servlet-name> < servlet-class> </ servlet-class> < init-param> < param-name> </ param-name> < param-value> </ param-value> </ init-param> </ servlet> < servlet-mapping> < servlet-name> </ servlet-name> < url-pattern> </ url-pattern> </ servlet-mapping> </ web-app> <%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head><title>Title</title>
</head>
<body>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<button id="btn">ajax提交</button>
<script>$("#btn").click(function () {let url = 'test/in';let data = '[{"id":1,"username":"张三"},{"id":2,"username":"李四"}]';$.ajax({type: 'POST',//大小写可以忽略url: url,data: data,contentType: 'application/json;charset=utf-8',success: function (data) {console.log(data);alert(data)}})})
</script>
</body>
</html>< beansxmlns = " http://www.springframework.org/schema/beans" xmlns: mvc= " http://www.springframework.org/schema/mvc" xmlns: context= " http://www.springframework.org/schema/context" xmlns: xsi= " http://www.w3.org/2001/XMLSchema-instance" xsi: schemaLocation= " http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsdhttp://www.springframework.org/schema/mvchttp://www.springframework.org/schema/mvc/spring-mvc.xsd" > < context: component-scanbase-package = " com.controller" /> < mvc: annotation-driven> </ mvc: annotation-driven> </ beans> package  com. entity ; public  class  User  { String  id; String  username; public  String  getId ( )  { return  id; } public  void  setId ( String  id)  { this . id =  id; } public  String  getUsername ( )  { return  username; } public  void  setUsername ( String  username)  { this . username =  username; } @Override public  String  toString ( )  { return  "User{"  + "id='"  +  id +  '\''  + ", username='"  +  username +  '\''  + '}' ; } 
} package  com. controller ; import  com. entity.  User ; 
import  org. springframework. stereotype.  Controller ; 
import  org. springframework. web. bind. annotation.  RequestBody ; 
import  org. springframework. web. bind. annotation.  RequestMapping ; 
import  org. springframework. web. bind. annotation.  ResponseBody ; import  java. util.  List ; @Controller 
@RequestMapping ( "/test" ) 
public  class  test { @RequestMapping ( "in" ) @ResponseBody public  List < User > ajax ( @RequestBody  List < User > )  { System . out. println ( list) ; return  list; } 
} package  com.  Interceptor ; import  org. springframework. web. servlet.  HandlerInterceptor ; public  class  MyInterceptor  implements  HandlerInterceptor  { 
} 
package  org. springframework. web. servlet ; import  javax. servlet. http.  HttpServletRequest ; 
import  javax. servlet. http.  HttpServletResponse ; 
import  org. springframework. lang.  Nullable ; public  interface  HandlerInterceptor  { default  boolean  preHandle ( HttpServletRequest  request,  HttpServletResponse  response,  Object  handler)  throws  Exception  { return  true ;  } default  void  postHandle ( HttpServletRequest  request,  HttpServletResponse  response,  Object  handler,  @Nullable  ModelAndView  modelAndView)  throws  Exception  { } default  void  afterCompletion ( HttpServletRequest  request,  HttpServletResponse  response,  Object  handler,  @Nullable  Exception  ex)  throws  Exception  { } 
} package  com.  Interceptor ; import  org. springframework. web. servlet.  HandlerInterceptor ; 
import  org. springframework. web. servlet.  ModelAndView ; import  javax. servlet. http.  HttpServletRequest ; 
import  javax. servlet. http.  HttpServletResponse ; public  class  MyInterceptor  implements  HandlerInterceptor  { @Override public  boolean  preHandle ( HttpServletRequest  request,  HttpServletResponse  response,  Object  handler)  throws  Exception  { System . out. println ( handler) ; System . out. println ( "Handler业务逻辑执行之前拦截一次,我是第一次" ) ; return  true ; } @Override public  void  postHandle ( HttpServletRequest  request,  HttpServletResponse  response,  Object  handler,  ModelAndView  modelAndView)  throws  Exception  { System . out. println ( handler) ; System . out. println ( modelAndView) ; System . out. println ( "在Handler逻辑执行完毕但未跳转页面之前拦截一次,我是第二次" ) ; } @Override public  void  afterCompletion ( HttpServletRequest  request,  HttpServletResponse  response,  Object  handler,  Exception  ex)  throws  Exception  { System . out. println ( handler) ; System . out. println ( ex) ; System . out. println ( "在跳转页面之后拦截一次,我是第三次" ) ; } 
}  < mvc: interceptors> < mvc: interceptor> < mvc: mappingpath = " /**" /> < beanclass = " com.Interceptor.MyInterceptor" > </ bean> </ mvc: interceptor> </ mvc: interceptors>    < mvc: interceptors> < beanclass = " com.Interceptor.MyInterceptor" > </ bean> </ mvc: interceptors>  < mvc: interceptors> < mvc: interceptor> < mvc: mappingpath = " /**" /> < beanclass = " com.Interceptor.MyInterceptor" > </ bean> </ mvc: interceptor> </ mvc: interceptors>   < mvc: interceptors> < mvc: interceptor> < mvc: mappingpath = " /**" /> < mvc: exclude-mappingpath = " /demo/**" /> < beanclass = " com.Interceptor.MyInterceptor" > </ bean> </ mvc: interceptor> </ mvc: interceptors> 
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head><title>Title</title>
</head>
<body>
<%System.out.println(1);
%>
1
</script>
</body>
</html><%System.out.println(1);
%>
< beanid = " viewResolver" class = " org.springframework.web.servlet.view.InternalResourceViewResolver" > < propertyname = " prefix" value = " /" > </ property> < propertyname = " suffix" value = " .jsp" > </ property> </ bean>  @RequestMapping ( "ix" ) public  String  ix ( )  { return  "test" ; } 
public  String  ix ( String  a, Integer  b)  { 
  @RequestMapping ( "ix" ) public  void  ix ( String  a, Integer  b)  { } 
 @RequestMapping ( "ix" ) public  String  ix ( String  a,  Integer  b)  throws  IOException  { return  null ; } 
@RequestMapping ( "ix" ) public  void  ix ( String  a,  Integer  b,  HttpServletResponse  mm)  throws  IOException  { mm. setContentType ( "text/html;charset=utf-8" ) ; PrintWriter  writer =  mm. getWriter ( ) ; writer. println ( "哈哈哈" ) ; } 
 @RequestMapping ( "ix" ) public  void  ix ( String  a,  Integer  b,  HttpServletResponse  mm)  throws  IOException  { 
} 
 
 
 
 
package  com.  Interceptor ; import  org. springframework. web. servlet.  HandlerInterceptor ; 
import  org. springframework. web. servlet.  ModelAndView ; import  javax. servlet. http.  HttpServletRequest ; 
import  javax. servlet. http.  HttpServletResponse ; public  class  MyInterceptor1  implements  HandlerInterceptor  { @Override public  boolean  preHandle ( HttpServletRequest  request,  HttpServletResponse  response,  Object handler)  throws  Exception  { System . out. println ( "preHandle1...." ) ; return  true ; } @Override public  void  postHandle ( HttpServletRequest  request,  HttpServletResponse  response,  Object  handler, ModelAndView  modelAndView)  throws  Exception  { System . out. println ( "postHandle1...." ) ; } @Override public  void  afterCompletion ( HttpServletRequest  request,  HttpServletResponse  response,  Object handler,  Exception  ex)  throws  Exception  { System . out. println ( "afterCompletion1...." ) ; } 
} package  com.  Interceptor ; import  org. springframework. web. servlet.  HandlerInterceptor ; 
import  org. springframework. web. servlet.  ModelAndView ; import  javax. servlet. http.  HttpServletRequest ; 
import  javax. servlet. http.  HttpServletResponse ; public  class  MyInterceptor2  implements  HandlerInterceptor  { @Override public  boolean  preHandle ( HttpServletRequest  request,  HttpServletResponse  response,  Object handler)  throws  Exception  { System . out. println ( "preHandle2...." ) ; return  true ; } @Override public  void  postHandle ( HttpServletRequest  request,  HttpServletResponse  response,  Object  handler, ModelAndView  modelAndView)  throws  Exception  { System . out. println ( "postHandle2...." ) ; } @Override public  void  afterCompletion ( HttpServletRequest  request,  HttpServletResponse  response,  Object handler,  Exception  ex)  throws  Exception  { System . out. println ( "afterCompletion2...." ) ; } 
} 
< mvc: interceptors> < mvc: interceptor> < mvc: mappingpath = " /**" /> < beanclass = " com.Interceptor.MyInterceptor1" > </ bean> </ mvc: interceptor> < mvc: interceptor> < mvc: mappingpath = " /**" /> < beanclass = " com.Interceptor.MyInterceptor2" > </ bean> </ mvc: interceptor> </ mvc: interceptors> < beanid = " viewResolver" class = " org.springframework.web.servlet.view.InternalResourceViewResolver" > < propertyname = " prefix" value = " /" > </ property> < propertyname = " suffix" value = " .jsp" > </ property> </ bean> @RequestMapping ( "ix" ) public  String  ix ( String  a,  Integer  b)  throws  IOException  { return  "test" ; } 
 < mvc: interceptors> < mvc: interceptor> < mvc: mappingpath = " /**" /> < beanclass = " com.Interceptor.MyInterceptor2" > </ bean> </ mvc: interceptor> < mvc: interceptor> < mvc: mappingpath = " /**" /> < beanclass = " com.Interceptor.MyInterceptor1" > </ bean> </ mvc: interceptor> </ mvc: interceptors> 
package  com.  Interceptor ; import  org. springframework. web. servlet.  HandlerInterceptor ; 
import  org. springframework. web. servlet.  ModelAndView ; import  javax. servlet. http.  HttpServletRequest ; 
import  javax. servlet. http.  HttpServletResponse ; public  class  MyInterceptor3  implements  HandlerInterceptor  { @Override public  boolean  preHandle ( HttpServletRequest  request,  HttpServletResponse  response,  Object handler)  throws  Exception  { System . out. println ( "preHandle3...." ) ; return  false ; } @Override public  void  postHandle ( HttpServletRequest  request,  HttpServletResponse  response,  Object  handler, ModelAndView  modelAndView)  throws  Exception  { System . out. println ( "postHandle3...." ) ; } @Override public  void  afterCompletion ( HttpServletRequest  request,  HttpServletResponse  response,  Object handler,  Exception  ex)  throws  Exception  { System . out. println ( "afterCompletion3...." ) ; } 
} 
 < mvc: interceptors> < mvc: interceptor> < mvc: mappingpath = " /**" /> < beanclass = " com.Interceptor.MyInterceptor1" > </ bean> </ mvc: interceptor> < mvc: interceptor> < mvc: mappingpath = " /**" /> < beanclass = " com.Interceptor.MyInterceptor3" > </ bean> </ mvc: interceptor> < mvc: interceptor> < mvc: mappingpath = " /**" /> < beanclass = " com.Interceptor.MyInterceptor2" > </ bean> </ mvc: interceptor> </ mvc: interceptors> 
 < mvc: interceptors> < mvc: interceptor> < mvc: mappingpath = " /**" /> < beanclass = " com.Interceptor.MyInterceptor1" > </ bean> </ mvc: interceptor> < mvc: interceptor> < mvc: mappingpath = " /**" /> < beanclass = " com.Interceptor.MyInterceptor2" > </ bean> </ mvc: interceptor> < mvc: interceptor> < mvc: mappingpath = " /**" /> < beanclass = " com.Interceptor.MyInterceptor3" > </ bean> </ mvc: interceptor> </ mvc: interceptors> 
 < dependency> < groupId> </ groupId> < artifactId> </ artifactId> < version> </ version> </ dependency> < beanid = " multipartResolver" class = " org.springframework.web.multipart.commons.CommonsMultipartResolver" > < propertyname = " maxUploadSize" value = " 1000000000" /> </ bean> <%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head><title>Title</title>
</head>
<body>
<form method="post" enctype="multipart/form-data" action="demo/upload"><input type="file" name="uploadFile"/><input type="submit" value="上传"/>
</form>
</body>
</html>package  com. controller ; import  org. springframework. stereotype.  Controller ; 
import  org. springframework. web. bind. annotation.  RequestMapping ; 
import  org. springframework. web. multipart.  MultipartFile ; import  javax. servlet. http.  HttpServletRequest ; 
import  java. io.  File ; 
import  java. io.  IOException ; 
import  java. text.  SimpleDateFormat ; 
import  java. util.  Date ; 
import  java. util.  UUID ; @Controller 
@RequestMapping ( "/demo" ) 
public  class  FileController  { @RequestMapping ( "upload" ) public  String  upload ( MultipartFile  uploadFile,  HttpServletRequest  request)  throws  IOException  { String  originalFilename =  uploadFile. getOriginalFilename ( ) ; String  extendName = originalFilename. substring ( originalFilename. lastIndexOf ( "." )  +  1 ,  originalFilename. length ( ) ) ; String  uuid =  UUID . randomUUID ( ) . toString ( ) ; String  newName =  uuid +  "."  +  extendName; String  realPath = request. getSession ( ) . getServletContext ( ) . getRealPath ( "/" ) ; System . out. println ( realPath) ; String  datePath =  new  SimpleDateFormat ( "yyyy-MM-dd" ) . format ( new  Date ( ) ) ; File  floder =  new  File ( realPath +  "/"  +  datePath) ; if  ( ! floder. exists ( ) )  {   floder. mkdirs ( ) ; } uploadFile. transferTo ( new  File ( floder,  newName) ) ; return  "success" ; } 
} <%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head><title>Title</title>
</head>
<body>
1
</body>
</html>
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
<?xml version="1.0" encoding="UTF-8"?> 
< web-appxmlns = " http://xmlns.jcp.org/xml/ns/javaee" xmlns: xsi= " http://www.w3.org/2001/XMLSchema-instance" xsi: schemaLocation= " http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" version = " 4.0" > < servlet> < servlet-name> </ servlet-name> < servlet-class> </ servlet-class> </ servlet> < servlet-mapping> < servlet-name> </ servlet-name> < url-pattern> </ url-pattern> </ servlet-mapping> </ web-app>   @Override public  void  service ( ServletRequest  servletRequest,  ServletResponse  servletResponse)  throws  ServletException ,  IOException  { String  realPath =  servletRequest. getServletContext ( ) . getRealPath ( "/" ) ; System . out. println ( realPath) ; } 
F : \xianmu\out\artifacts\xianmu_Web_exploded\
C : \Users \33988 \AppData \Local \JetBrains \IntelliJIdea2021 .3 \tomcat\062 b022d- 1657 - 4d 8 b- b4e9- da3eeae3d227\conf\Catalina \localhost
 
 
 
<?xml version="1.0" encoding="UTF-8"?> 
< projectxmlns = " http://maven.apache.org/POM/4.0.0" xmlns: xsi= " http://www.w3.org/2001/XMLSchema-instance" xsi: schemaLocation= " http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" > < modelVersion> </ modelVersion> < groupId> </ groupId> < artifactId> </ artifactId> < version> </ version> < properties> < maven.compiler.source> </ maven.compiler.source> < maven.compiler.target> </ maven.compiler.target> </ properties> </ project> 
 
< packaging> </ packaging> 
 
 
  < dependencies> < dependency> < groupId> </ groupId> < artifactId> </ artifactId> < version> </ version> < scope> </ scope> </ dependency> </ dependencies> 
 
 
< Contextpath = " /maven" docBase = " F:\maven\target\maven-1.0-SNAPSHOT.war" /> F : \Program  Files \apache- tomcat- 8.5 .50 \webapps\maven\ 
< Contextpath = " /maven" docBase = " F:\maven\target\maven-1.0-SNAPSHOT" /> F:\maven\target\maven-1.0-SNAPSHOT\
<Context path="/xianmu" docBase="F:\xianmu\out\artifacts\xianmu_Web_exploded" />
F:\xianmu\out\artifacts\xianmu_Web_exploded\
String  realPath =  servletRequest. getServletContext ( ) . getRealPath ( "/" ) ; 
String  realPath =  servletRequest. getServletContext ( ) . getRealPath ( "" ) ; 
package  com. controller ; import  org. springframework. stereotype.  Controller ; 
import  org. springframework. web. bind. annotation.  RequestMapping ; 
import  org. springframework. web. multipart.  MultipartFile ; import  javax. servlet. http.  HttpServletRequest ; 
import  java. io.  File ; 
import  java. io.  IOException ; 
import  java. text.  SimpleDateFormat ; 
import  java. util.  Date ; 
import  java. util.  UUID ; @Controller 
@RequestMapping ( "/demo" ) 
public  class  FileController  { @RequestMapping ( "upload" ) public  String  upload ( MultipartFile  uploadFile,  HttpServletRequest  request)  throws  IOException  { String  originalFilename =  uploadFile. getOriginalFilename ( ) ; String  extendName = originalFilename. substring ( originalFilename. lastIndexOf ( "." )  +  1 ,  originalFilename. length ( ) ) ; String  uuid =  UUID . randomUUID ( ) . toString ( ) ; String  newName =  uuid +  "."  +  extendName; String  realPath = request. getSession ( ) . getServletContext ( ) . getRealPath ( "/" ) ; System . out. println ( realPath) ; String  datePath =  new  SimpleDateFormat ( "yyyy-MM-dd" ) . format ( new  Date ( ) ) ; File  floder =  new  File ( realPath +  "/"  +  datePath) ; if  ( ! floder. exists ( ) )  {   floder. mkdirs ( ) ; } uploadFile. transferTo ( new  File ( floder,  newName) ) ; return  "success" ; } 
} 
package  com. controller ; import  org. springframework. stereotype.  Controller ; 
import  org. springframework. web. bind. annotation.  ControllerAdvice ; 
import  org. springframework. web. bind. annotation.  ExceptionHandler ; 
import  org. springframework. web. bind. annotation.  RequestMapping ; 
import  org. springframework. web. servlet.  ModelAndView ; import  javax. servlet. http.  HttpServletResponse ; 
import  java. io.  IOException ; 
@Controller 
public  class  GlobalExceptionResolver  { @ExceptionHandler ( ArithmeticException . class ) public  ModelAndView  handleException ( ArithmeticException  exception,  HttpServletResponse  response)  { ModelAndView  modelAndView =  new  ModelAndView ( ) ; modelAndView. addObject ( "msg" ,  exception. getMessage ( ) ) ; modelAndView. setViewName ( "error" ) ; return  modelAndView; } @RequestMapping ( "err" ) public  String  ix ( String  a,  Integer  b)  throws  IOException  { System . out. println ( 1 ) ; int  i =  1  /  0 ; return  "err" ; } 
} <%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head><title>Title</title>
</head>
<body>
错误是:${msg}
</body>
</html>modelAndView. addObject ( "msg" ,  exception. getMessage ( ) ) ; 
modelAndView. setViewName ( "error" ) ; 
 @ExceptionHandler ( ArithmeticException . class ) public  ModelAndView  handleException ( ArithmeticException  exception,  HttpServletResponse  response)  { ModelAndView  modelAndView =  new  ModelAndView ( ) ; modelAndView. addObject ( "msg" ,  exception. getMessage ( ) ) ; modelAndView. setViewName ( "error" ) ; return  modelAndView; } @ExceptionHandler ( ArithmeticException . class ) public  ModelAndView  handleException1 ( ArithmeticException  exception,  HttpServletResponse  response)  { ModelAndView  modelAndView =  new  ModelAndView ( ) ; modelAndView. addObject ( "msg" ,  exception. getMessage ( ) + "22" ) ; modelAndView. setViewName ( "error" ) ; return  modelAndView; } 
package  com. controller ; import  org. springframework. stereotype.  Controller ; 
import  org. springframework. web. bind. annotation.  ExceptionHandler ; 
import  org. springframework. web. servlet.  ModelAndView ; import  javax. servlet. http.  HttpServletResponse ; @Controller 
public  class  Global  { @ExceptionHandler ( ArithmeticException . class ) public  ModelAndView  handleException ( ArithmeticException  exception,  HttpServletResponse  response)  { ModelAndView  modelAndView =  new  ModelAndView ( ) ; modelAndView. addObject ( "msg" ,  exception. getMessage ( ) ) ; modelAndView. setViewName ( "error" ) ; return  modelAndView; } 
} package  com. controller ; import  org. springframework. stereotype.  Controller ; 
import  org. springframework. web. bind. annotation.  ControllerAdvice ; 
import  org. springframework. web. bind. annotation.  ExceptionHandler ; 
import  org. springframework. web. servlet.  ModelAndView ; import  javax. servlet. http.  HttpServletResponse ; @ControllerAdvice 
@Controller 
public  class  Global  { @ExceptionHandler ( ArithmeticException . class ) public  ModelAndView  handleException ( ArithmeticException  exception,  HttpServletResponse  response)  { ModelAndView  modelAndView =  new  ModelAndView ( ) ; modelAndView. addObject ( "msg" ,  exception. getMessage ( ) ) ; modelAndView. setViewName ( "error" ) ; return  modelAndView; } 
} 
 
package  com. controller ; import  org. springframework. stereotype.  Controller ; 
import  org. springframework. web. bind. annotation.  RequestMapping ; @Controller 
public  class  Redirect  { @RequestMapping ( "/re" ) public  String  re ( )  { return  "redirect:ree?name="  +  1 ; 
} @RequestMapping ( "/ree" ) public  String  ree ( String  name)  { System . out. println ( name) ; System . out. println ( 1 ) ; return  "ree" ; } 
}  @RequestMapping ( "/reee" ) public  String  reee ( RedirectAttributes  redirectAttributes, HttpSession  session)  { redirectAttributes. addFlashAttribute ( "name" ,  "22" ) ; Object  name =  session. getAttribute ( "name" ) ; System . out. println ( name) ;  return  "redirect:reeee?name="  +  1 ; } @RequestMapping ( "/reeee" ) 
public  String  reeee ( String  name,  HttpSession  session,  @ModelAttribute ( "name" )  String  na,  ModelAndView  modelAndView)  { System . out. println ( modelAndView) ; System . out. println ( na) ;  String  name2 =  ( String )  session. getAttribute ( "name" ) ; System . out. println ( name2) ;  System . out. println ( 44444444 ) ; System . out. println ( name) ; Object  name1 =  session. getAttribute ( "name" ) ; System . out. println ( name1) ; return  "ree" ; } 
 
 
< web-app> </ web-app>    < dependencies> < dependency> < groupId> </ groupId> < artifactId> </ artifactId> < version> </ version> < scope> </ scope> </ dependency> </ dependencies> package  com. mvc. framework ; import  javax. servlet. http.  HttpServlet ; 
public  class  DispatcherServlet  extends  HttpServlet  { 
} <?xml version="1.0" encoding="UTF-8"?> 
< web-appxmlns = " http://xmlns.jcp.org/xml/ns/javaee" xmlns: xsi= " http://www.w3.org/2001/XMLSchema-instance" xsi: schemaLocation= " http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" version = " 4.0" > < display-name> </ display-name> < servlet> < servlet-name> </ servlet-name> < servlet-class> </ servlet-class> </ servlet> < servlet-mapping> < servlet-name> </ servlet-name> < url-pattern> </ url-pattern> </ servlet-mapping> </ web-app> package  com. mvc. framework. annotations ; import  java. lang. annotation.  * ; @Documented 
@Target ( ElementType . TYPE ) 
@Retention ( RetentionPolicy . RUNTIME ) 
public  @interface  Controller  { String  value ( )  default  "" ; 
} package  com. mvc. framework. annotations ; import  java. lang. annotation.  * ; @Documented 
@Target ( ElementType . TYPE ) 
@Retention ( RetentionPolicy . RUNTIME ) 
public  @interface  Service  { String  value ( )  default  "" ; 
} package  com. mvc. framework. annotations ; import  java. lang. annotation.  * ; @Documented 
@Target ( { ElementType . TYPE , ElementType . METHOD } ) 
@Retention ( RetentionPolicy . RUNTIME ) 
public  @interface  RequestMapping  { String  value ( )  default  "" ; 
} package  com. mvc. framework. annotations ; import  java. lang. annotation.  * ; @Documented 
@Target ( ElementType . FIELD  ) 
@Retention ( RetentionPolicy . RUNTIME ) 
public  @interface  Autowired  { String  value ( )  default  "" ; 
} package  com. mvc. framework. service ; public  interface  DemoService  { String  get ( String  name) ; 
} package  com. mvc. framework. service. impl ; import  com. mvc. framework. service.  DemoService ; public  class  DemoServiceImpl  implements  DemoService  { @Override public  String  get ( String  name)  { System . out. println ( "打印:" + name) ; return  name; } 
} package  com. mvc. framework. servlet ; import  javax. servlet.  ServletException ; 
import  javax. servlet. http.  HttpServlet ; 
import  javax. servlet. http.  HttpServletRequest ; 
import  javax. servlet. http.  HttpServletResponse ; 
import  java. io.  IOException ; 
public  class  DispatcherServlet  extends  HttpServlet  { @Override public  void  init ( ServletConfig  config)  throws  ServletException  { } @Override protected  void  doGet ( HttpServletRequest  req,  HttpServletResponse  resp)  throws  ServletException ,  IOException  { doPost ( req, resp) ; } @Override protected  void  doPost ( HttpServletRequest  req,  HttpServletResponse  resp)  throws  ServletException ,  IOException  { } 
}  @Override public  void  init ( ServletConfig  config)  throws  ServletException  { } 
< beans> < component-scanbase-package = " com.mvc.framework" /> </ beans> <?xml version="1.0" encoding="UTF-8"?> 
< web-appxmlns = " http://xmlns.jcp.org/xml/ns/javaee" xmlns: xsi= " http://www.w3.org/2001/XMLSchema-instance" xsi: schemaLocation= " http://xmlns.jcp.org/xml/ns/javaeehttp://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" version = " 4.0" > < display-name> </ display-name> < servlet> < servlet-name> </ servlet-name> < servlet-class> </ servlet-class> < init-param> < param-name> </ param-name> < param-value> </ param-value> </ init-param> </ servlet> < servlet-mapping> < servlet-name> </ servlet-name> < url-pattern> </ url-pattern> </ servlet-mapping> </ web-app> package  com. mvc. framework. servlet ; import  javax. servlet.  ServletConfig ; 
import  javax. servlet.  ServletException ; 
import  javax. servlet. http.  HttpServlet ; 
import  javax. servlet. http.  HttpServletRequest ; 
import  javax. servlet. http.  HttpServletResponse ; 
import  java. io.  IOException ; 
public  class  DispatcherServlet  extends  HttpServlet  { @Override public  void  init ( ServletConfig  config)  throws  ServletException  { String  contextConfigLocation =  config. getInitParameter ( "contextConfigLocation" ) ; String  s =  doLoadconfig ( contextConfigLocation) ; doScan ( s) ; doInstance ( ) ; doAutoWired ( ) ; initHandlerMapping ( ) ; System . out. println ( "初始化完成...,等待请求与映射匹配了" ) ; } private  void  initHandlerMapping ( )  { } private  void  doAutoWired ( )  { } private  void  doInstance ( )  { } private  void  doScan ( String  path)  { } private  String  doLoadconfig ( String  contextConfigLocation)  { return  "" ; } @Override protected  void  doGet ( HttpServletRequest  req,  HttpServletResponse  resp)  throws  ServletException ,  IOException  { doPost ( req,  resp) ; } @Override protected  void  doPost ( HttpServletRequest  req,  HttpServletResponse  resp)  throws  ServletException ,  IOException  { } 
}  < dependency> < groupId> </ groupId> < artifactId> </ artifactId> < version> </ version> </ dependency> < dependency> < groupId> </ groupId> < artifactId> </ artifactId> < version> </ version> </ dependency> package  com. mvc. framework. controller ; import  com. mvc. framework. annotations.  Autowired ; 
import  com. mvc. framework. annotations.  Controller ; 
import  com. mvc. framework. annotations.  RequestMapping ; 
import  com. mvc. framework. service.  DemoService ; import  javax. servlet. http.  HttpServletRequest ; 
import  javax. servlet. http.  HttpServletResponse ; @Controller 
@RequestMapping ( "/demo" ) 
public  class  DemoController  { @Autowired private  DemoService  demoService; @RequestMapping ( "/query" ) public  String  query ( HttpServletRequest  request,  HttpServletResponse  response,  String  name)  { String  s =  demoService. get ( name) ; return  s; } } package  com. mvc. framework. servlet ; import  com. mvc. framework. annotations.  Autowired ; 
import  com. mvc. framework. annotations.  Controller ; 
import  com. mvc. framework. annotations.  RequestMapping ; 
import  com. mvc. framework. annotations.  Service ; 
import  org. dom4j.  Document ; 
import  org. dom4j.  Element ; 
import  org. dom4j. io.  SAXReader ; import  javax. servlet.  ServletConfig ; 
import  javax. servlet.  ServletException ; 
import  javax. servlet. http.  HttpServlet ; 
import  javax. servlet. http.  HttpServletRequest ; 
import  javax. servlet. http.  HttpServletResponse ; 
import  java. io.  File ; 
import  java. io.  IOException ; 
import  java. io.  InputStream ; 
import  java. lang. reflect.  Field ; 
import  java. lang. reflect.  Method ; 
import  java. net.  URLDecoder ; 
import  java. nio. charset.  StandardCharsets ; 
import  java. util.  ArrayList ; 
import  java. util.  HashMap ; 
import  java. util.  List ; 
import  java. util.  Map ; 
public  class  DispatcherServlet  extends  HttpServlet  { private  static  List < String > =  new  ArrayList < > ( ) ; private  static  Map < String ,  Object > =  new  HashMap < > ( ) ; private  static  List < String > =  new  ArrayList < > ( ) ; private  static  Map < String ,  Method > =  new  HashMap < > ( ) ; @Override public  void  init ( ServletConfig  config)  { String  contextConfigLocation =  config. getInitParameter ( "contextConfigLocation" ) ; String  s =  doLoadconfig ( contextConfigLocation) ; doScan ( s) ; doInstance ( ) ; doAutoWired ( ) ; initHandlerMapping ( ) ; System . out. println ( "初始化完成...,等待请求与映射匹配了" ) ; } private  void  initHandlerMapping ( )  { if  ( map. isEmpty ( ) )  {  return ; } for  ( Map. Entry < String ,  Object > :  map. entrySet ( ) )  { Class < ? > =  entry. getValue ( ) . getClass ( ) ; if  ( aClass. isAnnotationPresent ( Controller . class ) )  { String  baseUrl =  "" ; if  ( aClass. isAnnotationPresent ( RequestMapping . class ) )  { String  value =  aClass. getAnnotation ( RequestMapping . class ) . value ( ) ; baseUrl +=  value;  } Method [ ]  methods =  aClass. getMethods ( ) ; for  ( int  j =  0 ;  j <  methods. length;  j++ )  { Method  method =  methods[ j] ; if  ( method. isAnnotationPresent ( RequestMapping . class ) )  { RequestMapping  annotation =  method. getAnnotation ( RequestMapping . class ) ; String  value =  annotation. value ( ) ;  String  url =  baseUrl; url +=  value; handlerMapping. put ( url,  method) ; } } } } } private  void  doAutoWired ( )  { if  ( map. isEmpty ( ) )  {  return ; } for  ( Map. Entry < String ,  Object > :  map. entrySet ( ) )  { try  { doObjectDependancy ( entry. getValue ( ) ) ; }  catch  ( Exception  e)  { e. printStackTrace ( ) ; } } } private  static  void  doObjectDependancy ( Object  object)  { Field [ ]  declaredFields =  object. getClass ( ) . getDeclaredFields ( ) ; if  ( declaredFields ==  null  ||  declaredFields. length ==  0 )  { return ; } for  ( int  i =  0 ;  i <  declaredFields. length;  i++ )  { Field  declaredField =  declaredFields[ i] ; if  ( ! declaredField. isAnnotationPresent ( Autowired . class ) )  { continue ; } if  ( fieldsAlreayProcessed. contains ( object. getClass ( ) . getName ( )  +  "."  +  declaredField. getName ( ) ) )  { continue ; } Object  dependObject =  null ; Autowired  annotation =  declaredField. getAnnotation ( Autowired . class ) ; String  value =  annotation. value ( ) ; if  ( "" . equals ( value. trim ( ) ) )  {  dependObject =  map. get ( declaredField. getType ( ) . getName ( ) ) ; if  ( dependObject ==  null )  { dependObject =  map. get ( lowerFirst ( declaredField. getType ( ) . getSimpleName ( ) ) ) ; } }  else  { dependObject =  map. get ( value+ declaredField. getType ( ) . getName ( ) ) ; } fieldsAlreayProcessed. add ( object. getClass ( ) . getName ( )  +  "."  +  declaredField. getName ( ) ) ; if  ( dependObject !=  null )  { doObjectDependancy ( dependObject) ; } declaredField. setAccessible ( true ) ; try  { declaredField. set ( object,  dependObject) ; }  catch  ( Exception  e)  { e. printStackTrace ( ) ; } } } private  void  doInstance ( )  { if  ( classNames. size ( )  ==  0 )  return ; if  ( classNames. size ( )  <=  0 )  return ;  try  { for  ( int  i =  0 ;  i <  classNames. size ( ) ;  i++ )  { String  className =  classNames. get ( i) ; Class < ? > =  Class . forName ( className) ; if  ( aClass. isAnnotationPresent ( Controller . class ) )  { String  simpleName =  aClass. getSimpleName ( ) ; String  s =  lowerFirst ( simpleName) ; Object  o =  aClass. newInstance ( ) ; map. put ( s,  o) ; } if  ( aClass. isAnnotationPresent ( Service . class ) )  { String  beanName =  aClass. getAnnotation ( Service . class ) . value ( ) ; Object  o =  aClass. newInstance ( ) ; int  ju =  0 ; if  ( "" . equals ( beanName. trim ( ) ) )  { beanName =  lowerFirst ( aClass. getSimpleName ( ) ) ; } else  { ju= 1 ; } if ( ju== 1 ) { UtilGetClassInterfaces . getkeyClass ( beanName, aClass, map, o) ; } else { map. put ( beanName,  o) ; } Class < ? > [ ]  interfaces =  aClass. getInterfaces ( ) ; if  ( interfaces !=  null  &&  interfaces. length >  0 )  { for  ( int  j =  0 ;  j <  interfaces. length;  j++ )  { Class < ? > =  interfaces[ j] ; map. put ( anInterface. getName ( ) ,  aClass. newInstance ( ) ) ; } } } } }  catch  ( Exception  e)  { e. printStackTrace ( ) ; } } private  static  String  lowerFirst ( String  str)  { char [ ]  chars =  str. toCharArray ( ) ; if  ( 'A'  <=  chars[ 0 ]  &&  chars[ 0 ]  <=  'Z' )  { chars[ 0 ]  +=  32 ;  } return  String . valueOf ( chars) ;  } private  void  doScan ( String  scanPackage)  { try  { String  scanPackagePath =  Thread . currentThread ( ) . getContextClassLoader ( ) . getResource ( "" ) . getPath ( )  +  scanPackage. replaceAll ( "\\." ,  "/" ) ; scanPackagePath =  URLDecoder . decode ( scanPackagePath,  StandardCharsets . UTF_8 . toString ( ) ) ; File  pack =  new  File ( scanPackagePath) ;  File [ ]  files =  pack. listFiles ( ) ; for  ( File  file :  files)  { if  ( file. isDirectory ( ) )  { doScan ( scanPackage +  "."  +  file. getName ( ) ) ; continue ; } if  ( file. getName ( ) . endsWith ( ".class" ) )  { String  className =  scanPackage +  "."  +  file. getName ( ) . replaceAll ( ".class" ,  "" ) ; classNames. add ( className) ;  } } }  catch  ( Exception  e)  { e. printStackTrace ( ) ; } } private  String  doLoadconfig ( String  contextConfigLocation)  { InputStream  resourceAsStream =  DispatcherServlet . class . getClassLoader ( ) . getResourceAsStream ( contextConfigLocation) ; SAXReader  saxReader =  new  SAXReader ( ) ; try  { Document  document =  saxReader. read ( resourceAsStream) ; Element  rootElement =  document. getRootElement ( ) ; Element  element =  ( Element )  rootElement. selectSingleNode ( "//component-scan" ) ; String  attribute =  element. attributeValue ( "base-package" ) ; return  attribute; }  catch  ( Exception  e)  { e. printStackTrace ( ) ; } return  "" ; } @Override protected  void  doGet ( HttpServletRequest  req,  HttpServletResponse  resp)  throws  ServletException ,  IOException  { doPost ( req,  resp) ; } @Override protected  void  doPost ( HttpServletRequest  req,  HttpServletResponse  resp)  throws  ServletException ,  IOException  { System . out. println ( "请求资源的路径为:"  +  req. getRequestURI ( ) ) ;  System . out. println ( "请求资源的完整路径为:"  +  req. getRequestURL ( ) ) ;  String  requestURI =  req. getRequestURI ( ) ; Method  method =  handlerMapping. get ( requestURI) ; } 
} package  com. mvc. framework. servlet ; public  class  a extends  b implements  f { public  static  void  main ( String [ ]  args)  { getkeyClass ( a. class ) ; } private  static  void  getkeyClass ( Class  a)  { Class  superclass =  a. getSuperclass ( ) ; if  ( "java.lang.Object" . equals ( superclass. getName ( ) ) )  { System . out. println ( a. getName ( ) ) ; }  else  { System . out. println ( a. getName ( ) ) ; getkeyClass ( superclass) ; } Class [ ]  interfaces =  a. getInterfaces ( ) ; for  ( Class  anInterface :  interfaces)  { getkeyInterface ( anInterface) ; } } private  static  void  getkeyInterface ( Class  a)  { Class [ ]  interfaces1 =  a. getInterfaces ( ) ; if  ( interfaces1. length <=  0 )  { System . out. println ( a. getName ( ) ) ; return ; } System . out. println ( a. getName ( ) ) ; Class  aClass =  interfaces1[ 0 ] ; getkeyInterface ( aClass) ; } 
} package  com. mvc. framework. servlet ; public  class  b implements  c{ 
} package  com. mvc. framework. servlet ; public  interface  c extends  d{ 
} package  com. mvc. framework. servlet ; public  interface  d { 
} package  com. mvc. framework. servlet ; public  interface  f extends  g{ 
} package  com. mvc. framework. servlet ; public  interface  g { 
} 
dependObject =  map. get ( value+ declaredField. getType ( ) . getName ( ) ) ; package  com. mvc. framework. util ; import  java. util.  Map ; public  class  UtilGetClassInterfaces  { public  static  void  getkeyClass ( String  beanName,  Class  a,  Map  map,  Object  o)  { Class  superclass =  a. getSuperclass ( ) ; if  ( "java.lang.Object" . equals ( superclass. getName ( ) ) )  { map. put ( beanName +  a. getName ( ) ,  o) ; }  else  { map. put ( beanName +  a. getName ( ) ,  o) ; getkeyClass ( beanName,  superclass,  map,  o) ; } Class [ ]  interfaces =  a. getInterfaces ( ) ; for  ( Class  anInterface :  interfaces)  { getkeyInterface ( beanName,  anInterface,  map,  o) ; } } private  static  void  getkeyInterface ( String  beanName,  Class  a,  Map  map,  Object  o)  { Class [ ]  interfaces1 =  a. getInterfaces ( ) ; if  ( interfaces1. length <=  0 )  { map. put ( beanName +  a. getName ( ) ,  o) ; return ; } map. put ( beanName +  a. getName ( ) ,  o) ; Class  aClass =  interfaces1[ 0 ] ; getkeyInterface ( beanName,  aClass,  map,  o) ; } }  if ( ju== 1 ) { UtilGetClassInterfaces . getkeyClass ( beanName, aClass, map, o) ; } else { map. put ( beanName,  o) ; } 
String  requestURI =  req. getRequestURI ( ) ; String  contextPath =  req. getContextPath ( ) ; System . out. println ( "项目名称:"  +  contextPath) ;  String  substring =  requestURI. substring ( contextPath. length ( ) ,  requestURI. length ( ) ) ; System . out. println ( "拿取的路径:"  +  substring) ;  Method  method =  handlerMapping. get ( substring) ; 
 String  value =  aClass. getAnnotation ( RequestMapping . class ) . value ( ) ; if  ( "/" . equals ( value. substring ( 0 ,  1 ) )  ==  false )  { value =  "/"  +  value; } 
  String  value =  annotation. value ( ) ;  if  ( "/" . equals ( value. substring ( 0 ,  1 ) )  ==  false )  { value =  "/"  +  value; } 
package  com. mvc. framework. servlet ; import  java. lang. reflect.  Method ; public  class  a { public  void  fa ( String  a,  Integer  b)  { System . out. println ( 1  +  a +  b) ; } public  void  fb ( )  { System . out. println ( 1 ) ; } public  static  void  main ( String [ ]  args)  { try  { Class < a> =  a. class ; a a =  aClass. newInstance ( ) ; Method [ ]  methods =  aClass. getMethods ( ) ; for  ( int  i =  0 ;  i <  methods. length;  i++ )  { System . out. println ( methods[ i] . getName ( ) ) ; } Method  fa =  aClass. getMethod ( "fb" ) ; fa. invoke ( a) ; Method  fb =  aClass. getMethod ( "fa" ,  String . class ,  Integer . class ) ; fb. invoke ( a,  "2" ,  1 ) ; }  catch  ( Exception  e)  { e. printStackTrace ( ) ; } } 
} 
package  com. mvc. framework. pojo ; import  java. lang. reflect.  Method ; 
import  java. util.  HashMap ; 
import  java. util.  Map ; 
import  java. util. regex.  Pattern ; public  class  Handler  { private  Object  controller; private  Method  method; private  Pattern  pattern;  private  Map < String ,  Integer > ;  public  Handler ( Object  controller,  Method  method,  Pattern  pattern)  { this . controller =  controller; this . method =  method; this . pattern =  pattern; this . paramIndexMapping =  new  HashMap < > ( ) ; } public  Object  getController ( )  { return  controller; } public  void  setController ( Object  controller)  { this . controller =  controller; } public  Method  getMethod ( )  { return  method; } public  void  setMethod ( Method  method)  { this . method =  method; } public  Pattern  getPattern ( )  { return  pattern; } public  void  setPattern ( Pattern  pattern)  { this . pattern =  pattern; } public  Map < String ,  Integer > getParamIndexMapping ( )  { return  paramIndexMapping; } public  void  setParamIndexMapping ( Map < String ,  Integer > )  { this . paramIndexMapping =  paramIndexMapping; } 
} private  List < Handler > =  new  ArrayList < > ( ) ; 
package  com. mvc. framework. servlet ; import  java. util. regex.  Matcher ; 
import  java. util. regex.  Pattern ; public  class  a { public  static  void  main ( String [ ]  args)  { String  a =  "[0-9]{3}" ; String  b =  "122" ; CharSequence  aa =  b; System . out. println ( b. matches ( a) ) ; Pattern  pattern =  Pattern . compile ( "[0-9]{3}" ) ; Matcher  m =  pattern. matcher ( "122" ) ; boolean  matches =  m. matches ( ) ; System . out. println ( matches) ; System . out. println ( pattern) ;  } 
} 
if  ( method. isAnnotationPresent ( RequestMapping . class ) )  { RequestMapping  annotation =  method. getAnnotation ( RequestMapping . class ) ; String  value =  annotation. value ( ) ;  if  ( "/" . equals ( value. substring ( 0 ,  1 ) )  ==  false )  { value =  "/"  +  value; } String  url =  baseUrl; url +=  value; Handler  handler =  new  Handler ( entry. getValue ( ) , method,  Pattern . compile ( url) ) ; Parameter [ ]  parameters =  method. getParameters ( ) ; for  ( int  i =  0 ;  i <  parameters. length;  i++ )  { Parameter  parameter =  parameters[ i] ; if ( parameter. getType ( ) == HttpServletRequest . class || parameter. getType ( ) == HttpServletResponse . class ) { handler. getParamIndexMapping ( ) . put ( parameter. getType ( ) . getSimpleName ( ) , i) ; } else { handler. getParamIndexMapping ( ) . put ( parameter. getName ( ) , i) ; } } handlerMapping. add ( handler) ; } 
package  com. mvc. framework. servlet ; import  java. lang. reflect.  Method ; public  class  a { public  void  fa ( String  a,  Integer  b,  int  j)  { } public  static  void  main ( String [ ]  args)  throws  Exception  { Class < a> =  a. class ; Method  fa =  aClass. getMethod ( "fa" ,  String . class ,  Integer . class ,  int . class ) ; Class < ? > [ ]  parameterTypes =  fa. getParameterTypes ( ) ; for  ( int  i =  0 ;  i <  parameterTypes. length;  i++ )  { Class < ? > =  parameterTypes[ i] ; System . out. println ( parameterType. getSimpleName ( ) ) ; } } 
} @Override protected  void  doPost ( HttpServletRequest  req,  HttpServletResponse  resp)  throws  ServletException ,  IOException  { Handler  handler =  getHandler ( req) ; if  ( handler ==  null )  { resp. getWriter ( ) . write ( "404 not found" ) ; return ; } Class < ? > [ ]  parameterTypes =  handler. getMethod ( ) . getParameterTypes ( ) ;  Object [ ]  objects =  new  Object [ parameterTypes. length] ; int [ ]  ii =  new  int [ parameterTypes. length] ; Map < String ,  String [ ] >  parameterMap =  req. getParameterMap ( ) ;  Set < Map. Entry < String ,  String [ ] >>  entries =  parameterMap. entrySet ( ) ; for  ( Map. Entry < String ,  String [ ] >  param :  entries)  { String  value =  "" ; for  ( int  i =  0 ;  i <  param. getValue ( ) . length;  i++ )  { if  ( i >=  param. getValue ( ) . length -  1 )  { value =  param. getValue ( ) [ i] ; continue ; } value =  param. getValue ( ) [ i]  +  "," ; } if  ( ! handler. getParamIndexMapping ( ) . containsKey ( param. getKey ( ) ) )  { continue ; } Integer  integer =  handler. getParamIndexMapping ( ) . get ( param. getKey ( ) ) ; if  ( "String" . equals ( parameterTypes[ integer] . getSimpleName ( ) ) )  { objects[ integer]  =  value; } if  ( "Integer" . equals ( parameterTypes[ integer] . getSimpleName ( ) )  ||  "int" . equals ( parameterTypes[ integer] . getSimpleName ( ) ) )  { value =  value. split ( "," ) [ 0 ] ; Integer  i =  null ; try  { i =  Integer . parseInt ( value) ; }  catch  ( Exception  e)  { e. printStackTrace ( ) ; throw  new  RuntimeException ( "String转换Integet报错,参数名称是:"  +  param. getKey ( ) ) ; } objects[ integer]  =  i; } ii[ integer]  =  1 ; } Integer  integer =  handler. getParamIndexMapping ( ) . get ( HttpServletRequest . class . getSimpleName ( ) ) ; objects[ integer]  =  req; ii[ integer]  =  1 ; integer =  handler. getParamIndexMapping ( ) . get ( HttpServletResponse . class . getSimpleName ( ) ) ; objects[ integer]  =  resp; ii[ integer]  =  1 ; for  ( int  i =  0 ;  i <  ii. length;  i++ )  { if  ( ii[ i]  ==  0 )  { if  ( "int" . equals ( parameterTypes[ i] . getSimpleName ( ) ) )  { objects[ i]  =  0 ; } } } try  { handler. getMethod ( ) . invoke ( handler. getController ( ) ,  objects) ;  }  catch  ( Exception  e)  { e. printStackTrace ( ) ; } } private  Handler  getHandler ( HttpServletRequest  req)  { if  ( handlerMapping. isEmpty ( ) )  { return  null ; } String  requestURI =  req. getRequestURI ( ) ; String  contextPath =  req. getContextPath ( ) ; String  substring =  requestURI. substring ( contextPath. length ( ) ,  requestURI. length ( ) ) ; for  ( Handler  handler :  handlerMapping)  { Matcher  matcher =  handler. getPattern ( ) . matcher ( substring) ; if  ( ! matcher. matches ( ) )  { continue ; } return  handler; } return  null ; } 
package  com. mvc. framework. controller ; import  com. mvc. framework. annotations.  Autowired ; 
import  com. mvc. framework. annotations.  Controller ; 
import  com. mvc. framework. annotations.  RequestMapping ; 
import  com. mvc. framework. service.  DemoService ; import  javax. servlet. http.  HttpServletRequest ; 
import  javax. servlet. http.  HttpServletResponse ; @Controller 
@RequestMapping ( "/demo" ) 
public  class  DemoController  { @Autowired private  DemoService  demoService; @RequestMapping ( "/query" ) public  String  query ( HttpServletRequest  request,  HttpServletResponse  response,  String  name)  { String  s =  demoService. get ( name) ; return  s; } } @RequestMapping ( "/query" ) public  String  query ( HttpServletRequest  request,  HttpServletResponse  response,  String  name)  { System . out. println ( request) ; System . out. println ( response) ;  String  s =  demoService. get ( name) ; return  s; } 
package  com. mvc. framework. service. impl ; import  com. mvc. framework. annotations.  Service ; 
import  com. mvc. framework. service.  DemoService ; @Service  
public  class  DemoServiceImpl  implements  DemoService  { @Override public  String  get ( String  name)  { System . out. println ( "打印:" + name) ; return  name; } 
}     if  ( parameter. getType ( )  ==  HttpServletRequest . class  ||  parameter. getType ( )  ==  HttpServletResponse . class )  { handler. getParamIndexMapping ( ) . put ( parameter. getType ( ) . getSimpleName ( ) ,  i) ; }  else { handler. getParamIndexMapping ( ) . put ( parameter. getName ( ) ,  i) ; } 
package  com. mvc. framework. servlet ; import  java. lang. reflect.  Method ; 
import  java. lang. reflect.  Parameter ; public  class  a { public  void  fa ( String  nam2,  String  name1)  { } public  void  fb ( String  nam2,  String  name1)  { } public  static  void  main ( String [ ]  args)  { Class < a> =  a. class ; Method [ ]  fa =  aClass. getMethods ( ) ; for  ( int  i =  0 ;  i <  fa. length;  i++ )  { Method  method =  fa[ i] ; System . out. println ( method. getName ( ) ) ; Parameter [ ]  parameters =  method. getParameters ( ) ; for  ( int  ii =  0 ;  ii <  parameters. length;  ii++ )  { Parameter  parameter =  parameters[ ii] ; System . out. println ( parameter. getName ( ) ) ; } } } 
} - parameters,编译选项
< build> < plugins> < plugin> < groupId> </ groupId> < artifactId> </ artifactId> < version> </ version> < configuration> < source> </ source> < target> </ target> < compilerArguments> < parameters/> </ compilerArguments> </ configuration> </ plugin> </ plugins> </ build> spring是使用字节码分析库(如 ASM)来动态分析类文件,而不依赖于编译器生成的字节码,这种技术使 Spring 能够获取参数名称,因为他是直接分析文件的,也就可以说,他内部的代码是相当于跳过了操作了-parameters选项,而访问并处理字节码文件(实际上再怎么处理字节码文件中都会保留原始参数名称,否则的话,也不可能使用该名称的变量了,他只是不让你直接获取即可,那么如果说选项中他-parameters是解决某一个开关导致的不让获取,那么spring是直接从文件找,也自然就跳过了这个开关)  
public  class  ceshi { public  static  void  main ( String [ ]  args) { for  ( String  arg :  args)  { System . out. println ( arg) ; } } 
} < dependency> < groupId> </ groupId> < artifactId> </ artifactId> < version> </ version> </ dependency> < build> < plugins> < plugin> < groupId> </ groupId> < artifactId> </ artifactId> < version> </ version> < configuration> < source> </ source> < target> </ target> < compilerArguments> < parameters/> </ compilerArguments> </ configuration> </ plugin> </ plugins> </ build> package  com. mvc. framework. servlet ; import  java. lang. reflect.  Method ; 
import  java. lang. reflect.  Parameter ; public  class  a { public  void  fa ( String  nam2,  String  name1)  { } public  void  fb ( String  nam2,  String  name1)  { } public  static  void  main ( String [ ]  args)  { Class < a> =  a. class ; Method [ ]  fa =  aClass. getMethods ( ) ; for  ( int  i =  0 ;  i <  fa. length;  i++ )  { Method  method =  fa[ i] ; System . out. println ( method. getName ( ) ) ; Parameter [ ]  parameters =  method. getParameters ( ) ; for  ( int  ii =  0 ;  ii <  parameters. length;  ii++ )  { Parameter  parameter =  parameters[ ii] ; System . out. println ( parameter. getName ( ) ) ; } } } 
} package  com. mvc. framework. config ; import  org. objectweb. asm.  ClassVisitor ; 
import  org. objectweb. asm.  Label ; 
import  org. objectweb. asm.  MethodVisitor ; 
import  org. objectweb. asm.  Opcodes ; import  java. util.  HashMap ; 
import  java. util.  Map ; public  class  ParameterNameVisitor  extends  ClassVisitor  { private  String  methodName; private  Map  map; public  ParameterNameVisitor ( String  methodName)  { super ( Opcodes . ASM7 ) ; this . methodName =  methodName; } @Override public  MethodVisitor  visitMethod ( int  access,  String  name,  String  descriptor,  String  signature,  String [ ]  exceptions)  { if  ( name. equals ( methodName) )  { return  new  MethodParameterVisitor ( ) ; } return  null ; } public  Map  getParameterNames ( )  { return  map; } class  MethodParameterVisitor  extends  MethodVisitor  { public  MethodParameterVisitor ( )  { super ( Opcodes . ASM7 ) ; } @Override public  void  visitLocalVariable ( String  name,  String  descriptor,  String  signature,  Label  start,  Label  end,  int  index)  { if  ( index >=  0 )  { if  ( map ==  null )  { map =  new  HashMap < > ( ) ; } map. put ( index -  1 ,  name) ;  } } } 
} package  com. mvc. framework. config ; import  org. objectweb. asm.  * ; import  java. util.  Map ; public  class  ParameterNameExtractor  { public  static  Map  getParameterNames ( String  className,  String  methodName)  throws  Exception  { ClassReader  reader =  new  ClassReader ( className) ; ParameterNameVisitor  visitor =  new  ParameterNameVisitor ( methodName) ; reader. accept ( visitor,  0 ) ; return  visitor. getParameterNames ( ) ; } 
} package  com. mvc. framework. servlet ; import  com. mvc. framework. config.  ParameterNameExtractor ; 
import  java. util.  Map ; public  class  a { String  name; public  a ( String  name)  { this . name =  name; } public  a ( String  name,  String  j)  { this . name =  name; } public  void  fa ( String  nam2,  String  name1)  { } public  void  fb ( String  nam2,  String  name1)  { } public  static  void  main ( String [ ]  args)  throws  Exception  { String  className =  "com.mvc.framework.servlet.a" ; String  methodName =  "fa" ; Map  map =  ParameterNameExtractor . getParameterNames ( className,  methodName) ; for  ( int  i =  0 ;  i <  map. size ( ) ;  i++ )  { System . out. println ( "参数列表的位置:"  +  i +  ",名称是: "  +  map. get ( i) ) ; } methodName =  "fb" ; map =  ParameterNameExtractor . getParameterNames ( className,  methodName) ; for  ( int  i =  0 ;  i <  map. size ( ) ;  i++ )  { System . out. println ( "参数列表的位置:"  +  i +  ",名称是: "  +  map. get ( i) ) ; } } 
}  Map < String , String > =  null ; try  { prmap =  ParameterNameExtractor . getParameterNames ( aClass. getName ( ) ,  method. getName ( ) ) ; } catch  ( Exception  e) { e. printStackTrace ( ) ; } Parameter [ ]  parameters =  method. getParameters ( ) ; for  ( int  i =  0 ;  i <  parameters. length;  i++ )  { Parameter  parameter =  parameters[ i] ; if  ( parameter. getType ( )  ==  HttpServletRequest . class  ||  parameter. getType ( )  ==  HttpServletResponse . class )  { handler. getParamIndexMapping ( ) . put ( parameter. getType ( ) . getSimpleName ( ) ,  i) ; }  else  { handler. getParamIndexMapping ( ) . put ( prmap. get ( i) ,  i) ; } } handlerMapping. add ( handler) ; 
package  com. mvc. framework. servlet ; import  com. mvc. framework. config.  ParameterNameExtractor ; import  java. util.  Map ; public  class  a { public  static  void  main ( String [ ]  args)  throws  Exception  { String  className =  "com.mvc.framework.controller.DemoController" ; String  methodName =  "query" ; Map  map =  ParameterNameExtractor . getParameterNames ( className,  methodName) ; for  ( int  i =  0 ;  i <  map. size ( ) ;  i++ )  { System . out. println ( "参数列表的位置:"  +  i +  ",名称是: "  +  map. get ( i) ) ; } } 
}   @Override public  void  init ( ServletConfig  config)  { String  className =  "com.mvc.framework.controller.DemoController" ; String  methodName =  "query" ; Map  map =  ParameterNameExtractor . getParameterNames ( className,  methodName) ; for  ( int  i =  0 ;  i <  map. size ( ) ;  i++ )  { System . out. println ( "参数列表的位置:"  +  i +  ",名称是: "  +  map. get ( i) ) ; } } 
 public  ClassReader ( String  className)  throws  IOException  { this ( readStream ( ClassLoader . getSystemResourceAsStream ( className. replace ( '.' ,  '/' )  +  ".class" ) ,  true ) ) ; } private  static  byte [ ]  readStream ( InputStream  inputStream,  boolean  close)  throws  IOException  { if  ( inputStream ==  null )  { throw  new  IOException ( "Class not found" ) ; }  else  { . . . 
private  void  initHandlerMapping ( ServletConfig  config)  { String  ba =  aClass. getName ( ) . replace ( '.' ,  '/' )  +  ".class" ; prmap =  ParameterNameExtractor . getParameterNames ( config. getServletContext ( ) . getRealPath ( "/" ) + "WEB-INF\\classes\\" + ba,  method. getName ( ) ) ; InputStream  inputStream =  new  FileInputStream ( className) ; ClassReader  reader =  new  ClassReader ( inputStream) ;