?
Solved

How to set a default value primefaces dropdown field

Posted on 2013-11-14
8
Medium Priority
?
4,249 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 2000 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
Industry Leaders: 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

Want to be a Web Developer? Get Certified Today!

Enroll in the Certified Web Development Professional course package to learn HTML, Javascript, and PHP. Build a solid foundation to work toward your dream job!

Question has a verified solution.

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

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses
Course of the Month11 days, 8 hours left to enroll

752 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