如何在Spring启动应用程序中预编译jsp?

时间:2016-01-22 10:08:45

标签: spring maven jsp spring-mvc spring-boot

我正在使用Spring启动,之前我们使用Spring和Tomcat。 两年前我们使用Spring和Tomcat时,我们使用maven插件来预编译jsp。 在部署之后避免每次首次访问都要进行编译是非常有用的。

但是,我们知道的所有maven插件都会转储一个web.xml文件,该文件列出了所有jsp和相关的生成的servlet。 使用Spring启动时,它不再使用web.xml,因此忽略此文件。

我们仍然有编辑,这是一个安全带,但每页首次访问都会受到惩罚。

有人知道是否可以在Spring启动应用程序中预编译jsp?

3 个答案:

答案 0 :(得分:16)

我在服务器启动时(不必使用JspC,更简单的构建文件)和构建时(服务器启动时间更快)进行预编译。我动态注册生成的servlet,因此如果添加/删除JSP,则不必手动更改任何文件。

在服务器启动时

使用ServletRegistration.Dynamic为每个JSP注册JSP_SERVLET_CLASS Servlet。 使用initParameter jspFile设置JSP文件名(ref

e.g。适用于ServletContextInitializerref)中的SpringBoot:

@Bean
public ServletContextInitializer preCompileJspsAtStartup() {
    return servletContext -> {
        getDeepResourcePaths(servletContext, "/WEB-INF/jsp/").forEach(jspPath -> {
            log.info("Registering JSP: {}", jspPath);
            ServletRegistration.Dynamic reg = servletContext.addServlet(jspPath, Constants.JSP_SERVLET_CLASS);
            reg.setInitParameter("jspFile", jspPath);
            reg.setLoadOnStartup(99);
            reg.addMapping(jspPath);
        });
    };
}

private static Stream<String> getDeepResourcePaths(ServletContext servletContext, String path) {
    return (path.endsWith("/")) ? servletContext.getResourcePaths(path).stream().flatMap(p -> getDeepResourcePaths(servletContext, p))
            : Stream.of(path);
}

在构建时

使用web.xmlJspC)为每个JSP和ref及其servlet映射生成Java源文件。

然后使用ServletContext注册这些内容(通过Tomcat&#39; WebXmlParser解析web.xml,例如SpringBoot:

@Value("classpath:precompiled-jsp-web.xml")
private Resource precompiledJspWebXml;

@Bean
public ServletContextInitializer registerPreCompiledJsps() {
    return servletContext -> {
        // Use Tomcat's web.xml parser (assume complete XML file and validate).
        WebXmlParser parser = new WebXmlParser(false, true, true);
        try (InputStream is = precompiledJspWebXml.getInputStream()) {
            WebXml webXml = new WebXml();
            boolean success = parser.parseWebXml(new InputSource(is), webXml, false);
            if (!success) {
                throw new RuntimeException("Error parsing Web XML " + precompiledJspWebXml);
            }
            for (ServletDef def :  webXml.getServlets().values()) {
                log.info("Registering precompiled JSP: {} = {} -> {}", def.getServletName(), def.getServletClass());
                ServletRegistration.Dynamic reg = servletContext.addServlet(def.getServletName(), def.getServletClass());
                reg.setLoadOnStartup(99);
            }

            for (Map.Entry<String, String> mapping : webXml.getServletMappings().entrySet()) {
                log.info("Mapping servlet: {} -> {}", mapping.getValue(), mapping.getKey());
                servletContext.getServletRegistration(mapping.getValue()).addMapping(mapping.getKey());
            }
        } catch (IOException e) {
            throw new RuntimeException("Error registering precompiled JSPs", e);
        }
    };
}

Maven配置示例,用于生成和编译JSP类,并生成precompiled-jsp-web.xml

<!-- Needed to get the jasper Ant task to work (putting it in the plugin's dependencies didn't work) -->
<dependency>
    <groupId>org.apache.tomcat</groupId>
    <artifactId>tomcat-catalina-ant</artifactId>
    <version>8.0.32</version>
    <scope>provided</scope>
</dependency>

<!-- ... -->

<plugin>
    <artifactId>maven-antrun-plugin</artifactId>
    <version>1.8</version>
    <executions>
        <execution>
            <id>precompile-jsp-generate-java</id>
            <!-- Can't be generate-sources because we need the compiled Henry taglib classes already! -->
            <phase>compile</phase>
            <goals>
                <goal>run</goal>
            </goals>
            <configuration>
                <tasks>
                    <echo message="Precompiling JSPs"/>
                    <property name="compile_classpath" refid="maven.compile.classpath"/>
                    <property name="target_dir" value="${project.basedir}/generated-sources/jspc" />
                    <path id="jspc_classpath">
                        <path path="${compile_classpath}"/>
                    </path>

                    <typedef resource="org/apache/catalina/ant/catalina.tasks" classpathref="jspc_classpath"/>

                    <mkdir dir="${target_dir}/java"/>
                    <mkdir dir="${target_dir}/resources"/>
                    <jasper
                            validateXml="false"
                            uriroot="${project.basedir}/src/main/webapp"
                            compilertargetvm="1.8"
                            compilersourcevm="1.8"
                            failonerror="true"
                            javaencoding="UTF-8"
                            webXml="${target_dir}/resources/precompiled-jsp-web.xml"
                            outputDir="${target_dir}/java/" >
                    </jasper>
                    <!-- Can't use Maven to compile the JSP classes because it has already compiled the app's classes
                         (needed to do that becuase JspC needs compiled app classes) -->
                    <javac srcdir="${target_dir}/java" destdir="${project.build.outputDirectory}" classpathref="jspc_classpath" fork="true"/>
                    <!-- Have to copy the web.xml because process-resources phase has already finished (before compile) -->
                    <copy todir="${project.build.outputDirectory}">
                        <fileset dir="${target_dir}/resources"/>
                    </copy>
                </tasks>
            </configuration>
        </execution>
    </executions>
</plugin>
<!-- Not strictly necessary, because Ant does the compilation, but at least attempts to keep it in sync with Maven -->
<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>build-helper-maven-plugin</artifactId>
    <executions>
        <execution>
            <id>add-precompiled-jsp-java-sources</id>
            <phase>generate-sources</phase>
            <goals><goal>add-source</goal></goals>
            <configuration>
                <sources>
                    <source>${project.basedir}/generated-sources/jspc/java</source>
                </sources>
            </configuration>
        </execution>
        <execution>
            <id>add-precompiled-jsp-resources</id>
            <phase>generate-resources</phase>
            <goals><goal>add-resource</goal></goals>
            <configuration>
                <resources>
                    <resource>
                        <directory>${project.basedir}/generated-sources/jspc/resources</directory>
                    </resource>
                </resources>
            </configuration>
        </execution>
    </executions>
</plugin>

答案 1 :(得分:1)

上面概述的“在服务器启动时”的注释:如果应用程序打包在可执行jar中,您创建的servlet将默认处于开发模式,因此如果您在生产模式下使用它,您还应该设置development = false ++以防止再次编译jsps:

reg.setInitParameter("genStringAsCharArray", "true");
reg.setInitParameter("trimSpaces", "true");
reg.setInitParameter("development", "false");

答案 2 :(得分:0)

基于paulcm的出色回答,我想出了自己的解决方案,因为上述解决方案对我不起作用,并且我无法找到错误。也许上面的答案对于tomcat9已经过时了。否则它在多模块设置中存在一些问题。但是:所有积分都属于paulcm

这只是编译时的解决方案。

将这两个插件添加到您的pom.xml

<plugin>
  <groupId>org.eclipse.jetty</groupId>
  <artifactId>jetty-jspc-maven-plugin</artifactId>
  <version>9.4.15.v20190215</version>
  <executions>
    <execution>
      <id>jspc</id>
      <goals>
        <goal>jspc</goal>
      </goals>
      <configuration>
        <mergeFragment>true</mergeFragment>
        <sourceVersion>1.8</sourceVersion>
        <targetVersion>1.8</targetVersion>
      </configuration>
    </execution>
  </executions>
</plugin>
<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-war-plugin</artifactId>
  <configuration>
    <webXml>${project.basedir}/target/web.xml</webXml>
  </configuration>
</plugin>

添加一个空的web.xml文件

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
   version="4.0"
   metadata-complete="true">
  <session-config>
    <cookie-config>
    </cookie-config>
  </session-config>
</web-app>

添加注册表

import org.apache.tomcat.util.descriptor.web.ServletDef;
import org.apache.tomcat.util.descriptor.web.WebXml;
import org.apache.tomcat.util.descriptor.web.WebXmlParser;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.web.servlet.ServletContextInitializer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.xml.sax.InputSource;

import javax.servlet.ServletRegistration;
import java.io.InputStream;
import java.util.Map;

@Configuration
public class PreCompileJspRegistry {

    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Bean
    public ServletContextInitializer registerPreCompiledJsps() {
        return servletContext -> {
            InputStream inputStream = servletContext.getResourceAsStream("/WEB-INF/web.xml");
            if (inputStream == null) {
                logger.info("web.xml konnte nicht gelesen werden");
                return;
            }
            try {
                WebXmlParser parser = new WebXmlParser(false, false, true);
                WebXml webXml = new WebXml();
                boolean success = parser.parseWebXml(new InputSource(inputStream), webXml, false);
                if (!success) {
                    logger.error("Error registering precompiled JSPs");
                }
                for (ServletDef def : webXml.getServlets().values()) {
                    logger.info("Registering precompiled JSP: {} = {} -> {}", def.getServletName(), def.getServletClass());
                    ServletRegistration.Dynamic reg = servletContext.addServlet(def.getServletName(), def.getServletClass());
                    reg.setLoadOnStartup(99);
                }

                for (Map.Entry<String, String> mapping : webXml.getServletMappings().entrySet()) {
                    logger.info("Mapping servlet: {} -> {}", mapping.getValue(), mapping.getKey());
                    servletContext.getServletRegistration(mapping.getValue()).addMapping(mapping.getKey());
                }
            } catch (Exception e) {
                logger.error("Error registering precompiled JSPs", e);
            }
        };
    }
}