Dynamic forms....

G

gbattine

Hi guys,
i'm a new user of jsf and i have a singolar question for you, i don't
know if a solution exists and i need your help.
I have a form with some fixed input fields.
I want my applications asks me in a menu page the number of forms i
want (3 for example) and dynamically shows me the form i have ,but
replicated for three times, each form as a row of a table for giving
user the possibility to see all the form in the same page before
uploading.
When the upload button is clicked i want all the form values are
inserted in a mysql table....
can someone tell me if a solution to my problem exists?
If it exists, can you help me with simple help...i'm a newbie.
Thanks very much
 
M

Moiristo

gbattine said:
Hi guys,
i'm a new user of jsf and i have a singolar question for you, i don't
know if a solution exists and i need your help.
I have a form with some fixed input fields.
I want my applications asks me in a menu page the number of forms i
want (3 for example) and dynamically shows me the form i have ,but
replicated for three times, each form as a row of a table for giving
user the possibility to see all the form in the same page before
uploading.
When the upload button is clicked i want all the form values are
inserted in a mysql table....
can someone tell me if a solution to my problem exists?
If it exists, can you help me with simple help...i'm a newbie.
Thanks very much

First of all, I don't think you can send multiple forms without
Javascript. Why do you want to have multiple forms? Anyway, you can
build such a page by creating a binding.

For example, I needed to build a form which had variable input fields.
The class I made looks like this:

public class DynamicForm {
public HtmlPanelGrid getFormElements() {
ReportInfo ri = (ReportInfo) ViewUtils.eval("#{reportInfo}");
Set keys = ri.getParameters().keySet();

HtmlPanelGrid grid = new HtmlPanelGrid();
grid.setColumns(2);
List children = grid.getChildren();

Iterator ix = keys.iterator();

while(ix.hasNext()){
String next = (String) ix.next();

HtmlOutputText ot = new HtmlOutputText();
ot.setValue(next);
children.add(ot);

HtmlInputText hit = new HtmlInputText();
hit.setSize(40);
hit.setTitle(next);
hit.setRequired(true);
hit.setStyleClass("textfield");

String bind = "#{reportInfo.parameters['" + next + "']}";
hit.setValueBinding("value",
ViewUtils.getApplication().createValueBinding(bind));


children.add(hit);
}

return grid;
}
}

Then I declard it in the grs-config.xml:

<managed-bean>
<managed-bean-name>dynamicForm</managed-bean-name>
<managed-bean-class>model.view.DynamicForm</managed-bean-class>
<managed-bean-scope>request</managed-bean-scope>
</managed-bean>

And used it in a JSF like this:

<h:panelGrid binding="#{dynamicForm.formElements}" />


I hope you can use this example to find a solution.


Regards,

Moiristo
 
G

gbattine

the question is:

i had to upload a form with fixed fields (firstname,lastname,city) for
three,four,five times (depending by user) but with different values

antonio lamanna napoli
george bush america

etc......

the fields are the same for each form......
 
M

Moiristo

gbattine said:
the question is:

i had to upload a form with fixed fields (firstname,lastname,city) for
three,four,five times (depending by user) but with different values

antonio lamanna napoli
george bush america

etc......

the fields are the same for each form......

My suggestion was that you fetch the value (the number of rows a user
wants) to a binding class, which in turn can generate the desired number
of fields.
 
G

gbattine

thanks....
i think it can be my solution....but i've no idea about doing it.....
can you help me,please?
You're my only hope,it's the first solution that someone says for my
problem.........
Can you help me with initial code and help?
 
M

Moiristo

gbattine said:
thanks....
i think it can be my solution....but i've no idea about doing it.....
can you help me,please?
You're my only hope,it's the first solution that someone says for my
problem.........
Can you help me with initial code and help?

I'll try to outline what I would have done:

1) Create a bean that stores the fieldValue (the desired number of fields)

2) Create a form for the fieldValue that navigates to the page where the
dynamic form is displayed once it is submitted, e.g.
action="#{toDynaForm}". You need to specify a navigation rule in the
faces-config.xml for this.

3) The dynaform page should contains at least something like this:
<h:form action="#{ActionClass.processForm}">
<h:panelGrid binding="#{dynamicForm.formElements}" />
</h:form>

4) Now have a look at the DynamicForm class I earlier posted. First, you
fetch the fieldValue from the bean that was sent. Then, you can create a
for-loop which just generates the desired HtmlInputText() objects.

5) Every HtmlInputText object needs to be binded to a bean variable.
(see the DynamicForm example). That's what the 'setValueBinding' method
does.

In your case, I think there's no clean solution to create a bean that
efficiently stores this values. I'd create a bean with a HashMap. When
the dynamicForm is built, you can initialize this map with keys that
have empty values. For example, each time a new HtmlInputText() is
created, you add a record to the map:
Map().put(""+fieldValue+counter, ""); (Where counter increments for
every field within the current row).
Now, you can bind the field value to the value of this key:
String bind = "#{Map[""+fieldValue+counter]}";
hit.setValueBinding("value",
getApplication().createValueBinding(bind));

6) When the dynaform page is submitted, you can fetch the bean Map in
the processForm() function and upload the data to a database.


I am no JSF expert, and it took me some time to understand this, too :)
I do think that this implementation conforms to the JSF framework the
most, but there might be easier (but dirtier) solutions using old JSP tags.
 
G

gbattine

thanks very much....
but it's too difficult for me and i have no too time now....
i've thinked a stupid solution but i don't know if it is correct.
Can you help me?

I've thinked so:
Suppose my application needs maximum 10 form(depending on user's
choice), i create the 10 forms and i use rendered attribute to shows
them watching the user's number.
If user insert 5 the forms from 6 to 10 aren't displayed....
i don't know if i can use this solution....
can you say me if it can be correct?
Thanks very much
 
M

Moiristo

gbattine said:
I've thinked so:
Suppose my application needs maximum 10 form(depending on user's
choice), i create the 10 forms and i use rendered attribute to shows
them watching the user's number.
If user insert 5 the forms from 6 to 10 aren't displayed....
i don't know if i can use this solution....
can you say me if it can be correct?
Thanks very much

When a user can enter any number, you need to think about a solution on
how to save all data in the backing bean. If you can be sure that a user
will not choose a number above 10, then this is a good solution. The
only thing you need to do is create a function that can be called by the
render attribute, which returns true while the desired number of fields
is not reached.


Greetz,
Moiristo

PS. I have been doing a lot of searches on the internet to find a
solution to send parameters to a method with JSF EL, e.g.
#{bean.toUppercase['string']}. Today, I found a solution, which can be
very useful in this case (I guess). Link:

http://www.nabble.com/a-way-to-pass-parameters-in-el-expressions-t2084266.html
 

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,774
Messages
2,569,596
Members
45,128
Latest member
ElwoodPhil
Top