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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T06:11:08+00:00 2026-06-16T06:11:08+00:00

Hi I have a problem about dynamically displaying the value of a HtmlSelectOneMenu. Below

  • 0

Hi I have a problem about dynamically displaying the value of a HtmlSelectOneMenu. Below is a small application that describes my problem.

I have a list of cars List<Car> carList = new ArrayList<Car>() in my backing bean.

Car is a abstract class and Toyota and Ford extends Car.

Now I need to display different message in the selectonemenu based on the class type. If it is Toyota then I would display something else. Maybe its clearer for the codes to tell the story.

Backing Bean:

@ManagedBean(name="myBean")
@SessionScoped
public class MyCarBackingBean implements PhaseListener {

    private List<Car> carList = new ArrayList<Car>();
    private HtmlSelectOneMenu hsom;
    Car myCar;

    @PostConstruct
    public void init() {
        carList.add(new Ford());
        carList.add(new Toyota());
    }


    @Override
    public void beforePhase(PhaseEvent event) {
    //hsom becomes null here. Im pretty sure the setHsom was called before and the variable was set.
    if(hsom != null) {
        switch((Integer)hsom.getValue()){
            case 1: hsom.setValue("This is a Ford car"); break;
            case 2: hsom.setValue("This is a Toyota car"); 
        }
    }


    //The rest of the world...
}

And i bind the selectonemenu to the component in my page:

<h:form>
    <h:selectOneMenu binding="#{myBean.hsom}">
       <f:selectItems value="#{myBean.carList}" var="car" itemValue="#{car.id}" itemLabel="#{car.id}" />
       </h:selectOneMenu>
    <h:commandButton value="Submit" action="#{myBean.mySubmit()}"/>
</h:form>

And finally the model classes:

public abstract class Car {

    protected int id;
    //Getters and Setters

}

public class Toyota extends Car {

    public Toyota(){
        this.id = 2; //in case of ford car, id is 1.
    }

}

And I’m thinking using a phase listener to change the display, cos I read some posts saying that it is bad to change the getters and setters and put business logic in them. Nor do I want to wrap those cars in other objects and make use of itemLabel and itemValue.

But when I was debugging it I found that hsom is null when the execution reaches beforePhase but it is not null in the rest part of the code.

So my questions are : is it a good way to use a phase listener for this? And why is the component object null in beforePhase?

  • 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-16T06:11:10+00:00Added an answer on June 16, 2026 at 6:11 am

    Add a different attribute (say description) to your class. Implement it as you like, and refer to it in the selectItems tag. V.g.

    <f:selectItems value="#{myBean.carList}" var="car" itemValue="#{car.id}" itemLabel="#{car.description}" />
    

    Alternatively, replace myBean.carList with a method that returns a List<SelectItem>, and create the selectItems as you wish.

    As a rule of thumb, try to keep the .xhtml as “logic-free” as possible.

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

Sidebar

Related Questions

I have problem adding arraylist to list view, will explain about my problem here..
I have got a performance problem about TextField.htmlText +=msg .And I know that TextField.appendText(msg)
My problem is this I have a base page that creates content dynamically. There
I have a small WCF hosting engine that I am writing that will dynamically
I have a problem that I tried to ask about previously, but didn't get
I have a problem about casting a CallableStatement to OracleCallableStatement . It gives ClassCastException
I have a problem about div position relative alignment. I want the second div
I am using MFC CFile Seek function. I have a problem about Seek out
I have a little problem about the sort direction of a specific column in
I have a great problem about the rotation in three.js I want to rotate

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.