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 8966393
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T17:01:03+00:00 2026-06-15T17:01:03+00:00

I put this problem in a simple example, a composite component that calculates the

  • 0

I put this problem in a simple example, a composite component that calculates the sum of 2 inputs and prints the result in an outputText

Main JSF page:

<html xmlns="http://www.w3.org/1999/xhtml" xmlns:h="http://java.sun.com/jsf/html" xmlns:ez="http://java.sun.com/jsf/composite/ezcomp/">
    <h:head></h:head>
    <h:body>
      <ez:Calculator />
      <br/>
      <br/>
      <ez:Calculator />
      <br/>
      <br/>
      <ez:Calculator />
    </h:body>
</html>

Composite component XHTML:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
  xmlns:composite="http://java.sun.com/jsf/composite"
  xmlns:h="http://java.sun.com/jsf/html">
    <h:head>
        <title>This content will not be displayed</title>
    </h:head>
    <h:body>
    <composite:interface componentType="calculator">
    </composite:interface>

    <composite:implementation>
      <h:form>
      <h:inputText id="first" value="#{cc.firstNumber}" /> 
      <h:commandButton value="+" action="#{cc.sum}"/>
      <h:inputText id="second" value="#{cc.secondNumber}" /> 
      </h:form>
      <h:outputText id="result" value="#{cc.result}" />
  </composite:implementation>

</h:body>
</html>

Composite component backing bean:

package ez;


import javax.faces.component.FacesComponent;
import javax.faces.component.UINamingContainer;

@FacesComponent("calculator")
public class Calculator extends UINamingContainer  {

    private Long firstNumber;
    private Long secondNumber;
    private Long result;

    public Calculator() {
    }

    @Override
    public String getFamily() { 
      return "javax.faces.NamingContainer"; 
    }

    public void setFirstNumber(String firstNumber) {
      this.firstNumber = Long.parseLong(firstNumber);
    }

    public String getFirstNumber() {
      if(firstNumber == null) {
        return null;
      }
      return firstNumber.toString();
    }

    public void setSecondNumber(String secondNumber) {
      this.secondNumber = Long.parseLong(secondNumber);
    }

    public String getSecondNumber() {
      if(secondNumber == null) {
        return null;
      }
      return secondNumber.toString();
    }

    public String getResult() {
      if(result == null) {
        return null;
      }
      return result.toString();
    }

    public void setResult(String result) {
      this.result = Long.parseLong(result);
    }     

    public void sum() {
      this.result = this.firstNumber + this.secondNumber;
    }
}

So, I have 3 Composite Components that all should do the same thing, but when I press a SUM button, after the server processes the request, the result is printed out on the page, but the other 2 components are cleared of their values.

How can I prevent this? How can I force it to retain those values?

  • 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-06-15T17:01:04+00:00Added an answer on June 15, 2026 at 5:01 pm

    UIComponent instances are recreated on every request, hereby losing all instance variables everytime. They basically act like request scoped managed beans, while you intend to have them in the view scope. You need to take view state saving into account on a per-attribute basis. This is normally by default already done for all attributes of #{cc.attrs}. So, if you can, just make use of it:

    <cc:interface componentType="calculator">
        <cc:attribute name="firstNumber" type="java.lang.Long" />
        <cc:attribute name="secondNumber" type="java.lang.Long" />
    </cc:interface>
    <cc:implementation>
        <h:form>
            <h:inputText id="first" value="#{cc.attrs.firstNumber}" /> 
            <h:commandButton value="+" action="#{cc.sum}"/>
            <h:inputText id="second" value="#{cc.attrs.secondNumber}" /> 
        </h:form>
        <h:outputText id="result" value="#{cc.attrs.result}" />
    </cc:implementation>
    

    with just this (nullchecks omitted; I recommend to make use of required="true" on the inputs)

    @FacesComponent("calculator")
    public class Calculator extends UINamingContainer {
    
        public void sum() {
            Long firstNumber = (Long) getAttributes().get("firstNumber");
            Long secondNumber = (Long) getAttributes().get("secondNumber");
            getAttributes().put("result", firstNumber + secondNumber);
        }
    
    }
    

    Otherwise, you’d have to take state saving into account yourself by delegating all attribute getters/setters to UIComponent#getStateHelper(). Based on the very same Facelets code as you have, the entire backing component would look like this:

    @FacesComponent("calculator")
    public class Calculator extends UINamingContainer {
    
        public void sum() {
            setResult(getFirstNumber() + getSecondNumber());
        }
    
        public void setFirstNumber(Long firstNumber) {
            getStateHelper().put("firstNumber", firstNumber);
        }
    
        public Long getFirstNumber() {
            return (Long) getStateHelper().eval("firstNumber");
        }
    
        public void setSecondNumber(Long secondNumber) {
            getStateHelper().put("secondNumber", secondNumber);
        }
    
        public Long getSecondNumber() {
            return (Long) getStateHelper().eval("secondNumber");
        }
    
        public void setResult(Long result) {
            getStateHelper().put("result", result);
        }
    
        public Long getResult() {
            return (Long) getStateHelper().eval("result");
        }
    
    }
    

    See, no local variables anymore. Note that I also removed the need for those ugly manual String-Long conversions by just declaring the right getter return type and setter argument type. JSF/EL will do the conversion automagically based on default converters or custom Converters. As there’s already a default one for Long, you don’t need to provide a custom Converter.


    Unrelated to the concrete problem, you can safely remove the getFamily() method. The UINamingContainer already provides exactly this. If you were implementing NamingContainer interface instead, then you’d indeed need to provide it yourself, but this is thus not the case here. The above backing component examples have it already removed.

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

Sidebar

Related Questions

Basically i have a problem with this timer program I am trying to put
how to put this code below? - (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component { NSString *year
This problem is very simple, but I just can't figure it out added to
I'm relatively new to scripting and apologize in advance for this painfully simple problem.
I know this must be a relatively simple problem, but Google has failed me.
This is i think a simple problem but i can't seem to solve it.
Hi I think this is very simple problem but I am not able to
I put this at the top, using sudo vi /etc/profile: PYTHONPATH=/home/myuser:/home/myotheruser When I use
I put this in my JavaScript, and introduced a 500 error in my $.ajax,
When I put this code in <asp:LoginStatus ID=LoginStatus1 runat=server style=top: 170px; left: 890px; position:

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.