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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T15:49:26+00:00 2026-06-17T15:49:26+00:00

I have a bean class that represents a calculator. This class exposes 4 methods:

  • 0

I have a bean class that represents a calculator. This class exposes 4 methods: add, subtract, multiply, and divide.

For the front end I am writing a JSP page. This page contains two text input boxes (one for x, and one for y). I have a pulldown menu where the user selects the operation and then one Calculate command button that performs the selected operation.

<h:form>
    <h:inputText value="#{calcBean.x}"></h:inputText>
    <h:inputText value="#{calcBean.y}"></h:inputText>
    <h:selectOneMenu>
        <f:selectItem itemValue="Add"></f:selectItem>
        <f:selectItem itemValue="Subtract"></f:selectItem>
        <f:selectItem itemValue="Multiply"></f:selectItem>
        <f:selectItem itemValue="Divide"></f:selectItem>
    </h:selectOneMenu>
    <h:commandButton value="Calculate" action="@SEE COMMENT">
    <%-- TODO the action of this command button needs to change depending on --%>
    <%-- the user selection. For example: If the user selects "Add" the action --%>
    <%-- needs to be #{calcBean.add} --%>
    </h:commandButton>
</h:form>

The issue I am running into is I don’t know how to change the action of the command button depending on the user selection.

I could do this with four different command buttons but that is not an elegant solution:

<h:form>
    <h:inputText value="#{calcBean.x}"></h:inputText>
    <h:inputText value="#{calcBean.y}"></h:inputText>
    <br/>
    <h:commandButton value="Add" action="#{calcBean.add}"></h:commandButton>
    <h:commandButton value="Subtract" action="#{calcBean.subtract}"></h:commandButton>
    <h:commandButton value="Multiply" action="#{calcBean.multiply}"></h:commandButton>
    <h:commandButton value="Divide" action="#{calcBean.divide}"></h:commandButton>
</h:form>
  • 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-17T15:49:27+00:00Added an answer on June 17, 2026 at 3:49 pm

    You can do this by binding the user’s selection to a property in the backing bean and using that selected value as a condition in your action method to do the proper calculation.

    <h:selectOneMenu value=#{calcBean.calculationType}>
        <f:selectItem itemValue="Add"></f:selectItem>
        <f:selectItem itemValue="Subtract"></f:selectItem>
        <f:selectItem itemValue="Multiply"></f:selectItem>
        <f:selectItem itemValue="Divide"></f:selectItem>
    </h:selectOneMenu>
    
    <h:commandButton value="Add" action="#{calcBean.doSelectedCalculation}"></h:commandButton>
    

    In your single unique action method, according to the selected calculation type you can do operation on the given x and y values.

    public void doSelectedCalculation()
    {
         if(calculationType.equals("Add"))
         {
              result = x + y;
         }
         else if(calculationType.equals("Subtract"))
         { 
              result = x - y;
         }
         ...
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Bean class called Bean_A that is map to table_A, and this
i have an managed bean(session scope) like this: class Home {// as homeBean public
I have this Spring config: <bean id=boo class=com.x.TheClass/> The class TheClass implements TheInterface .
I have a session bean class that has some database operations. I need to
I have this bean @XmlRootElement class Test { boolean someValue; List<Field> fields; } I
I have something like that: faces-config.xml <managed-bean> <managed-bean-name>aBean</managed-bean-name> <managed-bean-class>some.pack.Bean</managed-bean-class> <managed-bean-scope>session</managed-bean-scope> </managed-bean> I have a
i have this bean public class Advertisement{ @Id @GeneratedValue(strategy = GenerationType.IDENTITY) @Column(name = pkid,
Support I have a Java Bean class that strictly holds instance fields: class College
i have bean that contain method [void return] and want access to this bean
I have an Entity/JSF Managed Bean Ticket that represents a helpdesk ticket. It has

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.