SpringBoot项目打成jar与war的区别

  • SpringBoot默认支持不少模板引擎,可是JSP只可以在War中使用,同时mvc.view.prifix/suffix必须主动配置给出,另外必须导入JSP的默认渲染servlet:"org.apache.jasper.servlet.JspServlet",即添加依赖:
<dependency>
   		<groupId>org.apache.tomcat.embed</groupId>
   		<artifactId>tomcat-embed-jasper</artifactId>
   		<scope>provided</scope>
   	</dependency>
复制代码
  • 不管是Jar仍是War都可以使用嵌套容器,java -jar来独立运行
  • 但只有war才能部署到外部容器中,且war中必须包含:"src/main/webapp/WEB-INF/web.xml"
  • SpringBoot中JSP模板引擎具有使用限制:
  • jsp不可以在jar中使用
  • Udertow容器不支持Jsp
  • 自定义的error.jsp错误页面并不可以复写默认的error handling view,若是你想要自定义错误页面,请尝试其余模板引擎Custom error pages
  • 若是你将项目打包成jar,就不要使用src/main/webapp目录,尽管该目录也是一个公共标准,可是它仅仅在war中有效,由于生成jar的构建工具将会自动把该目录忽略

Do not use the src/main/webapp directory if your application is packaged as a jar. Although this directory is a common standard, it works only with war packaging, and it is silently ignored by most build tools if you generate a jar.html

  • SpringBoot的欢迎页同时支持静态资源模板引擎,若是项目中不存在" "、"/"Handling,那么将优先查看静态资源位置中是否存在index.html,不然才会查看index模板,若是都不存在将使用默认欢迎页

Spring Boot supports both static and templated welcome pages. It first looks for an index.html file in the configured static content locations. If one is not found, it then looks for an index template. If either is found, it is automatically used as the welcome page of the application.java

  • Spring5.+中与SpringMVC担负对做用的SpringWebFlux并非彻底依赖于Servlet API,因此不能将它打包成war更不能使用src/main/webapp目录

Spring WebFlux applications do not strictly depend on the Servlet API, so they cannot be deployed as war files and do not use the src/main/webappdirectory.web

  • 用户可以直接访问src/main/webapp中的静态资源,但并不能直接访问src/main/resources中的静态资源,可是Spring提供了ResourceHttpRequestHandler来配置src/main/resources(classpath)下指定访问目录
  • MVC中Interceptor只可以拦截Handlingsrc/main/webapp中的静态资源,对src/main/resources中的静态资源无效
  • 默认状态下,用户不具有src/main/webapp/WEB-INF的直接访问权限,可是能够经过程序中forwardredirect达到间接访问的目的,因此war项目中一般会将须要控制权限的资源文件放入到WEB-INF
  • 另外可见:Spring工程访问src/main/resources与src/main/webapp下静态资源的区别
相关文章
相关标签/搜索