我们可以在JSF2复合组件中拥有一个组件范围的bean吗

我们可以在JSF2复合组件中拥有一个组件范围的bean吗

本文介绍了我们可以在JSF2复合组件中拥有一个组件范围的bean吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如何创建组件范围的" bean,或者说是复合组件内部的局部变量",它们对于复合组件的实例是私有的,并且在该实例中一直存在生活.

I was wondering how I could create "component-scoped" beans, or so-to-say, "local variables inside a composite component" that are private to the instance of the composite component, and live as long as that instance lives.

下面是更多详细信息,并通过示例进行说明:

Below are more details, explained with an example:

假设有一个计算器"组件-允许用户键入数学表达式并评估其值的组件. (可选)它还会绘制关联的函数.

Suppose there is a "calculator" component - something that allows users to type in a mathematical expression, and evaluates its value. Optionally, it also plots the associated function.

我可以制造具有以下特征的复合组件

I can make a composite component that has:

  • 用于接受数学表达式的文本框
  • 两个按钮分别为评估"和绘图"
  • 另一个绘制函数的嵌套组件

这显然是一个独立的功能;这样一来,想要使用它的人就可以说< math:expressionEvaluator/>

It is evidently a self-contained piece of function; so that somebody who wants to use it may just say <math:expressionEvaluator />

但是显然,该实现需要一个Java对象-一些用于评估表达式的对象,一些用于计算绘图点的对象,等等-我想它可以是一个bean-仅针对 this 组件的实例,而不是在该组件的所有实例之间共享的视图范围或请求范围的bean.

But obviously, the implementation would need a java object - something that evaluates the expression, something that computes the plot points, etc. - and I imagine it can be a bean - scoped just for this instance of this component, not a view-scoped or request-scoped bean that is shared across all instances of the component.

如何创建这样的bean?复合组件甚至可以做到吗?

How do I create such a bean? Is that even possible with composite components?

推荐答案

没有按组件实例"范围.但是您仍然可以达到预期的效果.

There is no "per-component instance" scope. But you can still achieve your desired effect.

使用ViewScoped bean进行评估和绘图-这些函数是无状态的",因此由您的输入提供.

Use a ViewScoped bean to do the evaluating and plotting - these functions are "stateless" and so are fed by your input.

您的输入将由用户提供的bean支持-用相同的方式,文本框或日历小部件需要将输入框绑定到用户提供的bean.这将保存您的无状态"范围的Bean所作用的数据.

Your input would be backed by a user supplied bean - in the same way a text box or calendar widget needs an input box bound to a user supplied bean. This holds the data that your "stateless" viewscoped bean acts on.

如果您真的想保留组件中包含的所有内容,我想您可以使用ViewScoped bean支持输入,该bean包含以输入id为键的映射.不确定是否可以.

If you really wanted to keep everything contained in the component, I guess you could back the input with a ViewScoped bean that contains a map keyed by the input id. Not sure if that would work though.

这篇关于我们可以在JSF2复合组件中拥有一个组件范围的bean吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 21:28