动态JSF(2.0)commandButtons - 使用参数设置动作 [英] Dynamic JSF (2.0) commandButtons - set action with arguments

查看:137
本文介绍了动态JSF(2.0)commandButtons - 使用参数设置动作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试向我的网页动态添加JSF < h:commandButtons> ,到目前为止,我已经显示,但是我无法使用参数设置动作,就像我可以在一个静态页面中:

action =#{bean.function(parameter)}。 (这当然是使用EL-2.2)

看看我发现我必须创建一个 MethodExpression ,但这对我和我来说是模糊的没有能够找到很多关于此的信息。如果有人可以通过雾光照亮,并解释如何做到这一点,将不胜感激。



编辑:所以现在我有这个

  public void displayNode(String childName){
//许多凌乱的代码实例化JSF组件

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

parentLabel.setId(label+ count ++); //我真的很讨厌不得不使用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表达式与在普通视图文件中使用的完全相同。 p>




更新这里是一个SSCCE,对于我,与Mojarra 2.1.12在Tomcat 7.0.27 。

 <!DOCTYPE html> 
< html xmlns =http://www.w3.org/1999/xhtml
xmlns:h =http://java.sun.com/jsf/html
>
< h:head>
< title> SO问题12098611< / title>
< / h:head>
< h:body>
< h:form binding =#{bean.form}>
< h:commandButton value =addaction =#{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);
}

}






不相关的具体问题,以上所有都是不好的做法。另请参阅 JSF中的'binding'属性如何工作?何时以及如何使用?


I'm trying to add JSF <h:commandButtons> dynamically to my webpage, and so far I have them displaying, but I cannot set the action with parameters, like I could in a static page:
action="#{bean.function(parameter)}". (this is of course using EL-2.2)
Looking around I find that I have to create a MethodExpression, but this is obscure to me and I haven't been able to find much information on this. If someone could shine a light through the fog and explain how this can be done, it would be greatly appreciated.

EDIT: so now I have this

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 );
    }

解决方案

Use ExpressionFactory#createMethodExpression().

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

Here's a convenience method:

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

The following action method examples:

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)

can then be created as follows:

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);

Note that the EL expression is exactly the same as you would use in the normal view file.


Update here's an SSCCE which works fine for me with Mojarra 2.1.12 on Tomcat 7.0.27.

<!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);
    }

}


Unrelated to the concrete problem, all of above is a poor practice. See also How does the 'binding' attribute work in JSF? When and how should it be used?

这篇关于动态JSF(2.0)commandButtons - 使用参数设置动作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆