40

値をBeanプロパティにバインドせずに、入力テキストフィールド値をBeanメソッドに渡すことはできますか?

<h:inputText value="#{myBean.myProperty}" />
<h:commandButton value="Test" action="#{myBean.execute()} />

一時的に保存せずにこれを行うことはできます#{myBean.myProperty}か?

4

2 に答える 2

56

UIInputビューに関してコンポーネントをバインドし、UIInput#getValue()メソッド引数としてその値を渡すために使用します。

<h:inputText binding="#{input1}" />
<h:commandButton value="Test" action="#{myBean.execute(input1.value)}" />

public void execute(String value) {
    // ...
}

値はこの方法ですでに変換され、通常のJSFの方法で検証されていることに注意してください。

参照:

于 2012-08-13T22:33:50.520 に答える
18

リクエストを取得し、プレーンなJava EE ServletRequest#getParameterを使用することで、フォームのパラメーターを回復できます。この方法を使用するときは、コンポーネントのIDと名前を設定することを忘れないでください。

<h:form id="myForm">
    <h:inputText id="txtProperty" /> <!-- no binding here -->
    <input type="text" id="txtAnotherProperty" name="txtAnotherProperty" />
    <h:commandButton value="Test" action="#{myBean.execute()} /> 
</h:form>

マネージドBean:

@ManagedBean
@RequestScoped
public class MyBean {
    public void execute() {
        HttpServletRequest request = (HttpServletRequest)FacesContext.getCurrentInstance().getExternalContext().getRequest();
        String txtProperty = request.getParameter("myForm:txtProperty");
        //note the difference when getting the parameter
        String txtAnotherProperty= request.getParameter("txtAnotherProperty");
        //use the value in txtProperty as you want...
        //Note: don't use System.out.println in production, use a logger instead
        System.out.println(txtProperty);
        System.out.println(txtAnotherProperty);
    }
}

詳細情報のある別のスレッド:

于 2012-08-13T21:26:24.843 に答える