web-dev-qa-db-ja.com

入力値をBeanプロパティにバインドせずに、入力テキスト値をBeanメソッドに渡す

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

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

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

37
membersound

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

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

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

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

こちらもご覧ください:

51
BalusC

リクエストを取得し、プレーン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);
    }
}

より多くの情報を持つ別のスレッド:

15
Luiggi Mendoza