Data from .jsp page in JSF Application

M

Mongoose

Hi All,

Another (conceptual) question regarding Java Server Faces . . . still
learning...

I would like to get a collection of the responses entered into a .jsp
page in my backing bean. For example, if I have a .jsp page with 3
selectonelist boxes I would like to get the actual choices made in the
listboxes in a collection (e.g. ArrayList) in the backing bean. Is
there some way I can get a collection of the responses in my backing
bean using some sort of JSF construct??? Obviously, I can bind the
responses to my private variables in the backing bean and "manually"
and populate an Arraylist but I don't want to do it this way.

Any help is greatly appreciated . . .

Thanks!

Andy
 
A

Arved Sandstrom

Mongoose said:
Hi All,

Another (conceptual) question regarding Java Server Faces . . . still
learning...

I would like to get a collection of the responses entered into a .jsp
page in my backing bean. For example, if I have a .jsp page with 3
selectonelist boxes I would like to get the actual choices made in the
listboxes in a collection (e.g. ArrayList) in the backing bean. Is
there some way I can get a collection of the responses in my backing
bean using some sort of JSF construct??? Obviously, I can bind the
responses to my private variables in the backing bean and "manually"
and populate an Arraylist but I don't want to do it this way.

Any help is greatly appreciated . . .

Thanks!

Andy

Maybe it's just me, but if the only reason you don't want to manually
populate an ArrayList with the 3 choices is precisely because it's a
manual procedure, but otherwise it's OK to join those 3 choices into the
ArrayList, why is it that you can't use a single selectManyListbox?

AHS
 
M

Mongoose

Maybe it's just me, but if the only reason you don't want to manually
populate an ArrayList with the 3 choices is precisely because it's a
manual procedure, but otherwise it's OK to join those 3 choices into the
ArrayList, why is it that you can't use a single selectManyListbox?

AHS- Hide quoted text -

- Show quoted text -


Hey AHS,

What I meant by not wanting to do it "manually" is that I want a
theoretical solution that will work for any combination of data input
"controls" I put on the .jsp page. In other words, if I add a radio
button to the .jsp page I still want the backing bean to work . . .
 
A

Arved Sandstrom

Mongoose said:
Hey AHS,

What I meant by not wanting to do it "manually" is that I want a
theoretical solution that will work for any combination of data input
"controls" I put on the .jsp page. In other words, if I add a radio
button to the .jsp page I still want the backing bean to work . . .

I understand the non-manual bit. What I don't quite get is your specific
example. Putting it another way, if it's meaningful to combine the
outputs of 3 selectOneListboxes into a single ArrayList then why don't
you have a single selectManyListbox in the first place?

Proceeding to the general case, what are you trying to accomplish? If I
understand you correctly, and this may help understand the specific
case, are you looking for some sort of single "input map" in the backing
bean, that will contain the submitted states of all radio buttons,
checkboxes, select lists, and input texts & secrets?

AHS
 
M

Mongoose

I understand the non-manual bit. What I don't quite get is your specific
example. Putting it another way, if it's meaningful to combine the
outputs of 3 selectOneListboxes into a single ArrayList then why don't
you have a single selectManyListbox in the first place?

Proceeding to the general case, what are you trying to accomplish? If I
understand you correctly, and this may help understand the specific
case, are you looking for some sort of single "input map" in the backing
bean, that will contain the submitted states of all radio buttons,
checkboxes, select lists, and input texts & secrets?

AHS- Hide quoted text -

- Show quoted text -

Hi AHS,

You stated that:

" . . . Proceeding to the general case, what are you trying to
accomplish? If I
understand you correctly, and this may help understand the specific
case, are you looking for some sort of single "input map" in the backing
bean, that will contain the submitted states of all radio buttons,
checkboxes, select lists, and input texts & secrets? . . . "

and that's EXACTLY right . . . I am looking for EXACTLY that . . .
some sort of construct . . . or as you say "input map" that will
contain the submitted states of all controls on the .jsp page. Is
there some construct to do this in JSF? Is there a collection of the
controls perhaps???
 
A

Arved Sandstrom

Mongoose said:
Hi AHS,

You stated that:

" . . . Proceeding to the general case, what are you trying to
accomplish? If I

and that's EXACTLY right . . . I am looking for EXACTLY that . . .
some sort of construct . . . or as you say "input map" that will
contain the submitted states of all controls on the .jsp page. Is
there some construct to do this in JSF? Is there a collection of the
controls perhaps???

OK, it's somewhat unorthodox at the level of the typical user to do
this, but it's not that complicated. I am not aware of any off-the-shelf
construct to do this, but you can handroll your own easily enough, or
peruse JSF implementation source.

Just create your page as per normal, but for the select lists and menus,
input texts, radio buttons and checkboxes, all you need to specify is an
ID (obviously for the menus and listboxes you'll still need to supply
content in the usual way). Make the IDs nice and descriptive - they will
be your control map keys.

So no "binding" attributes, no "value" attributes.

In the action method, get the UIViewRoot from the FacesContext and
recursively walk the component tree. The getFamily() method on each
UIComponent will tell you whether or not it's a UIInput, or
UISelectMenu, and so forth. The getValue() method (you can ignore
getLocalValue() and getSubmittedValue()) will give you the actual value
you're looking for. Bear in mind it will be an array for the listboxes.
Also keep in mind converters if you've used any.

You can easily populate a HashMap or whatever with id-value pairs as you
walk the tree.

AHS
 
M

Mongoose

OK, it's somewhat unorthodox at the level of the typical user to do
this, but it's not that complicated. I am not aware of any off-the-shelf
construct to do this, but you can handroll your own easily enough, or
peruse JSF implementation source.

Just create your page as per normal, but for the select lists and menus,
input texts, radio buttons and checkboxes, all you need to specify is an
ID (obviously for the menus and listboxes you'll still need to supply
content in the usual way). Make the IDs nice and descriptive - they will
be your control map keys.

So no "binding" attributes, no "value" attributes.

In the action method, get the UIViewRoot from the FacesContext and
recursively walk the component tree. The getFamily() method on each
UIComponent will tell you whether or not it's a UIInput, or
UISelectMenu, and so forth. The getValue() method (you can ignore
getLocalValue() and getSubmittedValue()) will give you the actual value
you're looking for. Bear in mind it will be an array for the listboxes.
Also keep in mind converters if you've used any.

You can easily populate a HashMap or whatever with id-value pairs as you
walk the tree.

AHS- Hide quoted text -

- Show quoted text -

Hey Arved . . . thanks for that . . . exactly what I want to do . . .

However, I was wondering if you could sketch the loop out that walks
the component tree in a little more detail for me? I'm having trouble
finding the exact syntax of how to do that on the web . . . and I'm
still learning JSF so I'm not familiar with all the syntax and such
yet . . .

Thanks so much,

Andy
 
J

John B. Matthews

[...]
However, I was wondering if you could sketch the loop out that walks
the component tree in a little more detail for me? I'm having
trouble finding the exact syntax of how to do that on the web . . .
and I'm still learning JSF so I'm not familiar with all the syntax
and such yet.

I am unfamiliar with JSF, but perhaps you could visit the components
recursively (untested):

private static void visit(List<UIComponent> list) {
for (UIComponent c : list) {
if (c.getChildCount() > 0) {
visit(c.getChildren());
} else {
System.out.println(c);
}
}
}

Then you could traverse the tree from its root:

UIViewRoot root = ...
if (root.getChildCount() > 0) {
visit(root.getChildren());
}
 
A

Arved Sandstrom

Mongoose said:
On Aug 24, 7:53 pm, Arved Sandstrom <[email protected]> wrote:
[ SNIP ]
Hey Arved . . . thanks for that . . . exactly what I want to do . . .

However, I was wondering if you could sketch the loop out that walks
the component tree in a little more detail for me? I'm having trouble
finding the exact syntax of how to do that on the web . . . and I'm
still learning JSF so I'm not familiar with all the syntax and such
yet . . .

Thanks so much,

Andy

Here's one quick example:

package org.ahs.jsf.controls;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.faces.component.UIComponent;
import javax.faces.component.UIInput;
import javax.faces.component.UIViewRoot;
import javax.faces.context.FacesContext;

public class ControlMapBean {

public String save() {
UIViewRoot viewRoot =
FacesContext.getCurrentInstance().getViewRoot();
Map<String, Object> controlMap =
new HashMap<String, Object>();

analyzeComponent(viewRoot, controlMap);

// do stuff with loaded control map

return null;
}

private void analyzeComponent(UIComponent uic,
Map<String, Object> controlMap) {
if (uic instanceof UIInput) {
controlMap.put(uic.getId(),
((UIInput)uic).getValue());
}

if (uic.getChildCount() > 0) {
List<UIComponent> children = uic.getChildren();
for (UIComponent child : children) {
analyzeComponent(child, controlMap);
}
}
}
}

This is barebones. Once you have the control map it's up to you to
interpret the map values, but presumably that's where your logic comes
in, because at a minimum you need to know what the ID's correspond to.
In general they will be single values or arrays
(object.getClass().isArray() will help you here).

AHS
 
M

Mongoose

Mongoose said:
On Aug 24, 7:53 pm, Arved Sandstrom <[email protected]> wrote:

[ SNIP ]




Hey Arved . . . thanks for that . . . exactly what I want to do . . .
However, I was wondering if you could sketch the loop out that walks
the component tree in a little more detail for me?  I'm having trouble
finding the exact syntax of how to do that on the web . . . and I'm
still learning JSF so I'm not familiar with all the syntax and such
yet . . .
Thanks so much,

Here's one quick example:

package org.ahs.jsf.controls;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.faces.component.UIComponent;
import javax.faces.component.UIInput;
import javax.faces.component.UIViewRoot;
import javax.faces.context.FacesContext;

public class ControlMapBean {

   public String save() {
     UIViewRoot viewRoot =
       FacesContext.getCurrentInstance().getViewRoot();
     Map<String, Object> controlMap =
       new HashMap<String, Object>();

     analyzeComponent(viewRoot, controlMap);

     // do stuff with loaded control map

     return null;
   }

   private void analyzeComponent(UIComponent uic,
       Map<String, Object> controlMap) {
     if (uic instanceof UIInput) {
       controlMap.put(uic.getId(),
         ((UIInput)uic).getValue());
     }

     if (uic.getChildCount() > 0) {
       List<UIComponent> children = uic.getChildren();
       for (UIComponent child : children) {
         analyzeComponent(child, controlMap);
       }
     }
   }

}

This is barebones. Once you have the control map it's up to you to
interpret the map values, but presumably that's where your logic comes
in, because at a minimum you need to know what the ID's correspond to.
In general they will be single values or arrays
(object.getClass().isArray() will help you here).

AHS- Hide quoted text -

- Show quoted text -

Hi Arved,

Thank you very much for that . . . I have everything working properly
now . . . now I'm going to work on the front-end a bit with
Tomahawk . . . (Many) more questions to follow . . .

Thanks Again!

Andy
 
M

Mongoose

Mongoose said:
On Aug 24, 7:53 pm, Arved Sandstrom <[email protected]> wrote:

[ SNIP ]




Hey Arved . . . thanks for that . . . exactly what I want to do . . .
However, I was wondering if you could sketch the loop out that walks
the component tree in a little more detail for me?  I'm having trouble
finding the exact syntax of how to do that on the web . . . and I'm
still learning JSF so I'm not familiar with all the syntax and such
yet . . .
Thanks so much,

Here's one quick example:

package org.ahs.jsf.controls;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.faces.component.UIComponent;
import javax.faces.component.UIInput;
import javax.faces.component.UIViewRoot;
import javax.faces.context.FacesContext;

public class ControlMapBean {

   public String save() {
     UIViewRoot viewRoot =
       FacesContext.getCurrentInstance().getViewRoot();
     Map<String, Object> controlMap =
       new HashMap<String, Object>();

     analyzeComponent(viewRoot, controlMap);

     // do stuff with loaded control map

     return null;
   }

   private void analyzeComponent(UIComponent uic,
       Map<String, Object> controlMap) {
     if (uic instanceof UIInput) {
       controlMap.put(uic.getId(),
         ((UIInput)uic).getValue());
     }

     if (uic.getChildCount() > 0) {
       List<UIComponent> children = uic.getChildren();
       for (UIComponent child : children) {
         analyzeComponent(child, controlMap);
       }
     }
   }

}

This is barebones. Once you have the control map it's up to you to
interpret the map values, but presumably that's where your logic comes
in, because at a minimum you need to know what the ID's correspond to.
In general they will be single values or arrays
(object.getClass().isArray() will help you here).

AHS- Hide quoted text -

- Show quoted text -

Hi Arved,

Thank you very much for that . . . I have everything working properly
now . . . now I'm going to work on the front-end a bit with
Tomahawk . . . (Many) more questions to follow . . .

Thanks Again!

Andy
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,077
Latest member
SangMoor21

Latest Threads

Top