0

This might be simple problem, but I am not able to figure it out.

Here is my xhtml page:

*update p tag here, I am using primefaces*
<h:outputLabel for="Number" value="Number(100-800):" />
    <h:inputText id="number" name="number_Name" value="#{validationView.number}" label="Number">
        <f:validateDoubleRange minimum="100" maximum="800" />
    </h:inputText>

<p:commandButton value="S-ubmit" name="submit" actionListener="#{userBean1.method1}" ajax="false" icon="ui-icon-check" validateClient="true"/>

This is my managed bean

import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;

import org.primefaces.context.RequestContext;

@ManagedBean(name="userBean1")
@SessionScoped
public class UserBean1
{
    public void method(){
       String value = FacesContext.getCurrentInstance().
        getExternalContext().getRequestParameterMap().get("number_Name");          
       System.out.println("Value: "+value);
    }

    public void method1(ActionEvent event) {    
    String param =  (String) event.getComponent().getAttributes().get("number_Name");
           System.out.println("Param: "+param);
    }
}

I tried both methods and in both cases it is printing null output. I imported el-impl-2,2.jar into my build path. I am using primefaces 5.1 and Tomcat7

SamK
  • 377
  • 9
  • 27
  • 1
    I think you have to step back and do some jsf 101. This is an unusual way of trying to get to values of input fields. It indeed does not work that way. You normally set values in beans and let the framework do all this plumbing for you – Kukeltje Feb 18 '15 at 01:08
  • I would like to get this input variables to set and call store procedure. – SamK Feb 18 '15 at 03:17
  • @Kukeltje: indeed, see also http://meta.stackexchange.com/questions/66377/what-is-the-xy-problem – BalusC Feb 18 '15 at 07:49

1 Answers1

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?

So I will assume your h:inputText looks something like this:

<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

Community
  • 1
  • 1
Tarik
  • 4,961
  • 3
  • 36
  • 67
  • 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 – Kukeltje Feb 18 '15 at 08:59
  • 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 – Tarik Feb 18 '15 at 13:46
  • That is why I think a jsf 101 is better. Also considering the other post. – Kukeltje Feb 18 '15 at 14:00
  • 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) – Tarik Feb 18 '15 at 14:18
  • Hahaha, sorry my bad. 101 is in courses you can follow the beginners one… no relation to jsf 1.1 http://english.stackexchange.com/questions/14265/what-does-something-101-mean – Kukeltje Feb 18 '15 at 14:46
  • Oh! Sorry I didn't knew that, it's the first time i saw that and I thought you are talking about a JSF version :) and yes you are right :) – Tarik Feb 18 '15 at 15:05