Solved

How to set a default value primefaces dropdown field

Posted on 2013-11-14
8
3,861 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
Comment Utility
Try with a List instead of a Map.
0
 
LVL 7

Author Comment

by:gudii9
Comment Utility
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
Comment Utility
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
Comment Utility
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
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 35

Expert Comment

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

Author Comment

by:gudii9
Comment Utility
>>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
Comment Utility
Please advise
0
 
LVL 7

Author Comment

by:gudii9
Comment Utility
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
changeXy challenge 13 56
nestparen challenge 4 55
topping3 challenge 14 48
groovy example issue 10 65
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…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
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 …

743 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

20 Experts available now in Live!

Get 1:1 Help Now