Background: The Spring Boot integrated Servlet is released as a ready-to-run war package for later packaging as a Docker image.
Original Address Https://github.com/weibaohui/springboot-servlet-jsp-war-demo
#1, Build.gradle configuration note, added the war plug-in, the dependency of Jstl, Tomcat-embed-jasper, so as to run the JSP page.
buildscript { ext { springBootVersion = ‘1.5.3.RELEASE‘ } repositories { maven { url "http://maven.aliyun.com/nexus/content/groups/public/" } } dependencies { classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}") }}apply plugin: ‘java‘apply plugin: ‘idea‘apply plugin: ‘org.springframework.boot‘apply plugin: ‘war‘version = ‘0.0.1-SNAPSHOT‘sourceCompatibility = 1.8repositories { maven { url "http://maven.aliyun.com/nexus/content/groups/public/" }}dependencies { compile ‘jstl:jstl:1.2‘ compile ‘org.apache.tomcat.embed:tomcat-embed-jasper‘ compile(‘org.springframework.boot:spring-boot-starter-web‘) testCompile(‘org.springframework.boot:spring-boot-starter-test‘)}
#2, Spring boot import file configuration Note: Inherit SpringBootServletInitializer
, open@ServletComponentScan
@SpringBootApplication@ServletComponentScanpublic class DemoApplication extends SpringBootServletInitializer { @Override protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) { return builder.sources(DemoApplication.class); } public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); }}
The correct posture of #3 and servlet/jsp
# #3.1, servlet Note: Using the @WebServlet
Configure page access address, access to the JSP page requires the use of a full relative path/WEB-INF/jsp/index.jsp
@WebServlet( urlPatterns = {"/index"})public class Index extends HttpServlet { protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { doGet(req, resp); } public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.getRequestDispatcher("/WEB-INF/jsp/index.jsp").forward(request, response); }}
# #3.2, Controller mode using JSP Note: With application.properties configuration, you can use a simplified JSP path ###①application.properties configuration
spring.mvc.view.prefix=/WEB-INF/jsp/spring.mvc.view.suffix=.jsp
###②controller Writing
@Controller public class HelloController { @RequestMapping("/index1") public ModelAndView index(ModelAndView view) { view.setViewName("index"); return view; } }
#4, run the method right-type the port file directly run Bootrun execute gradle build,java-jar Xxx.war
The Spring Boot integrated Servlet is released as a ready-to-run war package for later packaging as a Docker image.