您好我尝试在Primefaces的DefaultMenuItem的某些属性(例如属性类)上设置值expresion。类似的东西:
<p:menuitem class="#{view.viewId.endsWith('index.xhtml') ? 'menuitm-hovered' : ''}" value="Home" url="/" />
exaclty这些和平
class="#{view.viewId.endsWith('index.xhtml') ? 'menuitm-hovered' : ''}"
但是编程方面。在bean上应该是这样的:
menuItem.setValueExpression("class", Helper2.createValueExpression("#{view.viewId.endsWith('index.xhtml') ? 'menuitm-hovered' : ''}", Object.class));
DefaultMenuItem dosent有setValueExpression方法所以我卡住了,我不知道我怎么能这样做。如果有人可以提供帮助,我将非常感激您的帮助。
据我所知,setValueExpression可以在GraphicImage中完成(在这种情况下使用value属性)
GraphicImage image = new GraphicImage();
image.setValueExpression("value", Helper2.createValueExpression("#{position.images.link}", Object.class));
Helper2类的静态方法:
public static ValueExpression createValueExpression(String expression, Class clazz) {
FacesContext fc = FacesContext.getCurrentInstance();
ELContext elContext = fc.getELContext();
ExpressionFactory expFactory = fc.getApplication().getExpressionFactory();
ValueExpression ret = expFactory.createValueExpression(elContext, expression, clazz);
return ret;
}
答案 0 :(得分:1)
试试这个:
MenuButton menuButton = new MenuButton();
item = new UIMenuItem();
item.setValue(command.getLabel());
item.setActionExpression(Utility.createMethodExp(command.getCommandExpression()));
item.setValueExpression("disabled", Utility.createExpression(command.getDisabledExpression(), Boolean.class));
menuButton.getChildren().add(item);
其中Utility.createMethodExpression是一个静态方法(我的类Utility):
public static MethodExpression createMethodExp(String stringExpression) {
FacesContext context = FacesContext.getCurrentInstance();
return context.getApplication().getExpressionFactory().createMethodExpression(Utility.getELContext(),
stringExpression, Void.class, new Class[] {});
}
和Utility.createExpression是
public static ValueExpression createExpression(String stringExpression, Class<?> type) {
FacesContext context = FacesContext.getCurrentInstance();
return context.getApplication().getExpressionFactory().createValueExpression(context.getELContext(),
stringExpression, type);
}