Hi,

I posted a message last week with a similar problem.  I basically have
several fields on a row in a table, each row containing the same fields
but the number of rows in the table is dynamic and is dependant on a
number of things that aren't defined at compile time.

Although I managed to figure out a workaround based on some other posts
in this group (although not ideal) of sorts and have got the dynamic
form (as described above) to work.

First I have defined a form bean (IterateForm) that contains a Vector
and a constructor capable of initialising it with a fixed number of
blank elements.  Each element is another bean (NameForm) which is
essentially a row in the HTML table containing the dynamic fields.

NameForm.java
-------------
public final class NameForm extends ActionForm
{
        private String title;
        private String forename;
        private String surname;

    public NameForm()
    {
                title = "";
                forename = "";
                surname = "";
    }

// getters and setters

IterateForm.java
-----------------
public final class IterateForm extends ActionForm
{
        private java.util.Vector names;

        // Constructor
        public IterateForm(int iNumElements)
        {
            names = new java.util.Vector(iNumElements);

                for(int i = 0; i < iNumElements; i++)
                {
                        NameForm nameForm = new NameForm();

                        nameForm.setTitle("");
                        nameForm.setForename("");
                        nameForm.setSurname("");

                        names.add(nameForm);
                }

                this.numRows = Integer.toString(iNumElements);
        }

        public java.util.Vector getNames()
        {
                return names;
        }

// Don't require setter.

My first JSP index.jsp allows you to specify the number of rows for the
dynamic form.

index.jsp
---------
<html:html locale="true">

<head>
        <title>Dynamic Forms Using Struts</title>
        <html:base/>
</head>

<body>

<h2>Dynamic Form Using Struts</h2>


<html:form action="/dynaFormSetup.do">

Number of Rows: <html:text property="numRows"/>

<html:submit />

</html:form>

</body>
</html:html>

This page submits its form to the DynaSetupAction action class.  This
class then creates a IterateForm (above) using the constructor and
passing it the number of rows for the dynamic form, which initialises
the collection with whatever number of elements you want.

DynaSetupAction.java
--------------------
public final class DynaSetupAction extends Action
{
        public ActionForward perform(ActionMapping mapping,
                                             ActionForm form,
                                                 HttpServletRequest
request,
                                                 HttpServletResponse
response)
                                     throws IOException,
ServletException
        {


                int numRows =
Integer.parseInt(((DynaSetupForm)form).getNumRows());

                HttpSession session = request.getSession();

                // Create IterateForm with dynamic number of rows.
                ActionForm iterForm = new IterateForm(numRows);

                session.setAttribute("iterateForm", iterForm);
                // request.setAttribute("iterateForm", iterForm);

                return (mapping.findForward("iterationForm"));
        }
}

This action then forwards control back to a JSP named IterateForm.jsp.
Which iterates around the collection creating rows the number of rows
specifid from the index.jsp page.

IterateForm.jsp
---------------
<html:html locale="true">

<head>
        <title>Dynamic Form Using Struts</title>
        <html:base/>
</head>
<body>

<h2>Dynamic Form Using Struts</h2>
<table align="center" width="70%">
<html:form action="/processForm.do" method="POST">

<th>Title</th>
<th>Forename</th>
<th>Surname</th>

<logic:iterate id="names" name="iterateForm" property="names"
type="com.kainos.struts.NameForm">
 <tr>
  <td><html:text name="names" property="title" indexed="true"/></td>
  <td><html:text name="names" property="forename" indexed="true"/></td>
  <td><html:text name="names" property="surname" indexed="true"/></td>
 </tr>
</logic:iterate>

<tr>
        <td colspan="3" align="center">
        <html:submit />
        </td>
</tr>
</html:form>
</table>

</body>
</html:html>

This page then submits its results to the IterateAction action class
which simply forwards control to the results page DynaFormResults.jsp
which displays them. (See Below).

DynaFormResults.jsp
-------------------
<html:html locale="true">

<head>
        <title>Dynamic Form Results Page</title>
</head>

<html:base/>

<body>

<h2>Results of Dynamic Form</h2>


<p>
First Person:<br>
<bean:write name="dynaForm" property="title" scope="request"
filter="false"/>
<bean:write name="dynaForm" property="forename" scope="request"
filter="false"/>
<bean:write name="dynaForm" property="surname" scope="request"
filter="false"/>

</body>
</html:html>

struts-config.xml
------------------
...
  <form-bean name="iterateForm" type="com.kainos.struts.IterateForm" />
  <form-bean name="name" type="com.kainos.struts.NameForm"/>
  <form-bean name="dynaSetupForm"
type="com.kainos.struts.DynaSetupForm"/>
...
    <forward   name="dynaFormResults"    path="/DynaFormResults.jsp"/>
    <forward   name="iterationForm"      path="/IterateForm.jsp"/>
...
        <action path="/processForm"
                        type="com.kainos.struts.IterateAction"
                        name="iterateForm"
                        scope="request"
                        input="/IterateForm.jsp" />

        <action path="/dynaFormSetup"
                        type="com.kainos.struts.DynaSetupAction"
                        name="dynaSetupForm"
                        scope="request"
                        input="/index.jsp" />
...


It isn't an ideal way to do it I suppose but it works for what I need.

Adrian Cunningham.



"Daniel Hinz" <[EMAIL PROTECTED]> wrote in message
news:<[EMAIL PROTECTED]>...
> Hi,
> 
> i posted a feature request on struts-dev on friday, which i'd like to 
> bring to this forum for dicsussion. Although this has already been 
> discussed in some threads, a satifactory conclusion has not been 
> reached (at least in my opinion). So here it goes:
> 
> A) Use Cases:
> 1) Develop a generic RDMBS Browser using Struts.
> 2) Develop a WebInterface for arbitrary XML editing.
> 
> B) Problems in Struts 1.0 and 1.1:
> For each property A the corresonding setA / getA methods are invoked 
> upon read/write. There's nothing wrong with that. However i must 
> deploy form fields that are unknown to me at compile-time. Thus i 
> cannot write a Bean with the appropriate methods.
> 
> C) Request for extending org.apache.struts.action.ActionForm:
> A simple solution would be to introduce methods with the following
> signature:
> public void setProperty(String propertyName, Object propertyValue) 
> public Object getProperty(String propertyName)
> 
> D) Workaround:
> None known to me. There's a class called DynaActionForm which allows 
> you to add a dynamic number of fields *in the struts-config.xml file*.

> This is not feasible since the properties are unknown at Servlet 
> Initialization time.
> 
> I'm quite new to Struts so please forgive me if i missed something 
> blatantly obvious.
> 
> Regards,
> 
> Daniel Hinz
> 
> --
> Daniel Hinz
> Software Engineer
> [EMAIL PROTECTED]
> 
> 
> --
> To unsubscribe, e-mail:
<mailto:[EMAIL PROTECTED]>
> For additional commands, e-mail: 
> <mailto:[EMAIL PROTECTED]>
> 


--

This e-mail is confidential and is intended for the named recipient only. If
you receive it in error please destroy the message and all copies. Kainos
Software Ltd. does not accept liability for damage sustained as a result of
malicious software (e.g. viruses). Kainos does not accept liability for, or
permit; the creation of contracts on its behalf by e-mail, the publication of
any defamatory statement by its employees by e-mail, or changes subsequently
made to the original message. The Company's registered office is located at
4-6 Upper Crescent, Belfast, BT7 1NT, Northern Ireland, Tel +44 28 9057 1100.

--
To unsubscribe, e-mail:   <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>

Reply via email to