Lambda expressions in JSP files will not compile
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>
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);
};
}