Search code examples
jspjsfelmanaged-bean

Why does ${bean.property} auto-creates a bean in JSP?


I know JSP has been deprecated.

Here, it is mentioned that

In JSP, the ${} won't autocreate the managed bean when it's not in scope yet. You can thus only use it if you can guarantee that #{} on the very same managed bean is been used somewhere before in the component tree and also take view build time vs view render time lifecycle into account.

Just to check whether its true or not, I came up with this:

A simple JSP page like this,

    <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
    <html>
        <head>
        </head>
        <body>

            ${myPlayersBean.playerName}
            Here
        </body>
    </html>

where

@ManagedBean(name = "myPlayersBean")
@RequestScoped
public class PlayersBean {

    private String playerName = "Rafael";
    // getters & setters
}

it does outputs on hitting http://localhost:8080/Leonard/faces/create.jsp:

Rafael Here

Please suggest?


Solution

  • This statement,

    In JSP, the ${} won't autocreate the managed bean when it's not in scope yet

    is only applicable when you're using JSF 1.1 or older or when you're using JSP 2.0 or older. Since JSF 1.2 and JSP 2.1, JSF EL was unified with JSP EL and that's why it started to work (via javax.el.CompositeELResolver).

    Nonetheless, using ${...} to access JSF managed beans is not recommended. It is not writable, while #{...} is writable so you really need to use #{...} in input components. Mixing ${...} and #{...} in same JSF page may end up to be confusing for future code readers/maintainers.