[英]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.我已经尝试了 stackoverflow 上提到的大部分建议,但还没有找到解决方案。 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.我正在使用IntelliJ IDEA 2016.2并应用了这些设置。
Project Structure
→ Project
, Project SDK
to 1.8 (java version "1.8.0_102")
Project Structure
→ Project
, Project SDK
到1.8 (java version "1.8.0_102")
Project Structure
→ Project
, Project Language Level
to 8.0 - Lambdas, type annotations etc.
Project Structure
→ Project
, Project Language Level
到8.0 - Lambdas, type annotations etc.
Settings
→ Build, Execution, Deployment
→ Compiler
→ Java Compiler
, Project bytecode version
to 1.8
Settings
→ Build, Execution, Deployment
→ Compiler
→ Java Compiler
, Project bytecode version
为1.8
Settings
→ Build, Execution, Deployment
→ Compiler
→ Java Compiler
, Target bytecode version
to 1.8
Settings
→ Build, Execution, Deployment
→ Compiler
→ Java Compiler
, Target bytecode version
为1.8
I'm using Tomcat v8.0.36 and have the following for JSP servlets.我正在使用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>
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:我使用的是 IntelliJ IDEA 2016.3.2,tomcat apache-tomcat-8.5.8,以下改动对我来说已经足够了:
1. Change following file: apache-tomcat-8.5.8\\conf\\web.xml 1.修改以下文件:apache-tomcat-8.5.8\\conf\\web.xml
2. Modify configuration for 2.修改配置
<servlet-class>org.apache.jasper.servlet.JspServlet</servlet-class>
Add following init params:添加以下初始化参数:
<init-param> <初始化参数>
<param-name>compilerSourceVM</param-name> <param-name>compilerSourceVM</param-name>
<param-value>1.8</param-value> <param-value>1.8</param-value>
</init-param> </init-param>
<init-param> <初始化参数>
<param-name>compilerTargetVM</param-name> <param-name>compilerTargetVM</param-name>
<param-value>1.8</param-value> <param-value>1.8</param-value>
</init-param> </init-param>
Finish.完成。
An updated answer for those using Spring Boot and Tomcat.针对使用 Spring Boot 和 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.
由于 Spring Boot/MVC 中没有 Tomcat 的 XML 配置文件,我改编了从这些 spring 文档链接的代码,以在我的基本应用程序类中创建一个定制器 bean。 Fixes problems caused by using Java 8 syntax in JSPs in both IntelliJ and Gradle CLI.
修复了在 IntelliJ 和 Gradle CLI 的 JSP 中使用 Java 8 语法引起的问题。
If you use Spring 1.x , add a EmbeddedServletContainerCustomizer bean:如果您使用Spring 1.x ,请添加一个 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:如果您使用Spring 2.x ,请添加一个 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);
};
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.