发表于: 2018-04-21 10:47:09
2 581
今天完成的事情:
1.maven配置文件中 org.apache.maven.plugins 版本号与内置maven中的版本不匹配,可能会导致打包后出现无法运行的情况:
编译时开头的告警信息:
告警信息:
[WARNING] Some problems were encountered while building the effective model for com.jnshu:SSM-demo01:war:1.0-SNAPSHOT
[WARNING] 'build.plugins.plugin.version' for org.apache.maven.plugins:maven-compiler-plugin is missing. @ line 35, column 21
[WARNING]
[WARNING] It is highly recommended to fix these problems because they threaten the stability of your build.
[WARNING]
[WARNING] For this reason, future Maven versions might no longer support building such malformed projects.
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>1.6</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
解决办法: 命令行中cd 到项目根目录 mvn help:describe -DartifactId=maven-compiler-plugin -DgroupId=org.apache.maven.plugins
最后会显示出内置编译版本,
Name: Maven Compiler Plugin
Description: The Compiler Plugin is used to compile the sources of your
project.
Group Id: org.apache.maven.plugins
Artifact Id: maven-compiler-plugin
Version: 3.1
Goal Prefix: compiler
将配置中的 <version>1.6</version> 改为 <version>3.1</version> 即可
2.踩坑<mvc:annotation-driven/>,使用自动扫描映射器和处理器时
<!-- <mvc:annotation-driven></mvc:annotation-driven> 可以替代上面的注解映射器和注解适配器 它会自动注册继承了 DefaultAnnotationHandlerMapping与AnnotationMethodHandlerAdapter 的两个Handler-->
<!-- 非DefaultAnnotationHandlerMapping与AnnotationMethodHandlerAdapter继承的自定义Handler,必须指定包 annotation-drive才会扫描并映射到 HandlerMapping -->
<!-- 可以扫描controller、service、... 这里让扫描controller,指定controller的包-->
<context:component-scan base-package="com.jnshu.ssm.Controller"/>
<mvc:annotation-driven/>
3.跟着网上大佬看前端控制器DispatcherServlet源码(大部分看不懂,只需要了解他具体的实现过程即可),了解内部实现过程,它时如何通过Servlet的web.xml配置文件实现拦截并跳转到前端控制器DispatcherServlet的?
web.xml配置:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<!-- SpringMvc前端控制器 -->
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- contextConfigLocation配置springmvc加载的配置文件(配置处理器映射器,适配器等)
如果不配置contextConfigLoaction,默认加载的是/WEB-INF/servlet名称-servlet.xml(springmvc-servlet.xml)
-->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<!-- 第一种:*.action。访问以.action结尾由DispatcherServlet进行解析;
第二种:/,所有访问的地址都由DispatcherServlet进行解析,对于静态文件的解析,
我们要配置不让DispatcherServlet进行解析。使用此种方法可以实现RESTful风格的url;
第三种:/*,这样配置不对,使用这种配置,最终要转发到一个jsp页面时,仍然会由
DispatcherServlet进行解析jsp地址,它不能根据jsp页面找到Handler,会报错-->
<url-pattern>*.action</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
源码 点开org.springframework.web.servlet.DispatcherServlet看看,里面有一个doDiapatch的方法:
方法的代码:
protected void doDispatch(HttpServletRequest request, HttpServletResponse response) throws Exception {
HttpServletRequest processedRequest = request;
HandlerExecutionChain mappedHandler = null;
boolean multipartRequestParsed = false;
WebAsyncManager asyncManager = WebAsyncUtils.getAsyncManager(request);
try {
try {
ModelAndView mv = null;
Exception dispatchException = null;
try {
processedRequest = this.checkMultipart(request);
multipartRequestParsed = processedRequest != request;
mappedHandler = this.getHandler(processedRequest);
if (mappedHandler == null || mappedHandler.getHandler() == null) {
this.noHandlerFound(processedRequest, response);
return;
}
HandlerAdapter ha = this.getHandlerAdapter(mappedHandler.getHandler());
String method = request.getMethod();
boolean isGet = "GET".equals(method);
if (isGet || "HEAD".equals(method)) {
long lastModified = ha.getLastModified(request, mappedHandler.getHandler());
if (this.logger.isDebugEnabled()) {
this.logger.debug("Last-Modified value for [" + getRequestUri(request) + "] is: " + lastModified);
}
if ((new ServletWebRequest(request, response)).checkNotModified(lastModified) && isGet) {
return;
}
}
if (!mappedHandler.applyPreHandle(processedRequest, response)) {
return;
}
mv = ha.handle(processedRequest, response, mappedHandler.getHandler());
if (asyncManager.isConcurrentHandlingStarted()) {
return;
}
this.applyDefaultViewName(processedRequest, mv);
mappedHandler.applyPostHandle(processedRequest, response, mv);
} catch (Exception var19) {
dispatchException = var19;
}
this.processDispatchResult(processedRequest, response, mappedHandler, mv, dispatchException);
} catch (Exception var20) {
this.triggerAfterCompletion(processedRequest, response, mappedHandler, var20);
} catch (Error var21) {
this.triggerAfterCompletionWithError(processedRequest, response, mappedHandler, var21);
}
} finally {
if (asyncManager.isConcurrentHandlingStarted()) {
if (mappedHandler != null) {
mappedHandler.applyAfterConcurrentHandlingStarted(processedRequest, response);
}
} else if (multipartRequestParsed) {
this.cleanupMultipart(processedRequest);
}
}
}
明天计划的事情:(一定要写非常细致的内容)
遇到的问题:(遇到什么困难,怎么解决的)
收获:(通过今天的学习,学到了什么知识)
评论