简体   繁体   中英

Lambda expressions in JSP files will not compile

I've tried most of the suggestions mentioned on stackoverflow but haven't come across a solution yet. The error I'm being presented with is the following.

An error occurred at line: 379 in the jsp file: /application-new-project_process.jsp
Lambda expressions are allowed only at source level 1.8 or above 

I'm using IntelliJ IDEA 2016.2 and have applied these settings.

Project StructureProject , Project SDK to 1.8 (java version "1.8.0_102")

Project StructureProject , Project Language Level to 8.0 - Lambdas, type annotations etc.

SettingsBuild, Execution, DeploymentCompilerJava Compiler , Project bytecode version to 1.8

SettingsBuild, Execution, DeploymentCompilerJava Compiler , Target bytecode version to 1.8

I'm using Tomcat v8.0.36 and have the following for JSP servlets.

<servlet>
    <servlet-name>jsp</servlet-name>
    <servlet-class>org.apache.jasper.servlet.JspServlet</servlet-class>
    <init-param>
        <param-name>fork</param-name>
        <param-value>false</param-value>
    </init-param>
    <init-param>
        <param-name>xpoweredBy</param-name>
        <param-value>false</param-value>
    </init-param>
    <init-param> 
        <param-name>compiler</param-name> 
        <param-value>modern</param-value> 
    </init-param> 
    <init-param> 
        <param-name>compilerSourceVM</param-name> 
        <param-value>1.8</param-value> 
    </init-param> 
    <init-param> 
        <param-name>compilerTargetVM</param-name> 
        <param-value>1.8</param-value> 
    </init-param> 
    <init-param> 
        <param-name>suppressSmap</param-name> 
        <param-value>true</param-value> 
    </init-param> 
    <load-on-startup>3</load-on-startup>
</servlet>

Any suggestions will be greatly appreciated!

I use IntelliJ IDEA 2016.3.2, tomcat apache-tomcat-8.5.8, following changes are sufficient for me:
1. Change following file: apache-tomcat-8.5.8\\conf\\web.xml
2. Modify configuration for

<servlet-class>org.apache.jasper.servlet.JspServlet</servlet-class>
  1. Add following init params:

    <init-param>
    <param-name>compilerSourceVM</param-name>
    <param-value>1.8</param-value>
    </init-param>
    <init-param>
    <param-name>compilerTargetVM</param-name>
    <param-value>1.8</param-value>
    </init-param>

Finish.

An updated answer for those using Spring Boot and Tomcat. Since there is no XML configuration file for Tomcat in Spring Boot/MVC, I adapted code linked from these spring docs to create a customizer bean in my base Application class. Fixes problems caused by using Java 8 syntax in JSPs in both IntelliJ and Gradle CLI.

If you use Spring 1.x , add a EmbeddedServletContainerCustomizer bean:

@Bean
public EmbeddedServletContainerCustomizer containerCustomizer() {
    return (ConfigurableEmbeddedServletContainer container) -> {
        TomcatEmbeddedServletContainerFactory tomcat = (TomcatEmbeddedServletContainerFactory) container;
        JspServlet servlet = tomcat.getJspServlet();
        Map<String, String> jspServletInitParams = servlet.getInitParameters();
        jspServletInitParams.put("compilerSourceVM", "1.8");
        jspServletInitParams.put("compilerTargetVM", "1.8");
        servlet.setInitParameters(jspServletInitParams);
    };
}

If you use Spring 2.x , add a WebServerFactoryCustomizer bean:

@Bean
public WebServerFactoryCustomizer containerCustomizer() {
    return (WebServerFactoryCustomizer<TomcatServletWebServerFactory>) factory -> {
        Map<String, String> jspServletInitParams = factory.getInitParameters();
        jspServletInitParams.put("compilerSourceVM", "1.8");
        jspServletInitParams.put("compilerTargetVM", "1.8");
        factory.getJsp().setInitParameters(jspServletInitParams);
    };
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM