MSC.Software India User Conference 2008

If you are working in the CAE and Engineering Simulation industry, dont miss out on this exciting opportunity to hear about the latest trends, tools and technologies from the industry leader, MSC Software, our partners and customers.


Early bird registration has closed. If you still have not registered, hurry up!!!

RichFaces Dynamic Menu, Finding Ajax Form, Custom Method Expression and Parameterized Action

Here is a common scenario you will come across while using JSF with RichFaces

Creating Dynamic RichMenu
Register the menu component in your view (Facelets or JSP)

<rich:dropdownmenu binding="#{bean.menuComponent}" value="File">

In the backing bean, use the component binding to create Menu Items programmatically
private HtmlDropDownMenu menuComponent;

public HtmlDropDownMenu getMenuComponent() {
FacesContext ctx = FacesContext.getCurrentInstance();
Application app = ctx.getApplication();
if(null == menuComponent)
menuComponent = (HtmlDropDownMenu)app.createComponent(HtmlDropDownMenu.COMPONENT_TYPE);
for(String key: map.keySet()) {
HtmlMenuItem item = (HtmlMenuItem)app.createComponent(HtmlMenuItem.COMPONENT_TYPE);
Class[] params = {};
MethodExpression actionExpression = app.getExpressionFactory()
String.class, params);
String onSelect = "open(this,'"+map.get(key).toString()+"')";
return menuComponent;

Notice how you can programmatically create a Mexpression in JSF 1.2 via UEL
Also, see how JavaScript event is wired to the dynamically created RichMenuItem

Finding the Form
RichFaces has a convenient JavaScript function A4J.findForm which you can use to locate the form corresponding to the Menu Item

function open(el, paramValue) {
var frm = A4J.findForm(el);
if(frm == null) {
alert("open: no form found.");
//add hidden parameter
var hidden = document.createElement('input');
hidden.setAttribute('name', 'PARAM_NAME');
hidden.setAttribute('type', 'hidden');
hidden.setAttribute('value', paramValue);


Managed Bean Action method
From the action method, find the value of interest and take appropriate action

public void menuAction (){
FacesContext facesContext = FacesContext.getCurrentInstance();
Application app = facesContext.getApplication();
Map reqMap = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap();
String param = reqMap.get(name).toString();

Feel free to use this code in your applications.
If you found this useful, please leave your comments / feedbacks that would prove helpful for the developer community.