在 JSF 中的托管组件之间传递数据

发布于 2024-08-19 04:24:24 字数 69 浏览 5 评论 0原文

实际上是否可以在 JSF 中的托管组件之间传递任何数据?如果是,如何实现这一目标?

有人可以提供任何样本吗?

Is it actually possible to pass any data between managed components in JSF? If yes, how to achieve this?

Could anyone provide any sample?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

找个人就嫁了吧 2024-08-26 04:24:24

有几种方法。如果托管 bean 彼此相关,最干净的方法是注入。根据 JSF 版本以及 Bean 是否由 CDI 管理,有不同的方法。

CDI

只需使用@Inject

@Named
@SessionScoped
public class Bean1 {

    // ...
}
@Named
@RequestScoped
public class Bean2 {

    @Inject
    private Bean1 bean1; // No getter/setter needed.
}

换句话说,源和目标 bean 的范围并不重要,因为 CDI 在幕后注入 代理

JSF 2.x

使用@ManagedProperty

@ManagedBean
@SessionScoped
public class Bean1 {

    // ...
}
@ManagedBean
@RequestScoped
public class Bean2 {

    @ManagedProperty("#{bean1}")
    private Bean1 bean1; // Getter/setter required.
}

在此特定示例中,其他方式不可能是可能的,因为 JSF 注入的是物理实例而不是代理实例。您只能将相同更广泛范围的bean注入到特定范围的bean中。

JSF 1.x

faces-config.xml 中使用<托管属性>

public class Bean1 {

    // ...
}
public class Bean2 {

    private Bean1 bean1; // Getter/setter required.
}
<managed-bean>
    <managed-bean-name>bean1</managed-bean-name>
    <managed-bean-class>com.example.Bean1</managed-bean-class>
    <managed-bean-scope>session</managed-bean-scope>
</managed-bean>

<managed-bean>
    <managed-bean-name>bean2</managed-bean-name>
    <managed-bean-class>com.example.Bean2</managed-bean-class>
    <managed-bean-scope>request</managed-bean-scope>
    <managed-property>
        <property-name>bean1</property-name>
        <value>#{bean1}</value>
    </managed-property>
</managed-bean>

另请参阅:

There are several ways. If the managed beans are related to each other, cleanest way would be injection. There are different ways depending on JSF version and whether beans are managed by CDI.

CDI

Just use @Inject.

@Named
@SessionScoped
public class Bean1 {

    // ...
}
@Named
@RequestScoped
public class Bean2 {

    @Inject
    private Bean1 bean1; // No getter/setter needed.
}

Other way around can also, the scope of the source and target bean doesn't matter because CDI injects under the covers a proxy.

JSF 2.x

Use @ManagedProperty.

@ManagedBean
@SessionScoped
public class Bean1 {

    // ...
}
@ManagedBean
@RequestScoped
public class Bean2 {

    @ManagedProperty("#{bean1}")
    private Bean1 bean1; // Getter/setter required.
}

Other way around is not possible in this specific example because JSF injects the physical instance and not a proxy instance. You can only inject a bean of the same or broader scope into a bean of a particular scope.

JSF 1.x

Use <managed-property> in faces-config.xml.

public class Bean1 {

    // ...
}
public class Bean2 {

    private Bean1 bean1; // Getter/setter required.
}
<managed-bean>
    <managed-bean-name>bean1</managed-bean-name>
    <managed-bean-class>com.example.Bean1</managed-bean-class>
    <managed-bean-scope>session</managed-bean-scope>
</managed-bean>

<managed-bean>
    <managed-bean-name>bean2</managed-bean-name>
    <managed-bean-class>com.example.Bean2</managed-bean-class>
    <managed-bean-scope>request</managed-bean-scope>
    <managed-property>
        <property-name>bean1</property-name>
        <value>#{bean1}</value>
    </managed-property>
</managed-bean>

See also:

冷心人i 2024-08-26 04:24:24

要添加到 BalusC 的答案中,如果您使用依赖注入框架(spring、guice 等),或者如果使用 JSF 2.0,您可以使用以下方法将一个托管 bean 设置到另一个托管 bean 中:(

@Inject
private Bean2 bean2;

或基于你的 DI 框架)

To add to BalusC's answer, if you are using a dependency-injection framework (spring, guice, etc.), or if using JSF 2.0, you can have one managed bean set into the other using just:

@Inject
private Bean2 bean2;

(or the appropriate annotation based on your DI framework)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文