You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在应用中使用Jakarta Faces 4.0新增的@ClientWindowScoped注解?

Jakarta Faces 4.0 @ClientWindowScoped 使用示例

基础配置与Bean定义

  • 为CDI托管Bean标注@ClientWindowScoped注解,搭配@Named使其可被JSF页面访问:
import jakarta.enterprise.context.ClientWindowScoped;
import jakarta.inject.Named;
import java.io.Serializable;

@Named
@ClientWindowScoped
public class ClientWindowBean implements Serializable {
    private String userInput;

    // Getter & Setter
    public String getUserInput() {
        return userInput;
    }

    public void setUserInput(String userInput) {
        this.userInput = userInput;
    }
}
  • 显式启用Client Window功能(Jakarta Faces 4.0默认已启用,若需指定模式可在web.xml中配置):
<context-param>
    <param-name>jakarta.faces.CLIENT_WINDOW_MODE</param-name>
    <param-value>url</param-value>
</context-param>

url模式下,框架会自动在URL中附加jfwid参数,无需手动处理。

JSF页面使用示例

创建client-window-demo.xhtml页面,引用上述Bean:

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="jakarta.faces.html">
<h:head>
    <title>ClientWindowScoped 演示</title>
</h:head>
<h:body>
    <h:form>
        <h:inputText value="#{clientWindowBean.userInput}" placeholder="输入内容" />
        <h:commandButton value="提交" />
    </h:form>
    <p>当前窗口的输入内容:#{clientWindowBean.userInput}</p>
</h:body>
</html>

核心说明

  • @ClientWindowScoped的Bean生命周期绑定到单个浏览器窗口/标签页,每个窗口会生成唯一的jfwid,不同窗口间的Bean实例完全隔离
  • 无需手动操作jfwid参数,框架会自动在导航、表单提交时维护该参数,确保请求关联到对应窗口的Bean
  • 若需手动获取当前窗口ID,可通过以下代码实现:
String windowId = FacesContext.getCurrentInstance().getExternalContext().getClientWindow().getId();

内容的提问来源于stack exchange,提问作者Marcos

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.01 14:40:28