Solved

How to set a default value primefaces dropdown field

Posted on 2013-11-14
8
4,147 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
Stressed Out?

Watch some penguins on the livecam!

 
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

How our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

Question has a verified solution.

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

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
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…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

724 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