JSP文件中的Lambda表达式将无法编译

时间:2016-10-14 04:36:49

标签: java jsp tomcat java-8 tomcat8

我已经尝试了stackoverflow上提到的大多数建议但尚未遇到解决方案。我正在提出的错误如下。

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 

我正在使用 IntelliJ IDEA 2016.2 并已应用这些设置。

Project StructureProjectProject SDK1.8 (java version "1.8.0_102")

Project StructureProjectProject Language Level8.0 - Lambdas, type annotations etc.

SettingsBuild, Execution, DeploymentCompilerJava CompilerProject bytecode version1.8

SettingsBuild, Execution, DeploymentCompilerJava CompilerTarget bytecode version1.8

我正在使用 Tomcat v8.0.36 ,并为JSP servlet提供以下内容。

<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>

任何建议都将不胜感激!

2 个答案:

答案 0 :(得分:13)

我使用IntelliJ IDEA 2016.3.2,tomcat apache-tomcat-8.5.8,以下更改对我来说已经足够了:
1.更改以下文件:apache-tomcat-8.5.8 \ conf \ web.xml
2.修改

的配置
 <servlet-class>org.apache.jasper.servlet.JspServlet</servlet-class>
  1. 添加以下init参数:

    &LT; INIT-PARAM&GT;
    &LT; PARAM-名称&gt;&compilerSourceVM LT; / PARAM-名称&gt;
    &LT; PARAM值&GT; 1.8&LT; / PARAM值&GT;
    &LT; / INIT-PARAM&GT;
    &LT; INIT-PARAM&GT;
    &LT; PARAM-名称&gt;&compilerTargetVM LT; / PARAM-名称&gt;
    &LT; PARAM值&GT; 1.8&LT; / PARAM值&GT;
    &LT; / INIT-PARAM&GT;

  2. 完成。

答案 1 :(得分:4)

使用Spring Boot 1.5.9.RELEASE和Tomcat 8.5.23的更新答案。由于Spring Boot / MVC中没有针对Tomcat的XML配置文件,因此我调整了从these spring docs链接的代码,以在我的基础Application类中创建自定义程序bean。修复了在IntelliJ和Gradle CLI中使用JSP中的Java 8语法导致的问题。

@SpringBootApplication
public class MyApplication extends SpringBootServletInitializer {

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
        return builder.sources(MyApplication.class);
    }

    public static void main(String[] args) {
        SpringApplication.run(MyApplication.class, args);
    }

    @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);
        };
    }

}