Pass input text value to bean method without binding input value to bean property
Asked Answered
C

2

40

Can I pass a input text field value to a bean method without binding the value to a bean property?

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

Can I do this without doing temporary save in #{myBean.myProperty}?

Caddis answered 13/8, 2012 at 20:39 Comment(2)
What would you accomplish by doing this (In other words why would you want to)?Beguile
I would save me several properties that are only used as a "workaround" for not having real input parameters.Caddis
T
56

Bind the component as UIInput to the view and use UIInput#getValue() to pass its value as method argument.

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

with

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

Note that the value is this way already converted and validated the usual JSF way.

See also:

Typesetter answered 13/8, 2012 at 22:33 Comment(4)
What way is is recommended? This one, or a additional property in the mananged bean?Titograd
@Alexander: Technically, it depends on the model. If you can, make model as slick as possible. Apparently, if the answer works for you, you can do so. But, functionally, this approach may confuse the starter/maintainer who's after you on the code and perhaps steer him/her in the wrong direction as to understanding why exactly you did so. So, I'd personally add a nice <!-- comment --> explaining why you didn't declare a separate model property.Typesetter
This answer does work nicely. I just wonder, because I have never seen this before. Thanks, I will do so.Titograd
Will it be possible in this way to edit inputText button's value and then pass it back to bean?Isosceles
G
18

You can recover the parameters of the form by getting the Request and using plain Java EE ServletRequest#getParameter. When you use this method, remember to set the id and name of your components:

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

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

Another thread with more info:

Guddle answered 13/8, 2012 at 21:26 Comment(5)
How to retrieve value from input box if it is inside a panel which itself is inside a form i.e. <form id="myform"><label id="mylabel"><input id = "myinput"/></label></form>Isosceles
@AjaySharma your input lacks a name. Set a name then retrieve the value by its nameGuddle
Tried but doesn't work. If input field is immediate to form field it works, but if it is inside any panel or any other tag it doesn'tIsosceles
Uh... Have you checked the generated html to see the name of the component? In jsf the name is usually the parent container plus : plus the id of the component, that's why you're missing it...Guddle
Thanks Luiggi, but I tried all the options. In the end I did a workaround by using hidden input tag just below the form tag. I was able to retrieve value simply by using getRequest().getParameter("param_name")Isosceles

© 2022 - 2024 — McMap. All rights reserved.