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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T20:11:17+00:00 2026-05-21T20:11:17+00:00

I am trying to set default value to h:selectOneMenu. But, It is not working.

  • 0

I am trying to set default value to h:selectOneMenu. But, It is not working.

This is my code

index.xhtml

<h:body>
        <h:form id="test">
        <h:selectOneMenu value="#{selectMenuBean.selectedItem}"
                         title="select version"
                         onchange="submit()"
                         disabled="false" id="combo">

            <f:selectItems value="#{selectMenuBean.selectItems}" />
        </h:selectOneMenu>

        </h:form>
    </h:body>

BackingBean

private String selectedItem;
private List selectItems;
private int version=3;

public List getSelectItems() {

    List<Version> selectedItems = ExportDao.getVersionsList();
    System.out.println("List size: "+selectedItems.size());
    selectItems = new ArrayList();

    for (Version v1 : selectedItems) {
        String DATE_FORMAT = "yyyy-MM-dd HH:mm";
        //Create object of SimpleDateFormat and pass the desired date format.
        SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT);
        selectItems.add(new SelectItem(v1.getVersion(), "V" + v1.getVersion() + "/" + sdf.format(v1.getDate())));
        if(version = v1.getVersion()) // I have to check the version and set the matching version as selected.
        selectedItem = "V" + v1.getVersion() + "/" + sdf.format(v1.getDate());

    }
    return selectItems;
}
  • 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-21T20:11:17+00:00Added an answer on May 21, 2026 at 8:11 pm

    A few possible solutions:

    1) Set the type of selectItems to SelectItem[] instead of and untyped List.

    or 2) Try setting the var, itemValue and itemLabel attributes of the selectItems like below, and put actual Version objects in your list.

    or my favorite, 3) Make a VersionConverter that knows how to convert a Version object from and to a String. Example below if your Version object is persisted in a database and has an Id. After this is constructed, your selectedItem and List selectItems should have the typ Version (and List), not String. JSF will handle the conversion by itself.

    @FacesConverter(forClass=Version.class)
    public class VersionConverter implements Converter{
    
        public VersionConverter() {
        }
    
        @Override
        public Object getAsObject(FacesContext facesContext, UIComponent component, String value) {
            if (value == null || value.length() == 0) {
                return null;
            }
            try {
                // Get an EJB that can fetch the Version from a DB. Alternativly, do whatever you need to get your object from a string.
                InitialContext ic = new InitialContext();
    
                MyDao myDao = (MyDao)ic.lookup(String.format("java:global/%s/MyBean", (String)ic.lookup("java:module/ModuleName")));
                return myDao.findEntity(Version.class, getKey(value));
            } catch (NamingException e) {
                return null;
            }
        }
    
        Long getKey(String value) {
            Long key;
            key = Long.valueOf(value);
            return key;
        }
    
        String getStringKey(Long value) {
            StringBuilder sb = new StringBuilder();
            sb.append(value);
            return sb.toString();
        }
    
        @Override
        public String getAsString(FacesContext facesContext, UIComponent component, Object object) {
            if (object == null) {
                return null;
            }
            if (object instanceof Version) {
                Version e = (Version) object;
                return getStringKey(e.getId());
            }
            else
                throw new IllegalArgumentException("object " + object + " is of type " + object.getClass().getName() + "; expected type: " + Version.class.getName());
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to set a default value for attribute threshold in this code,
I'm trying to set a #default_value on my D7 form, but it's not working...
THIS QUESTION IS NOT ABOUT HOW TO SET DEFAULT VALUE OF A WIDGET Hello
I am trying to set the default value for a form field in SharePoint
I'm trying to set a default value for a search field. The idea is
I have a dataset that i am trying to set the default value as
I am trying to set a number of Enums to default value I am
I'm trying to set my default page to Index.html on an ASP.NET site running
I am trying to set a default image for my image upload form. In
I'm trying to set the default value for a Content Taxonomy field in a

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.