The Spring Boot integrated Servlet is released as a ready-to-run war package for later packaging as a Docker image.

Source: Internet
Author: User
Tags aliyun

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.