Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6611829
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T20:01:20+00:00 2026-05-25T20:01:20+00:00

I faced this situation quite a few times, and I found my solution but

  • 0

I faced this situation quite a few times, and I found my solution but not sure if it’s a good one.

I need to call some service which interacts with the database and then share the result between multiple components.

Consider this example:

<p:outputPanel id="myPanel">
  <c:set var="myVar" value="#{myService.retrieveVarFromDB(param)}" />
  <my:comp1 val="#{myVar.field1}" />
  <my:comp2 val="#{myVar.field2}" />
  <my:comp3 val="#{myVar.field3}" />
</p:outputPanel>

Here myVar could be reevaluated multiple times, upon ajax update requests. But the good thing is that myService is called only once every time, not for every component which uses myVar. Some of my colleagues do it like this:

  <my:comp1 val="#{myService.retrieveVarFromDB(param).field1}" />
  <my:comp2 val="#{myService.retrieveVarFromDB(param).field2}" />

which is a total overhead. So far my solution satisfies me, but I also know that mixing JSF with JSTL tags could sometimes cause strange things to happen. And many of our co-developers try to avoid using them.

So, is there more elegant solution to this problem without using <c:set />?

Edit:
Calculating myVar just once doesn work for me, because as I stated earlier I need to reevaluate it multiple times. For example when user chooses an item from some select component, an ajax request is sent to the server which in turn makes call to the database, reevaluating myVar value.

Edit2:
myService is actually a managed bean, which uses injected EJB reference to interact with the database. I called it this way to show what it does, but that happened to be misleading. Sorry about that.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T20:01:20+00:00Added an answer on May 25, 2026 at 8:01 pm

    Getter calls are extraordinary cheap. They’re only expensive if you’re doing more in a getter than just returning the property. I suggest to do the job in bean’s (post) constructor instead and provide a new getter which only returns that result.

    For aliasing of long EL expressions, you can always use <ui:param>.

    As to mixing JSTL with JSF, they can cause view scoped beans to break and they may lead to unexpected behaviour when used in iterating JSF components such as <ui:repeat> and <h:dataTable>. JSTL and JSF namely don’t run in sync as you’d expect from the coding. It’s JSTL which runs first from top to bottom during JSF view build time (to create the JSF component tree) and then it’s JSF which runs from top to bottom again during JSF view render time (to generate HTML).

    See also:

    • Why JSF calls getters multiple times
    • JSTL c:if doesn't work inside a JSF h:dataTable
    • Communication in JSF 2.0 – @ViewScoped fails in tag handlers

    Update, as per your edit:

    Calculating myVar just once doesn work for me, because as I stated earlier I need to reevaluate it multiple times. For example when user chooses an item from some select component, an ajax request is sent to the server which in turn makes call to the database, reevaluating myVar value.

    Just do that in the action listener method which is invoked by the ajax request. Do above all not abuse the getter for that.

    <f:ajax listener="#{bean.itemChanged}" />
    

    with

    public void itemChanged() {
        myVar = myService.retrieveVarFromDB(param);
    }
    

    then you can use #{bean.myVar} in the view.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I faced SSLHandshakeException on Heroku. This app was not SSL app. But this app
I faced this strange situation where foreach like construct of javascript does not work
I have faced the same problem many times. The Same Problem was With This
We're often faced with the need to send a data file to one of
This might be deleted, since involves idea sharing which is not quite allowed in
I'm creating simple proxy server but I faced a strange situation, I've following code
I'm faced with a situation in JavaScript when I need to update an object
I am faced with a situation where the user does not want to allow
I've faced with situation when I need to have EF readonly property in case
For those that are knowledgeable in this subject (and hopefully faced a similar situation),

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.