Examples of running Web application JSP + war package in tomcat and tomcat instances

Source: Internet
Author: User

Examples of running Web application JSP + war package in tomcat and tomcat instances
Introduce dependency first:

In build. gradle

// Supports the thymeleaf template // compile ("org. springframework. boot: spring-boot-starter-thymeleaf ") // supports jsp compile ('javax. servlet: jstl ')//
 Compile ('javax. servlet: javax. servlet-api ') compile ('org. springframework. boot: spring-boot-starter-tomcat ') compile ('org. apache. tomcat. embed: tomcat-embed-jasper ')//
 

Description of dependency package range:

Compile: Compile the dependency scope. If this parameter is not specified, the dependency range is used by default. Maven dependencies using this dependency scope are valid for compiling, testing, and running classpath. A typical example is spring-code. This dependency is required during compilation, testing, and running.

Test: Test the dependency scope. Maven dependency of the secondary dependency scope is only valid for testing classpath. This dependency cannot be used when you compile the main code or run the project. A typical example is Jnuit, Which is required only when testing code is compiled and executed.

Provided: The dependency scope is provided. Maven dependency using this dependency scope is valid for compiling and testing classpath, but not for running. A typical example is servlet-api. This dependency is required during project compilation and testing. However, Maven does not need to be repeatedly introduced during project running because of the container and the provision.

Note: It has been verified that the thymeleaf template and JSP cannot be used at the same time. If thymeleaf is enabled, the controller still jumps to the same name html template file under the templates according to the thymeleaf format, at the same time, the servlet-api package uses the compile range, but both idea and external tomcat can start normally.

Application. properties configuration

To support jsp, You need to configure the path and type of the returned file in application. properties, where the type of the returned file is jsp and the path is under/WEB-INF/jsp.

spring.mvc.view.prefix: /WEB-INF/jsp/spring.mvc.view.suffix: .jsp
Control class

As shown in the above steps, the control class is written here and simple code is directly added, which is no different from the normal springMVC:

@ Controller @ RequestMapping ("/learnJsp") public class LearnResourceController {@ RequestMapping ("") public ModelAndView index () {List
 
  
LearnList = new ArrayList
  
   
(); LearnResouce bean = new LearnResouce ("Official Reference documentation", "Spring Boot Reference Guide "," http://docs.spring.io/spring-boot/docs/1.5.1.RELEASE/reference/htmlsingle/#getting-started-first-application "); LearnList. add (bean); bean = new LearnResouce (" Official SpriongBoot example "," official SpriongBoot example "," https://github.com/spring-projects/spring-boot/tree/master/spring-boot-samples "); LearnList. add (bean); bean = new LearnResouce (" longguo Emy "," Spring Boot tutorial series "," http://www.roncoo.com/article/detail/125488 "); LearnList. add (bean); bean = new LearnResouce (" Doodle MD independent blog "," Spring Boot dry goods series "," http://tengj.top/ "); LearnList. add (bean); bean = new LearnResouce (" backend programming "," Spring Boot tutorial and video "," http://www.toutiao.com/m1559096720023553/ "); LearnList. add (bean); bean = new LearnResouce (" DD "," Spring Boot series "," http://www.roncoo.com/article/detail/125488 "); LearnList. add (bean); bean = new LearnResouce (" pure smile "," Sping Boot series "," http://www.ityouknow.com/spring-boot "); LearnList. add (bean); bean = new LearnResouce (" CSDN -- Xiaogang blog column "," Sping Boot learning "," http://blog.csdn.net/column/details/spring-boot.html "); LearnList. add (bean); bean = new LearnResouce (" Liang guizhao's blog "," Spring Boot secrets and practices "," http://blog.csdn.net/column/details/spring-boot.html "); LearnList. add (bean); bean = new LearnResouce (" Lin xiangxian blog series "," Learn Spring Boot from scratch "," http://412887952-qq-com.iteye.com/category/356333 "); LearnList. add (bean); ModelAndView modelAndView = new ModelAndView ("/index "); modelAndView. addObject (" learnList ", learnList); return modelAndView ;}}
  
 

LearnResouce

public class LearnResouce {    private String author;    private String title;    private String url;    public String getAuthor() {        return author;    }    public void setAuthor(String author) {        this.author = author;    }    public String getTitle() {        return title;    }    public void setTitle(String title) {        this.title = title;    }    public String getUrl() {        return url;    }    public void setUrl(String url) {        this.url = url;    }    public LearnResouce(String author, String title, String url) {        this.author = author;        this.title = title;        this.url = url;    }}

Jsp page writing

Create index. jsp in the webapp/WEB_INF/jsp directory

<%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%><%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%><%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
 
 
 
 
 
<Script src = "$ {pageContext. request. contextPath}/vanilla-cream-css/js/libs/modernizr. min. js "> </script> <script src =" $ {pageContext. request. contextPath}/vanilla-cream-css/js/libs/jquery-1.10.0.js "> </script> <script src =" $ {pageContext. request. contextPath}/vanilla-cream-css/js/libs/jquery-ui.min.js "> </script> <script src =" $ {pageContext. request. contextPath}/vanilla-cream-css/js/libs/bootstrap. min. js "> </script> <Script src = "$ {pageContext. request. contextPath}/vanilla-cream-css/js/general. js"> </script> Sptring Boot learning resources are excellent. If you love me, pay attention to the doodle public account: java supergod School
Author Tutorial name Address
$ {Learn. author} $ {Learn. title} Click me
Page access Verification:

Finally, start tomcat and enter http: // localhost: 9090/learnJsp to view the effect:

External Tomcat server deployment war package: add
// Compile as war package apply plugin: 'War'

Then run

Or execute gradle build on the command line.

Generates the war package to the path generated in the configuration file.

Put the war in tomcat's webapps and start tomcat. The following figure shows normal access:

Note:To create a war package for springboot, you must make the following modifications. Otherwise, the corresponding jsp path is not found:

The main class inherits SpringBootServletInitializer and overwrites the configure method.

If the external container is deployed, it cannot depend on the main function of the Application. Instead, it must be similar to the web. xml file configuration to start the Spring application context. In this case, we need to inherit SpringBootServletInitializer in the startup class and implement the configure method:

Public class Application extends SpringBootServletInitializer {

@ Override

Protected SpringApplicationBuilder configure (SpringApplicationBuilder application ){

Return application. sources (Application. class );

}

}

The function of this class is similar to that of configuring listeners responsible for initializing the Spring application context in web. xml, but no additional XML files need to be written here.

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.