Lambda in MyFaces @ManagedBean causes missing/weird behavior

472 views Asked by At

I have this small project...

In file src/main/java/com/mycompany/mavenproject2/Root.java:

package com.mycompany.mavenproject2;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.function.Supplier;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet("/")
public class Root extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest request,
            HttpServletResponse response)
            throws IOException {

        Supplier<String> welcomeMsg = () -> "Hello world! I'm a lambda.";
        PrintWriter writer = response.getWriter();
        writer.println(welcomeMsg.get());
    }
}

In file src/main/java/com/mycompany/mavenproject2/HelloBackingBean.java:

package com.mycompany.mavenproject2;

import java.util.function.Supplier;
import javax.faces.bean.ManagedBean;

@ManagedBean
public class HelloBackingBean {

    public String getMessage() {
        if (false) {
            Supplier<String> msg = () -> "Hello from a lambda";
            return msg.get();
        } else {
            return "Hello from string literal";
        }
    }

}

In file src/main/webapp/hello.xhtml:

<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://xmlns.jcp.org/jsf/html"
      xmlns:ui="http://xmlns.jcp.org/jsf/facelets"
      xmlns:f="http://xmlns.jcp.org/jsf/core"
      xmlns:c="http://xmlns.jcp.org/jsp/jstl/core">

    <h:head>
        <title>Test Page</title>
    </h:head>
    <h:body>
        <p>#{helloBackingBean.message}</p>
    </h:body>
</html>

In pom.xml:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
                             http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.mycompany</groupId>
    <artifactId>mavenproject2</artifactId>
    <version>0.1</version>
    <packaging>war</packaging>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>

    <dependencies>
        <dependency>
            <groupId>javax</groupId>
            <artifactId>javaee-api</artifactId>
            <version>7.0</version>
            <scope>provided</scope>
        </dependency>

        <!-- JSF implementation -->        
        <dependency>
            <groupId>org.apache.myfaces.core</groupId>
            <artifactId>myfaces-impl</artifactId>
            <version>2.2.5</version>
            <scope>runtime</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.eclipse.jetty</groupId>
                <artifactId>jetty-maven-plugin</artifactId>
                <version>9.2.5.v20141112</version>
                <configuration>
                    <scanIntervalSeconds>1</scanIntervalSeconds>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

Run the site using mvn jetty:run. I am using Maven version 3.2.1.

The problem

If I have the boolean literal in HelloBackingBean be true, nothing shows up when I visit localhost:8080/faces/hello.xhtml. However, if I change it to false (as it is given above), I see "Hello from string literal," as expected.

This leads me to conclude that somehow the very presence of a lambda is causing MyFaces/JSF to misbehave. This is further corroborated by localhost:8080/, which is powered by the Root class, which uses a lambda to get its message and works as expected.

My question is: why is my lambda usage not working in this project? Furthermore, why do I not get some kind of error message? Am I not checking the right places? I see no activity in the terminal when I access localhost:8080/faces/hello.xhtml.

Also worth noting that I assume javac will completely leave out code inside an if (false) block. If it doesn't... well... then I have even less idea how this is working or what's going wrong...

1

There are 1 answers

0
lu4242 On

JSF 2.2 is JDK 6 compatible, not JDK 8. But recently a patch was submitted in MYFACES-3945. It was already committed and it will be available when MyFaces 2.2.7 is released, which will be done soon. It should solve your problem.

If you have any doubt or possible bug over MyFaces please send an email to MyFaces users mailing list. If it is a confirmed bug, report it on MyFaces Core issue tracker.