DefaultMenuItem,带有来自.properties文件的值(资源包) [英] DefaultMenuItem with a value from a .properties file (resource bundle)
问题描述
好,所以我试图将我的项目从Primefaces 3.5移到4.0,并且我在努力使用新的MenuModel API.我敢肯定这很愚蠢,但是我无法为我的面包屑显示DefaultMenuItem,其中的值来自.properties文件(在faces-config中定义为资源包).
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).
我对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);
}
我尝试使用Primefaces 4.0和新的DefaultMenuItem对象:
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());
< ---我什至在绝望中尝试过哈哈
menuItem.setTitle(page.getTitle());
<--- I even tried that in despair haha
我查阅了用户指南和api文档(未注释:(),但是在大多数情况下,示例使用的是纯字符串'.关于这种情况的讨论不多.我在做什么错了?
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 ?
谢谢.
推荐答案
在遇到同样的问题后,我发现了该解决方案并且可以使用!,我使用的是primefaces 5.0:
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;
}
这篇关于DefaultMenuItem,带有来自.properties文件的值(资源包)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!