Solved

How to set a default value primefaces dropdown field

Posted on 2013-11-14
8
4,095 Views
Last Modified: 2013-12-13
Hi,

I have added new  dropdown field to my xhtml using JSF and primefaces.

I have requirement that it should populate default value of 1000 coming from collection object Map of one other form (mxxCppForm.java) when i come to that page in the Add scenario for the first time.

In the update scenario since i have the value i do not need to set default value but display the added value which I added in the last sentence. How to achieve this


My code is mxxCppAdd.xhtml

   <h:panelGrid>
                        <p:outputLabel for="mxxCpp" value="Mxx Cpp" />
                        <p:selectOneMenu id="mxxCpp"
                            value="#{mxxCppForm.mxxCppCompany.mxxCpppercentage}"
                            styleClass="newVarClass" value="1000">
                            <f:selectItems value="#{mxxCppForm.mxxCppListItems}" />
                        </p:selectOneMenu>

                    </h:panelGrid>

Open in new window



i tried giving like value="1000" and got error
javax.faces.view.facelets.FaceletException: Error Parsing
mxxCppAdd.xhtml
Error Traced[line: 3333] Attribute "value" was already specified for element  </p:selectOneMenu>


Do i supposed to give hard code value like
 value="1000"

or
get from collection object Map within mxxCppForm.java . If i need to get from Map in mxxCppForm.java how to get it?

My list looks like this in the mxxCppForm.java


      private static Map<String, String> mxxCppListItems;
      static {
            mxxCppListItems = new LinkedHashMap<String, String>();
            mxxCppListItems.put("1000", "1000");
            mxxCppListItems.put("2000", "2000");
            mxxCppListItems.put("3000", "3000");
            mxxCppListItems.put("4000", "4000");
            mxxCppListItems.put("5000", "5000");
            
            

      };


public Map<String, String> getMxxCppListItems() { return mxxCppListItems; }

please advise
Any links resources ideas highly appreciated. Thanks in advance
0
Comment
Question by:gudii9
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
8 Comments
 
LVL 35

Expert Comment

by:girionis
ID: 39651150
Try with a List instead of a Map.
0
 
LVL 7

Author Comment

by:gudii9
ID: 39651275
how list would be different from map in this case. How to use list. You mean array list. Please advise.
0
 
LVL 35

Accepted Solution

by:
girionis earned 500 total points
ID: 39655650
Yes I mean an array list. Define your mxxCppListItems as list
List<String> mxxCppListItems  = null;
...
....
mxxCppListItems = new AttayList<String>(5);
            mxxCppListItems.add("1000");
            mxxCppListItems.add("2000");
            mxxCppListItems.add("3000");
            mxxCppListItems.add("4000");
            mxxCppListItems.add("5000");

Open in new window

0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 7

Author Comment

by:gudii9
ID: 39661400
private static Map<String, String> mxxCppListItems;
      static {
            mxxCppListItems = new LinkedHashMap<String, String>();
            mxxCppListItems.put("1000", "1000");
            mxxCppListItems.put("2000", "2000");
            mxxCppListItems.put("3000", "3000");
            mxxCppListItems.put("4000", "4000");
            mxxCppListItems.put("5000", "5000");
           
           

      };

What is the meaning of above code i saw in my project.

Why they used map inside other map and then there is static block. It is not clear to me. Please advise with any good link, example
0
 
LVL 35

Expert Comment

by:girionis
ID: 39668563
They just initialise the map in a static block of code.
0
 
LVL 7

Author Comment

by:gudii9
ID: 39669942
>>They just initialise the map in a static block of code.


what is advantage of doing it.

Also is there is a way i can get default value in primefaces dropdown field with existing code without changing LinkedHashMap to ArrayList. Please advise
0
 
LVL 7

Author Comment

by:gudii9
ID: 39675552
Please advise
0
 
LVL 7

Author Comment

by:gudii9
ID: 39690909
I observed whichever i define at the top

mxxCppListItems.put("2000", "2000");
mxxCppListItems.put("1000", "1000");
mxxCppListItems.put("3000", "3000");
            mxxCppListItems.put("4000", "4000");
            mxxCppListItems.put("5000", "5000");

that is coming as default value in that order. Is it is good fix?
Please advise
0

Featured Post

Is Your DevOps Pipeline Leaking?

Is your CI/CD pipeline a hodge-podge of randomly connected tools? You’ve likely got a tool to fix one problem & then a different tool to fix another, resulting in a cluster of tools with overlapping functionality. Learn how to optimize your pipeline with Gartner's recommendations

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
JAVA API design with micro service cloud in mind 1 109
Regarding swagger API 1 59
Netbeans and org.apache.commons.lang3 issue 3 53
using executorService 3 19
INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
Suggested Courses

732 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question