Spring boot (II) integrating JSP and production environment deployment

1、 Introduction

One development scenario that I have to say about Java is web development, which is also one of the most popular development scenarios in Java. When it comes to web development, one technology that can't be bypassed is JSP. Because there are still many companies using JSP on the market, this paper introduces how spring boot integrates JSP development and the detailed deployment method of production environment.

2、 Integrated JSP

development environment

JSP integration steps

The specific integration methods are as follows:

1. Create JSP directory

Create a directory webapp / WEB-INF / JSP under Src / main to store JSP pages, as shown in the following figure:

2.application. Properties configure JSP information

application. Properties is a global configuration file, which can set a lot of information, such as setting logs, setting cache, setting spring, spring session and so on. In this paper, we only need to set the JSP directory file and file suffix. The code is as follows:

spring.mvc.view.prefix=/WEB-INF/jsp
spring.mvc.view.suffix=.jsp

More applications For the properties setting information, see the official document: https://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#common -application-properties

3.pom. XML add JSP configuration

In POM XML needs to add three components:

The specific codes are as follows:

<!--web支持-->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>

<!--JavaServer Pages Standard Tag Library,JSP标准标签库-->
<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>jstl</artifactId>
</dependency>

<!--内置tocat对Jsp支持的依赖,用于编译Jsp-->
<dependency>
    <groupId>org.apache.tomcat.embed</groupId>
    <artifactId>tomcat-embed-jasper</artifactId>
    <scope>provided</scope>
</dependency>

Note: the scope value of Tomcat embedded Jasper is provided, which means that other devices will provide it instead of packaging it. If it is packaged, it will conflict with the jar provided by external tomcat, resulting in project startup failure.

4. Write spring MVC code

After the first three steps of configuration, the project configuration has been almost completed, and then the code is written. Like spring MVC, the code is divided into two parts: Java class writing and identification annotation, and JSP template creation and writing. In order to better demonstrate the function of spring boot, we will simplify the business logic as much as possible. In this example, we create a cat class, set the label hi = "Hello cat", and output the label on the page.

package com.hellospringboot.hellospringboot;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
@Controller
@RequestMapping("/cat") //创建路由规则http://xxxx/cat
public class Cat {
    /**
     * 默认路由方法
     *
     * @return
     */
    @RequestMapping("")
    public ModelAndView index() {
        ModelAndView modelAndView = new ModelAndView("/index"); //设置对应JSP的模板文件
        modelAndView.addObject("hi","Hello,Cat"); //设置${hi}标签的值为Hello,Cat
        return modelAndView;
    }
}

Interpretation of spring MVC annotation

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<h1>
    ${hi}
</h1>
</body>
</html>

This JSP only does one thing to display the value of the ${hi} tag in the Java class.

5. Operation procedure

So far, if you are using the idea development tool, you can now run the debugger and directly run index JSP or startup file, or startup file (application. Java), and then enter in the browser: http://localhost:8080/cat You can view it.

3、 Production environment deployment

Deployment steps

1. Download and install Tomcat

Download address: https://tomcat.apache.org/download-90.cgi

As shown in the figure below:

Windows Download: 64 bit windows zip | linux download: tar gz

Note: if the installation free version downloaded from Windows version is placed on drive C, pay attention to assign sufficient permissions to the folder, otherwise the access page will display 400 or 505 after startup.

2. The entry class inherits springbootservletinitializer and overrides the configure method

If you want to deploy the production environment, you need to configure the entry class of spring boot separately. You need to inherit the springbootservletinitializer class and override the configure method, because by default, external Tomcat cannot read the main method of the spring boot entry class to start the program loading. You need to inherit. The code is as follows:

package com.hellospringboot.hellospringboot;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
/**
 * 启动类,程序入口
 */
@SpringBootApplication
public class HelloSpringBootApplication  extends SpringBootServletInitializer{
    /**
     * 重写configure方法,加载启动类
     * @param application
     * @return
     */
    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(HelloSpringBootApplication.class);
    }
    /**
     * Spring Boot 默认main方法
     * @param args
     */
    public static void main(String[] args) {
        SpringApplication.run(HelloSpringBootApplication.class,args);
    }
}

3. Configure POM xml

You need to configure POM XML, exclude the built-in Tomcat jar package to prevent conflicts with the external Tomcat jar package after packaging, resulting in project startup failure. The configuration is as follows:

<!--排除内置tomcat jar包-->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-tomcat</artifactId>
    <scope>provided</scope>
</dependency>

The main code is provided. The setting package will not be included and will be provided by other facilities.

4. Generate war file from idea

Found:

Amend to read:

Why do you need to change the jar package format to war? Because if it is packaged as a jar package, it will not contain JSP files, so 404 will be returned when accessing, and JSP files will be included when packaging as war. Therefore, you need to change the packaging format to war

What is the difference between jar and war?

Therefore, judging from the difference between jar and war, it is also appropriate for web programs to be packaged in war format.

If you need to modify the file name of the generated file, you can set the finalname attribute under build. The code is as follows:

<build>
    <finalName>name</finalName>
</build> 

Select the menu bar build = > build artifacts.. = > Click rebuild to generate the war package, as shown in the figure below:

After generation, find the generated war file in the target directory of the project, as shown in the following figure:

5. Configure Tomcat running project

Add the context setting in the host tag, and fill in the war file name in the docbase attribute. The configuration is as follows:

<Host name="localhost"  appBase="webapps" unpackWARs="true" autoDeploy="true">    
    <Context path="" docBase="springbootjsp" debug="0" privileged="true" reloadable="true" />
    <!--
    其他代码
    -->
</Host>

Run bin / shutdown Bat file, start Tomcat

Enter address: http://localhost:8080/cat visit.

The deployment of this project is successful. Although I have deployed to the windows server, Linux is the same step.

4、 Knowledge extension: spring boot template recommendation

Although we described in detail the use of JSP in spring boot above, the official spring boot does not recommend the use of JSP (see below for the reasons).

Spring boot recommended template engine:

As shown in the figure below:

Why doesn't spring recommend JSP?

There are several reasons why spring does not recommend jsp:

For more details, click to view: https://spring.io/blog/2012/10/30/spring-mvc-from-jsp-and-tiles-to-thymeleaf

Conclusion: according to the official statement, if you have hundreds of JSP pages, we do not recommend that you should immediately abandon them and reuse thymeleaf. However, if you start developing a new project, you are strongly encouraged to compare other template engines and JSPS to determine which is more suitable for you.

5、 References

JSP for developing web applications: http://tengj.top/2017/03/13/springboot5/

Welcome to scan the code and join the circle for discussion and exchange

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>