[英]Lambda expressions in JSP files will not compile (continued)
I found here the following article: Lambda expressions in JSP files will not compile我在这里找到以下文章: JSP 文件中的 Lambda 表达式将无法编译
So I added compilerSourceVM and compilerTargetVM to conf/web.xml in Tomcat.所以我在 Tomcat 的 conf/web.xml 中添加了 compilerSourceVM 和 compilerTargetVM。 (not in the project web.xml).
(不在项目 web.xml 中)。
So in general I can use stream, but for (some?) lambda expression the JSP will not be compiled by tomcat.所以一般来说我可以使用 stream,但是对于(一些?)lambda 表达式,JSP 不会被 JSP 编译。
The following are examples.以下是示例。 I want get the lambda working, I'm not looking for better coding in first example.
我想让 lambda 工作,我不是在第一个示例中寻找更好的编码。
Simple example (working):简单示例(工作):
<p>
Cookies:<br />
<%
Cookie[] cookies = request.getCookies();
if ( cookies == null || cookies.length == 0 ) {
out.println("No cookies found<br/>");
} else {
out.println("Cookies found size=" + cookies.length + "<br/>");
List<Cookie> cList = Arrays.stream(request.getCookies()).collect(Collectors.toList());
for ( Cookie c : cList ) {
out.println("C: " + c + "<br/>");
}
out.println("Cookies found size=" + cookies.length + "<br/>");
}
%>
</p>
Simple example (not working):简单示例(不工作):
Instead to have a for loop and collecting in a list, I will use the Stream::forEach with the lambda expression like:我将使用 Stream::forEach 和 lambda 表达式来代替 for 循环并在列表中收集,例如:
Arrays.stream(request.getCookies()).forEach(cookie -> {out.println("Cookie information here<br/>");});
In Eclipse the JSP will be compiled and don't show some errors.在 Eclipse 中 JSP 将被编译并且不显示一些错误。
Deploying to Tomcat and calling the URL, I'm getting:部署到 Tomcat 并调用 URL,我得到:
org.apache.jasper.JasperException: Unable to compile class for JSP:
An error occurred at line: [53] in the jsp file: [/report.jsp]
Unhandled exception type IOException
50: } else {
51: out.println("Cookies found size=" + cookies.length + "<br/>");
52: Arrays.stream(request.getCookies()).forEach(cookie -> {
53: out.println("Cookie information here<br/>");
54: });
Any hints?有什么提示吗?
Thanks in advance.提前致谢。
Edit:编辑:
I switched to Tomcat 9. Same problem.我切换到 Tomcat 9。同样的问题。 I found also some hints, that Java 1.8 + stream + lambdas should work in Tomcat8+9.
我还发现了一些提示,Java 1.8 + stream + lambdas 应该在 Tomcat8+9 中工作。
So I changed the lambda expression like:所以我改变了 lambda 表达式,如:
Arrays.stream(request.getCookies()).forEach(cookie -> {System.out.println("Cookie information here<br/>");});
So I replaced "out.println" to "System.out.println".所以我将“out.println”替换为“System.out.println”。 Now the stream and lambda are working.
现在 stream 和 lambda 正在工作。 Println going to console as expected.
Println 按预期进入控制台。 Also added some ".filter".
还添加了一些“.filter”。 Also working fine.
也工作正常。
Now the question.现在的问题。 Why I can't use out.println or out.write in the lambda expression.
为什么我不能在 lambda 表达式中使用 out.println 或 out.write。 In the same JSP the out.println is working in the for-each loop showing in the example above.
在同一个 JSP 中,out.println 在上面示例中显示的 for-each 循环中工作。 Any idea?
任何想法?
Edit:编辑:
Found the next problem, this is pure Java problem, so when you edit a JSP in Eclipse.发现下一个问题,这是纯Java问题,所以当你在Eclipse中编辑一个JSP时。 Eclipse is not showing this problem in the editor.
Eclipse 没有在编辑器中显示这个问题。
a.) out.write/out.println will throw a checked exception, so I need a try/catch, because lambda's not allow a checked exception. a.) out.write/out.println 会抛出一个检查异常,所以我需要一个 try/catch,因为 lambda 不允许检查异常。
b.) out Variable must be final. b.) out 变量必须是最终的。 So you need a
final JspWriter myOut = out;
所以你需要一个
final JspWriter myOut = out;
c.) You may have to import the JSP API lib into our project. c。)您可能必须将 JSP API 库导入我们的项目。 Gradle example:
providedCompile 'javax.servlet.jsp:javax.servlet.jsp-api:2.3.0'
Gradle 示例:
providedCompile 'javax.servlet.jsp:javax.servlet.jsp-api:2.3.0'
Can you try adding the below init parameters in your web.xml,您可以尝试在 web.xml 中添加以下初始化参数吗?
<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>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.