DefaultMenuItem with a value from a .properties file (resource bundle)

萝らか妹 提交于 2020-01-06 12:44:09

问题


Ok so I'm trying to move my project from Primefaces 3.5 to 4.0 and I'm struggling with the new MenuModel API. I'm sure it's something stupid, but I can't display a DefaultMenuItem for my Breadcrumb where the value is from a .properties file (defined in faces-config as resource bundle).

What I had with Primefaces 3.5 :

        MenuItem menuItem;

        // Where "getPath()" returns all the pages needed to get to the current one
        for (Page page : currentPage.getPath()) {
            menuItem = new MenuItem();
            // Where "getTitle()" returns something like "#{message.home}" which will be resolved as "Home"
            menuItem.setValueExpression("value", expressionFactory.createValueExpression(elContext, page.getTitle(), String.class));
            menuItem.setActionExpression(expressionFactory.createMethodExpression(elContext, page.getAction(), String.class, new Class[0]));
            menuItem.setAjax(false);
            menuItem.setAsync(false);
            menuItem.setImmediate(true);
            breadCrumbModel.addMenuItem(menuItem);
        }

What I tried with Primefaces 4.0 and the new DefaultMenuItem object :

menuItem.setValue(page.getTitle());

menuItem.setValue(expressionFactory.createValueExpression(elContext, tmp.getTitle(), String.class));

menuItem.setParam("value", expressionFactory.createValueExpression(elContext, tmp.getTitle(), String.class));

menuItem.setTitle(page.getTitle()); <--- I even tried that in despair haha

I looked up the user guide and the api doc (uncommented :( ) but it doesn't talk much about this scenario, most of the time the examples use plain ol' String. What am I doing wrong ?

Thanks.


回答1:


After having the same issue i found this solution and it works!, i'm using primefaces 5.0:

public void addMiga(String action, String label) throws Exception {
    DefaultMenuItem item = new DefaultMenuItem();
    item.setFragment(label);
    String valueTitulo = "";
    HtmlOutputText seccionLabel = new HtmlOutputText();
    if (label != null && !"".equals(label)) {
        String[] expresiones = label.split(",");
        for (String expresion : expresiones) {
            if (expresion.contains("label_")
                    || expresion.contains("_label")) {
                seccionLabel.setValueExpression("value",
                        getValueExpression(expresion));

            } else {
                seccionLabel.setValue(expresion);
            }
            valueTitulo += " " + (String) seccionLabel.getValue();
        }
    }
    item.setValue(valueTitulo);
    item.setId(Integer.toString(this.migas.getElements().size()));
    item.setOnclick(ConstantesErp.FUNCION_STATUS);
    item.setCommand(action);
    item.setAjax(false);
    this.migas.addElement(item);
}

public static ValueExpression getValueExpression(String nombre) {
        FacesContext fc = FacesContext.getCurrentInstance();
        Application app = fc.getApplication();
        ELContext elContext = fc.getELContext();
        ExpressionFactory factory = app.getExpressionFactory();
        ValueExpression ve = null;
        try {
            ve = factory.createValueExpression(elContext, "#" + "{" + nombre
                    + "}", String.class);
        } catch (Exception e) {
            ve = null;
        }
        return ve;
    }


来源:https://stackoverflow.com/questions/21914562/defaultmenuitem-with-a-value-from-a-properties-file-resource-bundle

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!