> I am relatively new to Struts. Hence I would like to
> invite your suggestions on the following
> implementation:
> 
> In our application, for which we are using Struts, we
> need to implement a table of multiple columns, where 
> 1. the number of rows will be dynamic and fetched from
> the database
> 2. a couple of columns will be editable by the users
> and
> 3. the rest of the columns will be read-only
> 
> Any ideas on how to implement this using Struts ?

Here's how I do it...

In your Action, fetch the data from the database and build an
ActionForm from it. Put this in the session scope like:

        overview = new OverviewForm();
      FILL IN INFORMATION HERE
        session.setAttribute("overviewForm", overview);

At the end of your Action, forward as usual to the JSP page
to display it. Now this JSP page will contain an <html:form>
tag for the next Action. For example:

        <html:form action="close">

So when we submit the form, we go to Action "close". We might
find the following in our struts-config.xml file:

      <action path="/close" type="CloseAction"
        name="overviewForm" scope="session" validate="false">
      </action>

This says that our jsp page must fill out a ActionForm called
"overviewForm". The same name as the ActionForm that we have
already preloaded. So rather than creating a new ActionForm,
JSP finds the one we made and uses it instead. Within the
<html:form> you can print out the data from your ActionForm as
you normally would, making some read-only and some input fields.

After filling in the fields that aren't read-only, this
ActionForm is then delivered to our CloseAction as input just
as it would be in any Struts example.

There is one big gotcha, though. The reset() method in the
ActionForm is always called between Actions which means all
your static pre-loaded read-only data will get wiped out. If you
don't want this to happen, you can modify your reset() method
to not do this. For example, the first thing my reset() does is:

        if (mapping.getPath().equals("/close"))
            return;

So if I'm on my way to the CloseAction, I don't reset anything
because I know I'll need it.

This works for me. Not sure it's the "official" way since I am
also new to struts. If there are better ways, I hope someone
will point them out.

Devon

Reply via email to