Rectangle 27 49

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.

What way is is recommended? This one, or a additional property in the mananged bean?

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

Will it be possible in this way to edit inputText button's value and then pass it back to bean?

jsf - Pass input text value to bean method without binding input value...

jsf parameter-passing managed-bean
Rectangle 27 48

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.

What way is is recommended? This one, or a additional property in the mananged bean?

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

Will it be possible in this way to edit inputText button's value and then pass it back to bean?

jsf - Pass input text value to bean method without binding input value...

jsf parameter-passing managed-bean
Rectangle 27 13

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

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>

@AjaySharma your input lacks a name. Set a name then retrieve the value by its name

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

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

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

jsf - Pass input text value to bean method without binding input value...

jsf parameter-passing managed-bean
Rectangle 27 12

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

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>

@AjaySharma your input lacks a name. Set a name then retrieve the value by its name

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

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

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

jsf - Pass input text value to bean method without binding input value...

jsf parameter-passing managed-bean
Rectangle 27 1

The standard approach is the following:

<h:form> 
    <p:inputText value="#{myClass.inputValue}"/> 
    <p:commandButton value="Submit" action="#{myClass.doSomething}"> 
</h:form>
@ManagedBean
@ViewScoped
public class myClass {
    private String inputValue;

    // getter and setter for inputValue

    public void doSomething() {
    }
}

When the user clicks the button, before doSomething is invoked, JSF cares about reading the value inputed in the inputText component, validating it if necessary, and invoking setter class for inputValue as per the EL expression #{myClass.inputValue} (note that EL automagically understands that the setter must be invoked in this case). This is very basic JSF. I recommend to study some Java EE 6 tutorial (this, for example).

i mean if i set inputext value from different object But i want to pass the user input value as method parameters. <p:inputText value="#{loginHandler.currentuser.name}"> <p:commandButton value="Update" action="myClass.update( user Input as parameters)">

java ee - how to pass input text value as method parameter from JSF pa...

jsf java-ee
Rectangle 27 18

You need to put all <h:inputXxx>/<h:selectXxx> components in a <h:form> and bind their value attribute to a bean property via an EL expression like #{bean.property}, backed by a getter/setter pair. When properly set, JSF will automatically set the values in the bean when the form is submitted via a <h:commandXxx> component in the very same form. You can specify a bean action method in action attribute of the <h:commandXxx> component via an EL expression like #{bean.action}, which points to the bare method action(). All submitted values are available right away there the usual Java way.

Given this JSF form example with one input field and one select field:

<h:form>
    <h:inputText value="#{bean.text}" required="true" />
    <h:selectOneMenu value="#{bean.choice}" required="true">
        <f:selectItem itemValue="#{null}" />
        <f:selectItem itemValue="One" />
        <f:selectItem itemValue="Two" />
        <f:selectItem itemValue="Three" />
    </h:selectOneMenu>
    <h:commandButton value="submit" action="#{bean.submit}" />
    <h:messages />
    <h:outputText value="#{bean.result}" />
</h:form>

The following bean prints the submitted values to the stdout, proving that JSF has already set the values long before the moment you access it in the action method.

package com.example;

import javax.inject.Named;
import javax.enterprice.context.RequestScoped;

@Named // Use @javax.faces.bean.ManagedBean on outdated environments.
@RequestScoped // Use @javax.faces.bean.RequestScoped on outdated environments.
public class Bean {

    private String text;
    private String choice;
    private String result;

    public void submit() {
        result = "Submitted values: " + text + ", " + choice;
        System.out.println(result);
    }

    public String getText() {
        return text;
    }

    public void setText(String text) {
        this.text = text;
    }

    public String getChoice() {
        return choice;
    }

    public void setChoice(String choice) {
        this.choice = choice;
    }

    public String getResult() {
        return result;
    }
}

That's all. Turning the regular form into an ajax form is a matter of nesting a <f:ajax> in the command component as below.

<h:commandButton value="submit" action="#{bean.submit}">
    <f:ajax execute="@form" render="@form" />
</h:commandButton>

You can find another example and valuable links at bottom of our JSF wiki page.

Do note that whatever you intend to do with the submitted values is beyond the responsibility of JSF. For example, manipulating it, passing into another class, saving it in database, etc. None of this all is related to JSF. It has as being a HTML form based framework already done its job of providing you the submitted values in flavor of usable Java variables. The remainder is up to you.

To investigate the next step, you should at this point just be doing as if you've already a bunch of prepared / hardcoded variables instead of a whole JSF based user interface. For example, in order save to the values in a database, people usually use a business service layer framework like EJB which in turn uses a persistence layer framework like JPA. Some people even use "plain vanilla" JDBC for that. For more links to concrete examples, start here: JSF Controller, Service and DAO.

If we assume there was an entity class which has text and number getters/setters, how it is possible to use that entity instead of defining the getters/setters again?

@hallaji Continue the last link in the answer.

How to send form input values and invoke a method in JSF bean - Stack ...

jsf input submit facelets managed-bean
Rectangle 27 3

Why not use a4j:commandButton instead of h:commandButton? It will execute ajax request and render what you want. No form submit will happen. Loks like what you need.

Yes, this helped. I have added the whole solution as another answer. for better formatting.

ajax - passing input text's value to server side on click of a button ...

ajax jsf richfaces ajax4jsf jsf-1.1
Rectangle 27 2

This is JSF's safeguard against tampered/hacked requests wherein the enduser uses client side languages/tools like HTML or JS to manipulate the HTML DOM tree and/or HTTP request parameters in such way that the outcome of JSF's disabled, readonly or even rendered attribute is altered.

Imagine what would happen if the JSF developer checked an user's role in such a boolean attribute against the admin role like so disabled="#{not user.hasRole('ADMIN')}" and a hacker manipulated it in such way that it isn't disabled anymore for non-admin users. That's exactly why you can only change the mentioned attributes (and the required attribute and all converters, validators, event listeners, etc) on the server side.

This can however be solved in a simpler manner without the need for an additional property, without the <p:remoteCommand> and the additional bean method.

<h:inputText id="violationAmount" ... readonly="#{makeViolationAmountEditable.value}" />
...
<h:selectBooleanCheckbox binding="#{makeViolationAmountEditable}">
    <f:ajax render="violationAmount" />
</h:selectBooleanCheckbox>

This is in detail explained in 2nd "See also" link

1) There you use component's value attribute for. 2) Sorry? Is <f:ajax> also new to you? I understand that binding is as least understood feature usually not explained in depth, but <f:ajax> should be already covered in the 1st chapters of a sane JSF2 book/tutorial. It just fires an ajax request on the default HTML DOM event and the render attribute tells JSF what components to re-render (update) in the response. This way the checkbox component's value is re-evaluated in readonly attribute.

In other words, you haven't even tried the given code? The code is just complete and as-is, as usual in my answers. Aren't you just astonished by its simplicity? How it works is already in depth explained in the given links. To continue on the last given link, the "view build time" versus "view render time" is in turn in depth explained in stackoverflow.com/a/3343681

primefaces - Confusion JSF Updating Value of Input text in Backing bea...

jsf primefaces
Rectangle 27 2

This is JSF's safeguard against tampered/hacked requests wherein the enduser uses client side languages/tools like HTML or JS to manipulate the HTML DOM tree and/or HTTP request parameters in such way that the outcome of JSF's disabled, readonly or even rendered attribute is altered.

Imagine what would happen if the JSF developer checked an user's role in such a boolean attribute against the admin role like so disabled="#{not user.hasRole('ADMIN')}" and a hacker manipulated it in such way that it isn't disabled anymore for non-admin users. That's exactly why you can only change the mentioned attributes (and the required attribute and all converters, validators, event listeners, etc) on the server side.

This can however be solved in a simpler manner without the need for an additional property, without the <p:remoteCommand> and the additional bean method.

<h:inputText id="violationAmount" ... readonly="#{makeViolationAmountEditable.value}" />
...
<h:selectBooleanCheckbox binding="#{makeViolationAmountEditable}">
    <f:ajax render="violationAmount" />
</h:selectBooleanCheckbox>

This is in detail explained in 2nd "See also" link

1) There you use component's value attribute for. 2) Sorry? Is <f:ajax> also new to you? I understand that binding is as least understood feature usually not explained in depth, but <f:ajax> should be already covered in the 1st chapters of a sane JSF2 book/tutorial. It just fires an ajax request on the default HTML DOM event and the render attribute tells JSF what components to re-render (update) in the response. This way the checkbox component's value is re-evaluated in readonly attribute.

In other words, you haven't even tried the given code? The code is just complete and as-is, as usual in my answers. Aren't you just astonished by its simplicity? How it works is already in depth explained in the given links. To continue on the last given link, the "view build time" versus "view render time" is in turn in depth explained in stackoverflow.com/a/3343681

primefaces - Confusion JSF Updating Value of Input text in Backing bea...

jsf primefaces
Rectangle 27 1

Don't use empty list. Initialize it with null or empty values. Let's say we have inputs as your list, your bean should look like this.

@Named
@ViewScoped
public class DataController implements Serializable {

    private List<String> inputs;

    // getters and setters

    @PostConstruct
    public void init() {
        inputs = new ArrayList<String>();
    }

    public List<Bean> getDisplay() {
        List<Bean> display = new ArrayList<Bean>();

        // add values to display

        for (int i = inputs.size(); i < display.size(); i++) {
            inputs.add("");
        }

        return display;
    }

    // for testing inputs
    public void testInputs() {
        for (String input : inputs) {
            System.out.println(">>>>>" + input);
        }
    }

}
<ui:repeat value="#{dataController.display()}" varStatus="idx" ...>
    ...
    <h:inputText value="#{dataController.inputs[idx.index]}" style="width:200px;height:30px;margin-bottom:15px;" autocomplete="off"></h:inputText>
    ...
</ui:repeat>

<p:commandButton value="Test Inputs" action="#{dataController.testInputs}" update="@form" />

But dataController.display() is a method which returns List<Bean> and I cant initialize display method initially in init() as it needs to be invoked after another method call.

Can u please tell me if i can use nested ui:repeat or any other approach to be suggested?

inputs
getDisplay()

Thankyou soo much for the solution..:-D It worked!!

list - Access values from dynamic input text generated from ui:repeat ...

list jsf javabeans uirepeat
Rectangle 27 1

To get managed bean value from hidden JSF input in JS you can using jQuery in following way:

First use h:inputText instead to definle class for search in jQuery ('classForSearch'). To hide input it add simple CSS class ('inpt-hidden'):

<style>
    .inpt-hidden { display: none; }
</style>

<h:inputText value="#{bean.myVariable}" styleClass="inpt-hidden classForSearch" />
<script type="text/javascript">
        function func(){
            var varFromBean = jQuery('.classForSearch').val();
            ....
        }

</script>

I would like to add that you can also give the h:inputText an ID and use the jQuery method on ID as well.

thanks a lot, this might help. But i wonder if there is some another way except the using of h:inputText? this seems to be quite unnatural one..?

Running jsf managed bean method from javascript - Stack Overflow

javascript jsf richfaces
Rectangle 27 1

I understand from there that, in the case of some validation error, the input field will keep the value entered by the user, and will be out of sync with the backing bean. I must emphasize that in my case, there were no validation errors, but still, adding an <f:ajax resetValues> to the commandButton worked for me.

resetValues
<a4j:commandButton  immediate="true"
    action="#{MyBean.clear}" render="form1" update="@form">
    <f:ajax render="code" resetValues="true" />
</a4j:commandButton>

partial page refresh - jsf inputText not refreshing when i reset the b...

jsf partial-page-refresh
Rectangle 27 10

If editableBaseSetList is an int[], then baseNumber is an int. You're now binding the input text component to this int.

This binding is however not bidirectional. The input element only gets to see the element you are binding to it, not the collection from which it originates. It thus has no knowledge of how to update this collection.

You thus need to bind to something that can be updated. If your list for instance contained an IntHolder with a getter and setter for the internal integer (say getInt() and setInt()), and the list would be ArrayList you would use:

<ui:repeat value="#{drawsetController.selected.editableBaseSetList}" var="baseNumber">
    <h:inputText value="#{baseNumber.int}"/>
</ui:repeat>

After the postback, JSF will call the setInt() method on each IntHolder in the list with the supplied values.

For a collection that already holds integers or other immutable types, it can be a bit of a hassle to convert it to such collection mentioned above. There is however also another solution. There you don't use the var attribute of ui:repeat but use its index. You then give the h:inputText a binding to the collection indexed by this index var.

Suppose you have the following bean:

import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;

@ManagedBean
@ViewScoped
public class RepeatBean {

    List<Integer> list;

    public List<Integer> getList() {
        return list;
    }

    @PostConstruct
    public void initList() {
        list = new ArrayList<Integer>();
        list.add(10);
        list.add(20);
        list.add(30);
    }

    public String save() {
        // list now contains the values provided by the user.
        return "";

    }

}

Used on the following Facelet:

<html xmlns="http://www.w3.org/1999/xhtml"
    xmlns:h="http://java.sun.com/jsf/html"        
    xmlns:ui="http://java.sun.com/jsf/facelets" 
    >

    <h:body>

        <h:form>
            <ui:repeat value="#{repeatBean.list}" varStatus="status">            
                <h:inputText value="#{repeatBean.list[status.index]}" />
            </ui:repeat>
            <h:commandButton value="Save" action="#{repeatBean.save}" />
        </h:form>

    </h:body>
</html>

This will initially display 10 20 30 on your screen. When you change the numbers and click save, you can verify via e.g. a break-point that the list instance variable contains the updated numbers.

Gee, thanks, now i'm begining to get a better feel on how jsf works.

Nice one. Note that varStatus on <ui:repeat> is not available in Facelets 1.x (for JSF 1.x), OP didn't explicitly mention that he is using JSF 2.x.

facelets - How to save an array in JSF with ui:repeat + h:inputText + ...

jsf facelets uirepeat
Rectangle 27 9

Either use <h:message> which you attach to specific components by for attribute which should refer the id of the input component:

<h:inputText id="name" value="#{contact.client.name}"></h:inputText>Name (Required)
<h:message for="name" />
<h:inputText id="email" value="#{contact.client.email}"></h:inputText>E-Mail (Required)
<h:message for="email" />
<h:inputText id="website" value="#{contact.client.website}"></h:inputText>Website (Optional)
<h:message for="website" />
<h:inputText id="text" value="#{contact.client.text}"></h:inputText>Message (Required):
<h:message for="text" />

or use <h:messages/> to display them all at a single place:

<h:messages />

Yes, bean validation messages also ends in there.

Don't forget to ensure that the button's render attribute covers them as well.

i follow your tutorial but i have a weird problem, i update my post, please coud you take a look ?

Is the concrete problem solved? Your new problem is unrelated. It look more like just a CSS issue.

yeah it is mate, thanks by the help. i will take a look in my CSS though.

It mentions it in your tutorial but javax.faces.INTERPRET_EMPTY_STRING_SUBMITTED_VALUES_AS_NULL needs to be set as a context-param in the web.xml for the @NotNull bean validator to work properly. Just for anyone wondering why your bean validation messages aren't appearing (like I was)

java - JSF: How validate fields and return error messages by bean vali...

java jsf jsf-2 bean-validation
Rectangle 27 1

It's a blur/focus javascript event handler on the input field. In jQuery it would be something like the following:

<input type="text" name="your-input" id="your-input" title="Enter a value"/>
$('#your-input').focus(function(){
    var defaultVal = $(this).attr('title');
    if($(this).val() == defaultVal)
        $(this).val('');
});
$('#your-input').blur(function(){
    var defaultVal = $(this).attr('title');
    if($(this).val() == '')
        $(this).val(defaultVal);
});
$('#your-input').trigger('blur');

It takes the default value from the input's title attribute and sets its value on focus and blur events depending on the current value of the input. The last trigger() call is so the input's value is correctly set when the page loads.

java - JSF: Prefilled values with input text field? - Stack Overflow

java jsf
Rectangle 27 3

JSF input components have 3 places where the value (the state) is stored:

String
  • Model value (when the entire form is successfully processed, stored as bean property)

JSF processes input components as follows:

  • Get HTTP request parameter value by component's client ID and store it as submitted value.
null
  • If entire form is successfully processed, set it as model value and set local value to null.

JSF renders values of input components as follows:

  • If submitted value is not null, display it.
  • Else if local value is not null, display it.

So, in your particular case of a general validation failure, JSF is for that hidden input component just redisplaying the local value instead of the model value. If you want to achieve the same with the output text, I think best is to just do the same as JSF:

<h:outputText value="#{empty hiddenId.submittedValue ? empty hiddenId.localValue ? hiddenId.value : hiddenId.localValue : hiddenId.submittedValue}" />
<h:inputHidden id="hiddenId" binding="#{hiddenId}" value="#{myBean.uid}" />

Alternatively, you could just use a read only input and remove the border by CSS if necessary:

<h:inputText id="hiddenId" value="#{myBean.uid}" readonly="true" style="border: none;" />

As to your bean, I'm not sure what's happening there as this class seems to be extremely tight coupled. I'd rather split it into 3 classes: one real backing bean, one stateless service and one model entity. Further, you should also make sure that you aren't binding view build time tags or attributes to a property of a view scoped bean. Otherwise it will indeed guaranteed be reconstructed on every single request.

eager=true
@ApplicationScoped

Thanks for your answer. I like the solution with inputText and readonly="true". Can you tell me though, why @PostConstruct is being called each time I make a POST request? How can I avoid this?

You're welcome. As to the view scope reconstruction problem, not sure. It should work fine on a @ViewScoped @ManagedBean (without all those other annotations indicating tight coupling; I can't answer reliably from top of head as I've never used this strange construct), provided that you aren't binding any view build time tags/attributes to a property of a view scoped bean. See also stackoverflow.com/a/3343681 and stackoverflow.com/a/8804588

Thanks. I am not sure if I understand what you mean by strange construct. Whas makes it strange? What should I change to make it not strange?

The thing is: All I want to do is presetting a value, which cannot be changed. If the form is submitted this value is used and processed as well...Simple thing, but this JSF stuff is kind of killing me.

1) By splitting the tightly coupled class as explained in my answer. 2) JSF is easier to understand if you understand how basic HTTP, Servlets and HTML (and JavaScript/ajax) work as JSF is basically sitting on top of that, abstracting away all the boilerplate.

jsf - Keep bean attributes after ajax form submit - Stack Overflow

ajax jsf primefaces
Rectangle 27 3

JSF input components have 3 places where the value (the state) is stored:

String
  • Model value (when the entire form is successfully processed, stored as bean property)

JSF processes input components as follows:

  • Get HTTP request parameter value by component's client ID and store it as submitted value.
null
  • If entire form is successfully processed, set it as model value and set local value to null.

JSF renders values of input components as follows:

  • If submitted value is not null, display it.
  • Else if local value is not null, display it.

So, in your particular case of a general validation failure, JSF is for that hidden input component just redisplaying the local value instead of the model value. If you want to achieve the same with the output text, I think best is to just do the same as JSF:

<h:outputText value="#{empty hiddenId.submittedValue ? empty hiddenId.localValue ? hiddenId.value : hiddenId.localValue : hiddenId.submittedValue}" />
<h:inputHidden id="hiddenId" binding="#{hiddenId}" value="#{myBean.uid}" />

Alternatively, you could just use a read only input and remove the border by CSS if necessary:

<h:inputText id="hiddenId" value="#{myBean.uid}" readonly="true" style="border: none;" />

As to your bean, I'm not sure what's happening there as this class seems to be extremely tight coupled. I'd rather split it into 3 classes: one real backing bean, one stateless service and one model entity. Further, you should also make sure that you aren't binding view build time tags or attributes to a property of a view scoped bean. Otherwise it will indeed guaranteed be reconstructed on every single request.

eager=true
@ApplicationScoped

Thanks for your answer. I like the solution with inputText and readonly="true". Can you tell me though, why @PostConstruct is being called each time I make a POST request? How can I avoid this?

You're welcome. As to the view scope reconstruction problem, not sure. It should work fine on a @ViewScoped @ManagedBean (without all those other annotations indicating tight coupling; I can't answer reliably from top of head as I've never used this strange construct), provided that you aren't binding any view build time tags/attributes to a property of a view scoped bean. See also stackoverflow.com/a/3343681 and stackoverflow.com/a/8804588

Thanks. I am not sure if I understand what you mean by strange construct. Whas makes it strange? What should I change to make it not strange?

The thing is: All I want to do is presetting a value, which cannot be changed. If the form is submitted this value is used and processed as well...Simple thing, but this JSF stuff is kind of killing me.

1) By splitting the tightly coupled class as explained in my answer. 2) JSF is easier to understand if you understand how basic HTTP, Servlets and HTML (and JavaScript/ajax) work as JSF is basically sitting on top of that, abstracting away all the boilerplate.

jsf - Keep bean attributes after ajax form submit - Stack Overflow

ajax jsf primefaces
Rectangle 27 1

First, there is no attribute called name in h:inputText, if you really need to use that attribute, please refer to that question for more details: How to specify name attribute in h:inputText?

h:inputText
<h:inputText id="number" value="#{validationView.number}" label="Number">
    <f:validateDoubleRange minimum="100" maximum="800" />
</h:inputText>

Second, in the externalContext().getRequestParameterMap().get(key) the key refers to the request parameters names included in the current request, which corresponds to the client ID (because name is autogenerated by JSF based on the client ID), so if for example your h:inputText is inside a form whose id= "form" then your client ID will be "form:number", More about When and how is clientID generated in JSF?

You can use your first method like this:

public void method(){

   String value = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("form:number");          
   System.out.println("Value: "+value);
}

In case you only know the component id of your h:inputText and not it's client ID please refer to: Find component by ID in JSF

Finnaly, I think that the best way is to use the following method which make you benefit from the ActionEvent:

public void method(ActionEvent event) {        

     String param = (String) ((UIInput) event.getComponent().findComponent("form:number")).getValue();
     System.out.println("Param: "+param);

}

NB: please note that event.getComponent() gets the component that triggered the event which is p:commandButton (in your case) and not the h:inputText

while your answer is correct, wouldn't it be more logical to just make use of the validationView.number? It will be better maintainable, better IDE support, codecompletion and refactoring wise? To me it looks like the OP has e.g. a php background, switches to java/jsfand tries to solve the X problem in a Y(/php) way

But number is in the validationView managed bean, while the action is in another managed bean. So, to use number directly he will need to inject validationView inside UserBean1, then what if validationView is requestScoped? that wouldn't be possible using JSF managed beans

That is why I think a jsf 101 is better. Also considering the other post.

But why JSF 1.1 would be better?! There is a lot of new features in JSF 2.0 he will miss in that case (I want to send some links but sorry i am answering from my mobile phone)

Hahaha, sorry my bad. 101 is in courses you can follow the beginners one no relation to jsf 1.1 english.stackexchange.com/questions/14265/

jsf 2 - Input text returning null value - Stack Overflow

jsf-2 primefaces
Rectangle 27 1

That should already be the default behaviour. You don't need to change anything on the given code example, assuming that the toString() method is properly implemented on the CustomClass. However, if it returns HTML, you'd need to add escape="false" to the output text to prevent JSF from auto-escaping it (which it does in order to prevent XSS attacks on user-controlled input):

<h:outputText value="#{contact.customTypeProperty}" escape="false" />

This is however not necessarily the best practice. You should control the presentation in the view side, not in a toString() in the model side. For example, assuming that CustomClass has in turn two properties foo and bar and you'd like to present it in a table:

<h:panelGrid columns="2">
    <h:outputText value="Foo" />
    <h:outputText value="#{contact.customTypeProperty.foo}" />

    <h:outputText value="Bar" />
    <h:outputText value="#{contact.customTypeProperty.bar}" />
</h:panelGrid>

Thanks BalusC, somehow the Object's toString is called. Although it is correctly overridden in CustomClass.

Put @Override annotation on the toString() method to make sure that you didn't make any typos of changes in method signature. It has to be public String toString() {} without arguments and throws.

Override notation is there, I guess the issue is unrelated to JSF. The wsimport doesn't generate toString. So the problem lies somewhere there.

jsf 2 - Custom data types in Facelets JSF 2 Expression Language - Stac...

jsf jsf-2 facelets el