Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can we have a component-scoped bean in a JSF2 composite component?

Tags:

java

jsf

jsf-2

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:

  • a text box for accepting the math expression
  • two buttons called "Evaluate", and "Plot"
  • another nested component that plots the function

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

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.

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

like image 893
Pradyumna Avatar asked Nov 30 '25 04:11

Pradyumna


1 Answers

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

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

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.

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.

like image 156
Brian Leathem Avatar answered Dec 02 '25 18:12

Brian Leathem



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!