2012-08-23 17 views
7

JSFを私のWebページに動的に追加しようとしていますが、これまで表示していましたが、静的ページ:
action="#{bean.function(parameter)}"。 (これはもちろんEL-2.2を使用しています)
私はMethodExpressionを作成する必要がありますが、これはわかりません。誰かが霧で光を照らし、これがどうやってできるかを説明すれば、それは大いに評価されるでしょう。動的JSF(2.0)commandButtons - 引数を指定してアクションを設定する

編集:今、私はこの

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

レンダリングされたプロパティを使用してコマンドボタンを表示/非表示にできませんか? – RajV

+0

nah私は他の関連ノードを表示するためのボタンでツリーノードを動的に作成しています –

答えて

17

使用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式は、通常のビューファイルで使用するものとまったく同じであることに注意してください。


更新は、ここでのTomcat 7.0.27にクロサギ科2.1.12と私のために正常に動作SSCCEです。具体的な問題へ

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

} 

無関係

<!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> 

、上記のすべてが悪い習慣です。関連項目 How does the 'binding' attribute work in JSF? When and how should it be used?

+1

もう一度、あなたはその人です。私はそれを作ったが、JSF 1.2を使っているので、私はインターネット上でこの情報を探していた。 –

+0

私は、 'button.setActionExpression(...)'を使う前に、私が前もって「MethodExpression」を必要としていたと思います。明白な引数のほかに、 'setActionExpression()'と 'setAction()'の違いは何ですか? –

+1

@Jake: 'setAction()'は[廃止予定]です(http://docs.oracle.com/javaee/6/api/javax/faces/component/UICommand.html#setAction(javax.faces.el.MethodBinding) )) "残ったEL"を使用してJSF 1.xを残しました。これは、独自のEL実装を 'ValueBinding'、' MethodBinding'などで使用しました(後でJSP ELで統一されました)。 JSF 2.xで廃止予定のAPIを使用しないでください**。それらは除去の対象です。 ELの歴史については、http://stackoverflow.com/questions/4812755/difference-between-jsp-el-jsf-el-and-unified-elも参照してください。 – BalusC

関連する問題