Spring Boot Dry series: (v) Developing Web application JSP

Source: Internet
Author: User

Spring Boot Dry series: (v) Developing Web application JSPoriginal 2017-04-05 toot md du ye Java Super Theological hall

Objective

The previous article introduced the spring boot using the Thymeleaf template engine, today to introduce how to use the Springboot official deprecated JSP, although the difficulty is a bit large, but it is quite interesting to play.

Body

First of all, look at the overall framework structure, with the previous introduction of the thymeleaf of the time, just a lot of WebApp the directory used to store JSP, static resources, or placed under the static resource.

Introducing Dependencies

If you use the inline Tomcat container to run it, just 3 of them are fine. The concept of scope dependency in Maven is described here, because subsequent concerns about this can be problematic.

The dependency range is used to control dependencies and three kinds of classpath (compile classpath, test classpath, run Classpath), and Maven has the following dependencies:

    • Compile: Compilation dependency range. If not specified, the dependency scope is used by default. Using this dependency-dependent Maven dependency is valid for compiling, testing, and running three kinds of classpath. The typical example is Spring-code, which needs to be used when compiling, testing, and running.

    • Test: Tests the dependency range. With a sub-dependent maven dependency, only valid for test classpath, this dependency will not be available when compiling the master code or running the project's use. The typical example is Jnuit, which is only needed when compiling the test code and running the test.

    • Provided: The range of dependencies has been provided. The use of this dependent MAVEN dependency is valid for compilation and test Classpath, but is not valid at run time. The typical example is SERVLET-API, which requires this dependency when compiling and testing a project, but when the project is run, it is not necessary for the container and the supply to be introduced repeatedly.

Application.properties Configuration

To support JSPs, you need to configure the path and type of the returned file in Application.properties

This specifies that the return file type is JSP, and the path is below/web-inf/jsp/.

Control class

The above steps have, here began to write the control class, directly on the simple code, and normal SPRINGMVC no difference:

JSP page Authoring

Start class

Startup class is the same or the simplest

Inline Tomcat Container Run project

Basic configuration OK can start the project, through Http://localhost:8080/learn access, I use the springboot is 1.5.2 version, jdk1.8, previously introduced, run the project there are three ways, I have done a test, It is found that Jasper dependencies in Maven are not as effective as adding provided and commenting out the dependency range, as follows:

There are cases where the provided is added:

    • Right-click to run the Startup class to access the page report 404 error

    • Operating normally with Spring-boot:run

    • Package into jar, run error via Java-jar Demo-0.0.1-snapshot.jar

    • Packaged into a war, run normally through Java-jar Demo-0.0.1-snapshot.war

To comment out the provided:

    • Right-click to run the Startup class to access the page normally

    • Spring-boot:run Run Access page OK

    • Package into jar, run error via Java-jar Demo-0.0.1-snapshot.jar

    • Packaged into a war, run normally through Java-jar Demo-0.0.1-snapshot.war

I tested several times, that is, when there is provided, right-click on the startup class to access the page, the prompt 404 error.

The other 3 cases are the same, jar runs are reported 404,spring-boot:run and the war can run.

Why does the jar package not work, we open the packaged jar and war to see the difference, as shown in the following 2 figure:

As can be seen from the above, the jar package will run with a 404 error, because the default JSP will not be copied in, and the war package contains JSP, so no problem.

Inline Tomcat Property Configuration

The occasional properties of Tomcat are defined in the Org.springframework.boot.autoconfigure.web.ServerProperties configuration class, and we only need to configure the properties in the Application.properties configuration. The common servlet container configuration is prefixed with "server", while the Tomcat-specific configuration is prefixed with "Server.tomcat". Here are some common examples.

To configure the servlet container:

To configure Tomcat:

More detailed servlet container configuration and Tomcat configuration, before you go to blogger article view: Spring Boot Dry series: Common Properties Summary

External Tomcat Server Deployment War package

When the spring boot project needs to be deployed in an external container, the Spring Boot Export War package will get an error if the deployment is directly in Tomcat, and you can try it if you don't believe it.

The following two points need to be modified before you can:

    • Inherit Springbootservletinitializer

      External container deployment, you cannot rely on the main function of application, Instead, to start the spring application context in a manner similar to the Web. xml file configuration, we need to inherit Springbootservletinitializer in the startup class and implement the Configure method:

The role of this class is similar to configuring the Listener in Web. Xml to initialize the spring application context, except that there is no need to write additional XML files here.

    • Pom.xml modifying the Tomcat-related configuration

      If you want to change the final packaging form to a war, you also need to modify the Pom.xml file because the spring-boot-starter-web contains an inline tomcat container, so the direct deployment in the external container will conflict with the error. There are two ways to solve this, as follows

      Method One:

There is a need to remove the dependency on embedded Tomcat, so that in the war package, the Tomcat-related jar package will not be included in the Lib directory, or a startup error may occur.

Another key point is that the scope of tomcat-embed-jasper must be provided.

Because Springbootservletinitializer need to rely on Javax.servlet, and Tomcat-embed-jasper below the Tomcat-embed-core there is this javax.servlet, if not provided, the final fight in the war will have servlet-api this jar, so it will be with T The conflict of Omcat itself. This key point is also adapted to the second method mentioned below.

Method Two:

Add the following configuration directly:

Provided the role of the above has been introduced very thorough, here is not verbose, this way the advantage is that the packaged war package is also suitable for Java-jar command launch and deployment to the external container.

If you don't like the default package name, you can add content through the node.

Finally start the Tomcat input Http://localhost:8080/springBootJsp/learn to see the effect, or the Mei Da

About using JAR deployment

The above has been tested, normally contains JSP page is unable to run with the jar, because the JSP default is in the WebApp directory, but packaged into a jar is not webapp this directory structure.

Although there is an introduction on the Internet through the Pom.xml configuration, the Web-inf directory is copied to meta-inf/resources below. But the blogger tried for a whole day or couldn't access it, and finally gave up. How interested you can continue to try, after all, the war can also be launched through the Java-jar command, right?

Summarize

I believe that the whole network can not find an article with my this article so detailed introduction of spring boot using JSP article. A lot of people ask me, why my many articles so easy to understand, I always cry to reply to their four words: theme reading, God knows how many articles I refer to the article on the Internet, plus how many of the relevant books on the content of this chapter, repeated contrast refining, and finally output the corresponding blog post. Seriously, I envy you in this era of the explosion of knowledge, just see a piece of the technology you want to learn a good article, less to go how many detours.

Said so many sensational words, which big brothers with a wave rhythm ah, long time did not receive a reward d===== ( ̄▽ ̄*) b

SOURCE download

( ̄︶ ̄) [Complete code for the relevant example]

Https://github.com/tengj/SpringBootDemo/tree/master

Spring Boot Dry series: (v) Developing Web application JSP

Related Article

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.