如何在JSP文件中运行Java 8 lambda函数

huangapple go评论79阅读模式
英文:

How do I run Java 8 lambda in JSP files

问题

这是一个使用Maven管理的Java JSP项目,并在本地运行Jetty。

在尝试在.jspf文件中运行lambda时,我遇到了以下错误:Lambda expressions are allowed only at source level 1.8 or above

我的设置:

  • 在Intellij IDEA的项目结构中,我已将项目SDK和项目语言设置为Java 8。
  • 模块语言级别为8 - 支持Lambda表达式、类型注解等。
  • 使用了<maven.compiler.source>1.8</maven.compiler.source><maven.compiler.target>1.8</maven.compiler.target>
  • 项目字节码版本设置为1.8
  • 使用了最新版本的Jetty(v9.4.31.x)
  • 项目使用了jetty-jspc-maven-plugin插件,在其中的<configuration>中将sourceVersiontargetVersion设置为1.8

是否有什么我遗漏的地方,或者说JSP / jspc不支持Java 8?

英文:

This is a Java JSP project with Maven management, and running Jetty locally.

I have a problem where I get this error when trying to run lambda in .jspf files: Lambda expressions are allowed only at source level 1.8 or above.

My setup:

  • In project structure in Intellij IDEA I have set Project SDK and Project language to Java 8.
  • Module language level is 8 - Lambdas, type annotations etc.
  • Using <maven.compiler.source>1.8</maven.compiler.source> and <maven.compiler.target>1.8</maven.compiler.target>
  • Project bytecode version is set to 1.8
  • Latest version of Jetty (v9.4.31.x)
  • The project is using jetty-jspc-maven-plugin plugin where I have set sourceVersion and targetVersion to 1.8 in <configuration>

Is there anything I'm missing, or is Java 8 simply not possible with JSP / jspc

答案1

得分: 1

现有的 https://github.com/jetty-project/embedded-jetty-jsp 中有一个设置 compilerSourceVMcompilerTargetVM 的示例,允许你的 *.jsp 文件使用 Java 8 的特性,比如 lambda 表达式。

基本上,你需要配置你的 JettyJspServlet

// 创建/注册 JSP Servlet(必须按规范命名为 "jsp")
ServletHolder holderJsp = new ServletHolder("jsp", JettyJspServlet.class);
holderJsp.setInitOrder(0);
holderJsp.setInitParameter("logVerbosityLevel", "DEBUG");
holderJsp.setInitParameter("fork", "false");
holderJsp.setInitParameter("xpoweredBy", "false");
holderJsp.setInitParameter("compilerTargetVM", "1.8");
holderJsp.setInitParameter("compilerSourceVM", "1.8");
holderJsp.setInitParameter("keepgenerated", "true");
servletContextHandler.addServlet(holderJsp, "*.jsp");

我已经添加了一个 lambda.jsp,在其中使用了一个简单的 lambda 表达式,以在 JSP 内进行演示(lambda 表达式与 jsp 的 JspWriter 混合使用相当复杂,从 hindsight 的角度来看,这可能不是最佳的示例选择)。

英文:

The existing https://github.com/jetty-project/embedded-jetty-jsp has an example of setting up the compilerSourceVM and compilerTargetVM to allow your *.jsp files to use Java 8 features, like lambda's.

Basically you configure your JettyJspServlet

// Create / Register JSP Servlet (must be named "jsp" per spec)
ServletHolder holderJsp = new ServletHolder("jsp", JettyJspServlet.class);
holderJsp.setInitOrder(0);
holderJsp.setInitParameter("logVerbosityLevel", "DEBUG");
holderJsp.setInitParameter("fork", "false");
holderJsp.setInitParameter("xpoweredBy", "false");
holderJsp.setInitParameter("compilerTargetVM", "1.8");
holderJsp.setInitParameter("compilerSourceVM", "1.8");
holderJsp.setInitParameter("keepgenerated", "true");
servletContextHandler.addServlet(holderJsp, "*.jsp");

I went ahead and added a lambda.jsp that uses a simple lambda within a JSP to prove it out. (lambda's mixed with jsp's JspWriter is rather complicated, it wasn't the best choice of example in hind-sight)

huangapple
  • 本文由 发表于 2020年9月25日 18:50:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/64062666.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定