• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to bind to an object inside HashMap.  RSS feed

 
Rahul Santha
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a text box in a JSP.
Value of the textbox is present in a DO.
DO is present inside a HashMap.

How do I write the expression in the textbox {#.. blah blah..} to point to the value and display it? Below is my code:

DataObject:
public class PageNameDO {

private String whereAmI = "";

public PageNameDO(String val){
whereAmI = val;
}
//getters and setters
}

BackingBean with HashMap inside it:
public class HashBind extends PageCodeBase {

private HashMap<String, PageNameDO> hash1 = null;

public HashBind(){
hash1 = new HashMap<String, PageNameDO>();
hash1.put("A", new PageNameDO("Apple"));
}

//getters and setters for hashmap
}

JSP Page:
<h:inputText styleClass="inputText"
id="text1"
value="#{pc_HashBind.hash1[0].pageNameDO.whereAmI}">
</h:inputText>

How to display the value inside the text box?

Help!!
 
Tim Holloway
Bartender
Posts: 18531
61
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm going to simplify that request a little to make it clearer, but you should be able to build it up from there.

Let's say you want to display/modify the element in Map myMap in your backing bean that's stored under the key "fruit".

First, you define the property "myMap" with its setter and getter methods. Actually probably just a getter since you probably don't want to replace the whole map.

Now in the input control, you code the value attribute using Java EL:

value="#{myMap['fruit']}"

That's all there is to it.

The unified Expression Language is fairly well documented in the Sun JEE reference documents. A lot of J(2)EE systems use an open-source EL interpreter: the jakarta commons EL project (http://commons.apache.org/el/), which is based on the Jakarta BeanUtils. The BeanUtils are used to resolve property references, so their documentation can be especially helpful.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!