Using a managed property Map in JSF to show a status in aSelectOneMenu and OutputText

A

andymconline

Hello,

I'm hoping someone can help me. I am trying to use a map-entry to
store and display the status of an entity in the database. I have the
following managed-bean defined in my faces-config.xml file:

<managed-bean>
<managed-bean-name>RPHandler</managed-bean-name>
<managed-bean-class>com.sett.backing.RPHandler</managed-bean-class>
<managed-bean-scope>session</managed-bean-scope>
<managed-property>
<property-name>ratePlan</property-name>
<value>#{ratePlanBean}</value>
</managed-property>
<managed-property>
<property-name>statusMap</property-name>
<property-class>java.util.HashMap</property-class>
<map-entries>
<map-entry>
<key>In Development</key>
<value>D</value>
</map-entry>
<map-entry>
<key>Active</key>
<value>A</value>
</map-entry>
<map-entry>
<key>Not For Sale</key>
<value>N</value>
</map-entry>
</map-entries>
</managed-property>
</managed-bean>


In my ratePlanForm.jsp file I have the following code to display a
SELECT box:

<h:selectOneMenu id="status" value="#{RPHandler.ratePlan.status}">
<f:selectItems value="#{RPHandler.statusMap}" />
<h:selectOneMenu>

Which creates the following HTML output:

<select id="status"><option value="D>In Development</option><option
value="A">Active</option><option value="N">Not For Sale</option></
select>

This is exactly what I want, however when I then try and show a list
of ratePlans and their associated status's I use:

<h:eek:utputText value="#{RPHandler.statusMap[ratePlan.status]}" />

Which obviously shows nothing as ratePlan.status is set to the value
from the SELECT box ("A" for example) and their is no key equal to A
in the HashMap (as I have had to declare the HashMap with the
descriptions in the key field so that the selectItems JSF tag displays
the descriptions in the SELECT box). How to I get the outputText to
show the textual description of A? Do I need to declare two hashmaps?

Many Thanks

Andy
 
L

Lew

andymconline said:
Hello,

I'm hoping someone can help me. I am trying to use a map-entry to
store and display the status of an entity in the database. I have the
following managed-bean defined in my faces-config.xml file:

<managed-bean>
<managed-bean-name>RPHandler</managed-bean-name>
<managed-bean-class>com.sett.backing.RPHandler</managed-bean-class>
<managed-bean-scope>session</managed-bean-scope>
<managed-property>
<property-name>ratePlan</property-name>
<value>#{ratePlanBean}</value>
</managed-property>
<managed-property>
<property-name>statusMap</property-name>
<property-class>java.util.HashMap</property-class>
<map-entries>
<map-entry>
<key>In Development</key>
<value>D</value>
</map-entry>
<map-entry>
<key>Active</key>
<value>A</value>
</map-entry>
<map-entry>
<key>Not For Sale</key>
<value>N</value>
</map-entry>
</map-entries>
</managed-property>
</managed-bean>


In my ratePlanForm.jsp file I have the following code to display a
SELECT box:

<h:selectOneMenu id="status" value="#{RPHandler.ratePlan.status}">
<f:selectItems value="#{RPHandler.statusMap}" />
<h:selectOneMenu>

Which creates the following HTML output:

<select id="status"><option value="D>In Development</option><option
value="A">Active</option><option value="N">Not For Sale</option></
select>

This is exactly what I want, however when I then try and show a list
of ratePlans and their associated status's I use:

<h:eek:utputText value="#{RPHandler.statusMap[ratePlan.status]}" />

Which obviously shows nothing as ratePlan.status is set to the value
from the SELECT box ("A" for example) and their is no key equal to A
in the HashMap (as I have had to declare the HashMap with the
descriptions in the key field so that the selectItems JSF tag displays
the descriptions in the SELECT box). How to I get the outputText to
show the textual description of A? Do I need to declare two hashmaps [sic]?

Nah, you need a List of a custom type that holds the attributes of interest
(name, for example). Return the entire CustomType object as the value, or
keep a Map of the CustomType objects on the server only, indexed by the
returned letter (Map <String, CustomType>). Then you display the returned
object's "name" attribute (or whatever you call it) wherever you need to.

By convention, attribute and variable names begin with a lower-case letter
('rpHandler').
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top