gpt4 book ai didi

jsf - 动态JSF(2.0)commandButtons-使用参数设置操作

转载 作者:行者123 更新时间:2023-12-04 10:01:21 25 4
gpt4 key购买 nike

我正在尝试将JSF <h:commandButtons>动态添加到我的网页,到目前为止,我一直在显示它们,但是我无法像在静态页面中那样使用参数设置操作:action="#{bean.function(parameter)}"。 (这当然是使用EL-2.2)
环顾四周,我发现我必须创建一个MethodExpression,但这对我来说是晦涩的,因此我无法找到很多有关此的信息。如果有人可以在雾中照亮并解释如何做到这一点,将不胜感激。

编辑:所以现在我有这个

public void displayNode( String childName ){
//lots of messy code instantiating JSF components

if( activeEmployee.getParent() != null ){
HtmlCommandButton parent = new HtmlCommandButton();
HtmlOutputText parentLabel = new HtmlOutputText();

parentLabel.setId("label" + count++); //I really hate having to use count
parentLabel.setValue( "Parent: " );

parent.setId("Parent" + count++);
String parentName = activeEmployee.getParent().getName();
parent.setValue( parentName );
MethodExpression expression = createMethodExpression("#{tree.displayNode('" + parentName + "')}",
null, String.class);
parent.setActionExpression( expression );

newDiv.getChildren().add( parentLabel );
newDiv.getChildren().add( parent );
}

最佳答案

使用 ExpressionFactory#createMethodExpression()

public abstract MethodExpression createMethodExpression(
ELContext context,
java.lang.String expression,
java.lang.Class<?> expectedReturnType,
java.lang.Class<?>[] expectedParamTypes)


这是一种方便的方法:
public static MethodExpression createMethodExpression(String expression, Class<?> returnType, Class<?>... parameterTypes) {
FacesContext facesContext = FacesContext.getCurrentInstance();
return facesContext.getApplication().getExpressionFactory().createMethodExpression(
facesContext.getELContext(), expression, returnType, parameterTypes);
}

以下操作方法示例:
public void submit1()
public String submit2()
public void submit3(String argument)
public String submit4(String argument)
public void submit5(String argument1, Long argument2)
public String submit6(Long argument1, String argument2)

然后可以如下创建:
createMethodExpression("#{bean.submit1}", null);
createMethodExpression("#{bean.submit2}", String.class);
createMethodExpression("#{bean.submit3('foo')}", null, String.class);
createMethodExpression("#{bean.submit4('foo')}", String.class, String.class);
createMethodExpression("#{bean.submit5('foo', 0)}", null, String.class, Long.class);
createMethodExpression("#{bean.submit6(0, 'foo')}", String.class, Long.class, String.class);

请注意,EL表达式与普通 View 文件中使用的表达式完全相同。

更新,这是一个SSCCE,与Tomcat 7.0.27上的Mojarra 2.1.12配合使用对我来说很好。

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
>
<h:head>
<title>SO question 12098611</title>
</h:head>
<h:body>
<h:form binding="#{bean.form}">
<h:commandButton value="add" action="#{bean.add}" />
</h:form>
</h:body>
</html>

@ManagedBean
@RequestScoped
public class Bean {

private UIForm form;

public void add() {
String id = "button" + form.getChildCount();
UICommand button = new HtmlCommandButton();
button.setId(id);
button.setValue(id);
button.setActionExpression(createMethodExpression(String.format("#{bean.submit('%s')}", id), null, String.class));
form.getChildren().add(button);
}

public void submit(String arg) {
System.out.println("submit: " + arg);
}

public UIForm getForm() {
return form;
}

public void setForm(UIForm form) {
this.form = form;
}

public static MethodExpression createMethodExpression(String expression, Class<?> returnType, Class<?>... parameterTypes) {
FacesContext facesContext = FacesContext.getCurrentInstance();
return facesContext.getApplication().getExpressionFactory().createMethodExpression(
facesContext.getELContext(), expression, returnType, parameterTypes);
}

}

与具体问题无关的,以上都是不好的做法。另请参阅 How does the 'binding' attribute work in JSF? When and how should it be used?

关于jsf - 动态JSF(2.0)commandButtons-使用参数设置操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12098611/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com