4

I would like to retrieve the value of JSF input box in a managed bean action method, without it being associated with any managed bean property. E.g.

<p:inputText id="txtuserid" value="" />

My use case is that in my application I will like to prompt user here and there for passwords for every DML operations, and therefore like to have a password and comment related fields on each of my UI, and the remarks needs to be saved in a common table for audit purpose.

How can I achieve this?

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
NKS
  • 1,140
  • 4
  • 17
  • 35
  • You forgot to tell where you want to retrieve it. – BalusC Sep 25 '13 at 10:59
  • In my ManagedBean that is associated with some entity not having the remarks and password fields – NKS Sep 25 '13 at 11:05
  • But you literally said "without managed bean" in title. What do you want? – BalusC Sep 25 '13 at 11:07
  • I want to access a JSF input box value which is not associated with any bean, in a Java ManagedBean class that is not associated with my input box in anyway. For example : On my UI I may have fields for UserID, UserName, UserAddress (all associated with ManagedBead - User) , I am also having fields like remark which is not associated with any bean. I want to retrieve the value of the independent remark input box value in Java Bean. .... – NKS Sep 25 '13 at 11:15
  • Aha, I see what you mean. I clarified the question. – BalusC Sep 25 '13 at 12:11

2 Answers2

6

Just do the same as JSF is doing under the covers: grabbing the HTTP request parameter. If you're familiar with basic HTML, you know that every HTML input element sends its name=value pair as HTTP request parameter.

Given a

<h:form id="formId">
    <p:inputText id="userId" /> <!-- Note: no value attribute at all, also no empty string! -->
    ...
    <p:commandButton value="submit" action="#{bean.submit}" />
</h:form>

which generates basically the following HTML

<form id="formId" name="formId">
    <input type="text" name="formId:userId" ... />
    ...
    <button type="submit" ...>submit</button>
</form>

you could grab it as follows from ExternalContext#getRequestParameterMap():

public void submit() {
    ExternalContext ec = FacesContext.getCurrentInstance().getExternalContext();
    String userId = ec.getRequestParameterMap().get("formId:userId");
    // ...
}

Don't forget to manually convert and validate it if necessary, like as JSF is doing under the covers. In other words, just repeat the JSF's job by writing additional code so that your code is not DRY :)

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
-3

If the remarks property is independent of any entities, so it will depend only as a property, to a managed-bean sessionScoped or requestScoped according to your needs, to to what you want. If you want that this property to be independant to any Java-Beans, so you can use the attirbute binding of the inputText tag.

To do that, see here at the good response of M. @BalusC : How does the 'binding' attribute work in JSF? When and how should it be used?

And see also: The reasons to use binding in a JSF form

Community
  • 1
  • 1
Omar
  • 1,430
  • 1
  • 14
  • 31
  • You forgot to tell how exactly this solves OP's concrete problem. – BalusC Sep 25 '13 at 12:09
  • The links especially the 1st give more details & explanations, but it seems the OP was asking about parameters, neither independent properties nor binding. – Omar Sep 25 '13 at 12:19
  • Yes, I know that the answer is *indirectly* somewhere in one of those links if you click through sufficiently. However, you are nowhere pointing out the exact solution in your answer, so the reader would have no clue where to stop by while clicking through. In other words, you are not concretely answering the question but basically saying "Uhm, it's perhaps here. Try looking around here." which is rather unhelpful. Just post a comment then :) – BalusC Sep 25 '13 at 12:20
  • Now, i got it well, thanks our teacher ;) – Omar Sep 25 '13 at 12:28