springboot2.2.6.RELEASE chapter2

2020-04-04  本文已影响0人  淼哥1986

Spring Boot includes support for embedded Tomcat, Jetty, and Undertow servers. Most developers
use the appropriate “Starter” to obtain a fully configured instance. By default, the embedded server
listens for HTTP requests on port 8080.

When using an embedded container, automatic registration of classes annotated with @WebServlet,
@WebFilter, and @WebListener can be enabled by using @ServletComponentScan.

JSP Limitations
When running a Spring Boot application that uses an embedded servlet container (and is packaged
as an executable archive), there are some limitations in the JSP support.
• With Jetty and Tomcat, it should work if you use war packaging. An executable war will work
when launched with java -jar, and will also be deployable to any standard container. JSPs are
not supported when using an executable jar.
• Undertow does not support JSPs.
• Creating a custom error.jsp page does not override the default view for error handling. Custom
error pages should be used instead.


官网的介绍有些瑕疵,springboot集成jsp是可以运行在.jar中的
笔者在集成jsp的时候遇到过如下问题:

按照官网的思路,jsp放在webapp下,打成war包,使用 java -jar myapp.war 形式启动。
问题:当war包特别大的时候,服务访问速度很慢。

另外一个方案是:
将webapp下的内容通过插件构建到META-INF/resources目录下

<resources>
            <resource>
                <directory>src/main/webapp</directory>
                <targetPath>META-INF/resources</targetPath>
                <includes>
                    <include>**/*.*</include>
                </includes>
           </resource>
</resources>

使用maven-assembly-plugin插件,将项目构建为自定义格式

<plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-resources-plugin</artifactId>
                <version>2.7</version>
                <executions>
                    <execution>
                        <id>copy-resources</id>
                        <phase>install</phase>
                        <goals>
                            <goal>copy-resources</goal>
                        </goals>
                        <configuration>
                            <resources>
                                <resource>
                                    <directory>src/main/resources</directory>
                                    <filtering>false</filtering>
                                </resource>
                            </resources>
                            <outputDirectory>${basedir}/target/demo/conf/</outputDirectory>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <artifactId>maven-assembly-plugin</artifactId>
                <version>2.4</version>
                <executions>
                    <execution>
                        <id>distro-assembly</id>
                        <phase>package</phase>
                        <goals>
                            <goal>single</goal>
                        </goals>
                        <configuration>
                            <descriptors>
                                <descriptor>assembly.xml</descriptor>
                            </descriptors>
                            <outputDirectory>target</outputDirectory>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>

输出两个目录,一个conf、一个lib

<assembly>
    <id>distribution</id>
    <formats>
        <format>tar.gz</format>
    </formats>
    <fileSets>
        <fileSet>
            <directory>${basedir}/target/demo/lib</directory>
            <outputDirectory>/lib</outputDirectory>
        </fileSet>
        <fileSet>
            <directory>${basedir}/src/main/resources</directory>
            <excludes>
                <exclude>package</exclude>
            </excludes>
            <outputDirectory>/conf</outputDirectory>
        </fileSet>
    </fileSets>
    <dependencySets>
        <dependencySet>
            <useProjectArtifact>true</useProjectArtifact>
            <outputDirectory>lib</outputDirectory>
            <scope>runtime</scope>
        </dependencySet>
    </dependencySets>
</assembly>

采用java -server -cp 的形式启动项目
这样启动后也是可以访问到jsp的。

上一篇 下一篇

猜你喜欢

热点阅读