【问题标题】:Why does ${bean.property} auto-creates a bean in JSP?为什么 ${bean.property} 在 JSP 中自动创建一个 bean?
【发布时间】:2016-07-03 15:45:03
【问题描述】:

我知道 JSP 已被弃用。

Here,有提到

在 JSP 中,${} 不在托管 bean 中时不会自动创建它 范围呢。因此,只有当您可以保证 #{} on 之前在某处使用过相同的托管 bean 组件树,还需要查看构建时间与查看渲染时间 考虑到生命周期。

只是为了检查它是否真实,我想出了这个:

这样一个简单的JSP页面,

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

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

在哪里

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

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

点击http://localhost:8080/Leonard/faces/create.jsp

Rafael Here

请推荐?

【问题讨论】:

标签: jsp jsf el managed-bean


【解决方案1】:

此声明,

在 JSP 中,${} 不在作用域内时不会自动创建托管 bean

仅在您使用 JSF 1.1 或更早版本或您使用 JSP 2.0 或更早版本时适用。从 JSF 1.2 和 JSP 2.1 开始,JSF EL 是 unified 和 JSP EL,这就是它开始工作的原因(通过 javax.el.CompositeELResolver)。

尽管如此,不推荐使用${...} 访问JSF 托管bean。它不可写,而#{...} 是可写的,因此您确实需要在输入组件中使用#{...}。在同一个 JSF 页面中混合 ${...}#{...} 可能最终会让未来的代码阅读器/维护者感到困惑。

【讨论】:

    猜你喜欢
    • 2019-04-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-28
    • 2011-01-09
    • 1970-01-01
    • 2013-09-28
    • 1970-01-01
    相关资源
    最近更新 更多