Solved

How to set a default value primefaces dropdown field

Posted on 2013-11-14
8
3,922 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
  • 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
 
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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
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 Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Java Filesystem manipulation of Word Docx file (find replace text) 3 64
Java Timer (static) 9 36
javap not working 8 36
expectj telnet failing 5 19
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
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 learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

919 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now