• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Passing data from JSF page in the backing bean

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm new to JSF, and I' trying to move some code from Struts frame work to JSF.

I don't know how to set a value to a property of a bean from the JSF page without exposing the value to the user.

In the Struts I set the value for the "action" property as follows:

<html:hidden property="action" name="MyBean" value = "add"/>

I'm not sure how I can do the samething in JSF since JSF "h:inputHidden" tag does not have "property" element.

><h:inputHidden id="action" value="add"/>

Below is the definition of the bean that I'm using.

Please help.
John

public class MyBean {

// Some data captured from the form

private String action;

pulbic String process() {
String outcome = null;

if ("add".equals(action) {
// outcome = add the data
} else if ("update".equals(action) {
// outcome = update existing data
} else else if ("delete".equals(action) {
// outcome = delete existing data
} else {
// unsupprted action
}
return outcome;

}
}
 
Ranch Hand
Posts: 15304
6
Mac OS X IntelliJ IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
dev_for_fun

Welcome to Javaranch! We don't have too many rules 'round these here parts, but we do have a Naming Policy. You'll need to adjust your display name to follow those guidelines.

You can do so by clicking here.

Thanks partner.
 
Gregg Bolinger
Ranch Hand
Posts: 15304
6
Mac OS X IntelliJ IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you don't need to expose the property to the user, why not just make it a private property of the bean?
 
John Cash
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you Gregg for your reply.
What I need to do is to assign a value from the JSP page to a bean property so that the server would know what to do with the submitted form.
The value assinged to the bean property would not be entered by the user, it would be hard-coded in the JSP similar to what I'm doing in the Structs
(<html:hidden property="action" name="MyBean" value = "add"/> .
I'm not sure how I can do the samething in JSF since JSF "h:inputHidden" tag does not have "property" element.

><h:inputHidden id="action" value="add"/>
I hope this clarify my question
Please help!
 
Gregg Bolinger
Ranch Hand
Posts: 15304
6
Mac OS X IntelliJ IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
But it does have a value attribute. JSF uses the value attribute for getting and setting bean properties.
 
John Cash
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How can I tie the value to the bean property e.g how do I set the value of myBean.action to the "add" value?
When the form is passed to the server I need myBean.action to have a value of "add".

In struts I have "property" and "value" attributes while in JSF I only "value" attribute.

Struts: html:hidden property="action" name="MyBean" value = "add"/> .

JSF: <h:inputHidden id="action" value="add"/>

Thank you again for ansering my questions
 
Ranch Hand
Posts: 99
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi
In JSF you can use <id> attribute

eg:


here <value> hold the value. and you can access the field by using <id> tag

in action to access the value of the variable profileId use,

hope this one helps you

Gopal
 
John Cash
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Gopal,

Thanks for your reply.

I cannot use:
�<h:inputText id="profileId" value="#{myForm.profileId}" size="25"/>�

Since I don�t want the user to set the value for the bean property, I want to hard-code the value fro the bean property in the jsp form depending on what that form should do (i.e add, modify, .. etc)
This is why I use:
�<h:inputHidden id="actionId" value="add"/>�

As you can see there is no place to link the value to the bean property since �h:inputHidden� does not have an attribute for that.

I used the <id> to retrieve the value on the server side using:

String action = (String)FacesContext.getCurrentInstance().getExternalContext()
.getRequestParameterMap().get(�actionId�); // will not work

This works only if I prefix �actionId� with the �formId� i.e. I should pass �formId:actionId� as a parameter key

String action = (String)FacesContext.getCurrentInstance().getExternalContext()
.getRequestParameterMap().get(�formId:actionId�); // works fine


The challenge is to find the �formId� since multiple jsp forms will use this mechanism.

I tried to use:
String action = ((HttpServletRequest)FacesContext.getCurrentInstance().getExternalContext().getRequest()).getParameter("actionId");

Or
String action = ((HttpServletRequest)FacesContext.getCurrentInstance().getExternalContext().getRequest()).getParameter("formId:actionId");

And both of they return null, I don�t know why.

Thank you
 
Ranch Hand
Posts: 362
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
John, I'm not quite sure what you want to do with the hidden field. After all the user will be submitting the form some way. Through a button click most surely. Why not use that to know what the form should do. Add button for add, subtract for subtract, etc you get the idea.

Secondly, when using

�<h:inputText id="profileId" value="#{myForm.profileId}" size="25"/>�

The user can only modify profileID if the setter looks like this

public void setProfileId(String profileID) {
this.profileId=profileID;
}

Take the "this.profileId=profileID;" out and you can call the setter all you want. Nothing will happen.

Finally regarding your problem with the formId. Have you tried getting the whole array of parameters? Finding the proper formId in it to use later in your code. Maybe getParameterMap() or getParameterNames() will help there.
 
John Cash
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Gerardo,

Thanks for your reply.
First:
I cannot use the button since all forms will submit to a single �button action� method which will dispatch the request to different handlers based on the value of the hidden field.
So if I have two forms both of them will have buttons with action = �myBean.process()�, but each form is doing a different action. �myBean.process()� needs to figure out which form is calling it so it takes the right action. Here the hidden field comes into play. I�m not sure how the button will help here.

Secondly, when using

�<h:inputText id="profileId" value="#{myForm.profileId}" size="25"/>�
Would display a text field to the user which I don�t want to. This field should be hard-coded in the jsp form and has nothing to do with the current user

Finally

I�m doing exactly what you described to find out what is the formId, but looks to me too much work to just set a value for a bean property.
Here is what I�m doing to get the formId:

Map parameterMap = FacesContext.getCurrentInstance()
.getExternalContext().getRequestParameterMap();

if (parameterMap.size() > 0) {
Set keys = parameterMap.keySet();
Iterator iter = keys.iterator();
while (iter.hasNext()) {
String key = (String) iter.next();
if (key != null && key.length() > 0) {
int index = key.trim().indexOf(':');
if (index == -1) {
continue;
} else {
formId = key.trim().substring(0, index);
break;
}
}
}

}

String operKey = formId + " perationId";


I�m not sure if this the right way to do that, I thought JSF may have an easier way to do what I�m trying to do. In struts you can do that in one line!
<html:hidden property="action" name="MyBean" value = "add"/> .

Thanks
 
Gerardo Tasistro
Ranch Hand
Posts: 362
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just a quick question. You have only one form per JSP, but many JSPs. And the form can be of any valid type across those. Right?
 
Gerardo Tasistro
Ranch Hand
Posts: 362
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
John I think the solution you're looking for is:

<h:form>

bla bla bla form stuff here

<h:commandButton value="Accept" action="#{mybean.doAdd}" rendered="#{mybean.actionToDo=='add'}/>
<h:commandButton value="Accept" action="#{mybean.doSubtract}" rendered="#{mybean.actionToDo=='subtract'}/>
<h:commandButton value="Accept" action="#{mybean.doMultiply}" rendered="#{mybean.actionToDo=='multiply'}/>


</h:form>

In this case only one of the three command buttons will be deplayed. Depending on the value of the actionToDo variable (which can't be modified by the user). JSF will automatically determine which method to call depending on the form type.

Note: wrote the code out of the top of my head so check it to make sure I didn't miss something.
 
John Cash
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you Geraldo for your reply.

I want all the jsp forms in my application to be submitted to a single action method, �process�. The �process� method will dispatch each "form" to the right handler. I need to tell the �process� method which handler to pass the "form" to.

Why I need to do that?
I have a BaseBean which extended by all other beans.
The BaseBean is defined as follows:

Public class BaseBean {

String operation;

// Action dispatcher
public String process(){
if (�add�.eqyals(this.operation) {
//call add handler;
} else if (�login�.eqyals(this.operation) {
call add handler;
} else {
//��.
}
Return outcome;
}
}

For the �process� method above to work I need to set the property �operation� from each jsp �form� depending on the purpose of the specified jsp.

The action value of the �commandButton� will always be myBean.process();

Hopes this explain what I�m trying to achieve
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The cleanest solution is here Passing action parameters from JSF to backing beans

 
On my planet I'm considered quite beautiful. Thanks to the poetry in this tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic