question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

DefaultMenuItem: EL is not calculated in setStyleClass method

See original GitHub issue

1) Environment

  • PrimeFaces version: 7.0.RC3
  • Does it work on the newest released PrimeFaces version? Version? No (7.0.RC3)
  • Does it work on the newest sources in GitHub? (Build by source -> https://github.com/primefaces/primefaces/wiki/Building-From-Source) not tested
  • Application server + version: Wildfly 7.0.0.Final
  • Affected browsers: Google Chrome

2) Expected behavior

EL expression assigned to the item by method DefaultMenuItem.setStyleClass() is correctly interpreted and produce the required behavior of dynamic menu component.

3) Actual behavior

EL expression assigned to the item by method DefaultMenuItem.setStyleClass() treated as a simple string and assigned to final HTML item class as it was passed into the method. Example of how it’s looks a tabindex="-1" class="ui-menuitem-link ui-corner-all #{"1".equals(categories.category) ? "ui-state-active" : ""}">

4) Steps to reproduce

Create a managed bean and XHTML pattern as explained below and run

5) Sample XHTML

<p:panelMenu id="id-categories" model="#{categories.model}" expanded="true"/>

6) Sample bean

@ManagedBean(name = "categories")
@SessionScoped
public class CategoriesBean {

    private static String CATEGORY_ID ="categoryId";

    private DynamicMenuModel model;

    @Inject
    private CategoryRepository categoryRepository;

    private Category category;

    public Category getCategory() {
        return category;
    }

    public void setCategory(Category category) {
        this.category = category;
    }

    public Collection<Category> getAll() {
        return categoryRepository.getAll();
    }

    @PostConstruct
    private void init() {
        model = new DynamicMenuModel();

        DefaultSubMenu categoriesSubmenu = new DefaultSubMenu("Categories");
        categoriesSubmenu.setExpanded(true);

        for (Category category : categoryRepository.getAll()) {
            categoriesSubmenu.addElement(createMenuItem(String.valueOf(category.getId()), category.getName()));
        }
        model.addElement(categoriesSubmenu);

        model.generateUniqueIds();
    }

    public MenuModel getModel() {
        return model;
    }

    public String actionSelectCategory(ActionEvent event) {
        MenuItem menuItem = ((MenuActionEvent) event).getMenuItem();
        String id = menuItem.getParams().get(CATEGORY_ID).get(0);

        this.category = categoryRepository.getById(Long.valueOf(id));
        return "/index.xhtml?faces-redirect=true";
    }

    private MenuItem createMenuItem(String id, String name) {
        DefaultMenuItem item = new DefaultMenuItem(name);
        item.setId(id);
        item.setCommand("#{categories.actionSelectCategory}");
        item.setParam(CATEGORY_ID, id);
        item.setStyleClass("#{\"" + id + "\".equals(categories.category) ? \"ui-state-active\" : \"\"}");
        return item;
    }
}

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:8 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
tandraschkocommented, Sep 28, 2019

As you are the first one since years, i suggest you to use the following workaround:

class MyDefaultMenuItem extends DefaultMenuItem {    
    @Override    
    public String getStyleClass() {    
        FacesContext facesContext = FacesContext.getCurrentInstance();    
        return facesContext.getApplication().evaluateExpressionGet(facesContext, super.getStyleClass(), String.class);    
    }    
}    
0reactions
ansurakincommented, Dec 3, 2020

Thanks! My be need create property like: boolean evaluateExpression

Read more comments on GitHub >

github_iconTop Results From Across the Web

Primefaces menu with EL as class StyleClass - Stack Overflow
The problem is that EL for style is not calculated and I see it fully in the browser but not only proper style...
Read more >
DefaultMenuItem (PrimeFaces 8.0 API)
A MethodExpression in the form of a string which is called after the menu item has been clicked. It is ignored when url...
Read more >
NetAdvantage for JSF - ComponentSource CDN
It does not work because the following method. In Java: getRowCount() returns -1. The number of rows in the ResultSet cannot be determined...
Read more >
Index (Struts 1.3.9 API)
actionIdURL(String, ModuleConfig, ActionServlet) - Static method in class ... keys to an internal cache, creating the cache if one is not already present....
Read more >
2014 - Thoughts on software development
Well, why not to pick any jQuery based plugin for responsive multi-level menus? ... #{price} is a current price as EL expression calculated...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found