struts: double processing after bean outputs form

A

Ariel Jakobovits

In using struts, I found that my ActionForms (which I will call beans
for the purpose of generality) have enough information about the
database schema, including dataypes, field lengths, null or not null,
required or not, etc. that they may be ideal for outputting the html
forms that will receive input.

The problem is, the first (and only) run-through of the jsp page would
allow the bean to output a form, but if I am trying to use struts
nomenclature, then I need Tomcat to re-process, or double process, the
jsp page to actually have struts translate the output form (using tags
like <html:form or <html:text) into the actual html equivalents.

Is there a way I can have this happen?
 
S

Sudsy

Ariel said:
In using struts, I found that my ActionForms (which I will call beans
for the purpose of generality) have enough information about the
database schema, including dataypes, field lengths, null or not null,
required or not, etc. that they may be ideal for outputting the html
forms that will receive input.

The problem is, the first (and only) run-through of the jsp page would
allow the bean to output a form, but if I am trying to use struts
nomenclature, then I need Tomcat to re-process, or double process, the
jsp page to actually have struts translate the output form (using tags
like <html:form or <html:text) into the actual html equivalents.

Is there a way I can have this happen?

I read your post a number of times and still can't figure out what
you're asking.
So let's review where the ActionForm fits into the Struts processing
cycle. If I submit an HTML form (via GET or POST method) to a Struts
action (as mapped in the struts-config.xml file) then the ActionForm
specified by the "name" attribute in the "action" tag will receive
the contents of the form fields. The ActionForm is either instantiated
or an available instance is reused and the appropriate mutator methods
are invoked. Your servlet (specified by the "type" attribute in the
"action" tag) then invokes the accessor methods to obtain the field
contents.
The ActionForm is merely a container for the form fields.
It has nothing to do with JSP.

So what, precisely, are you trying to accomplish?
 
W

Wendy S

Sudsy said:
I read your post a number of times and still can't figure out what
you're asking.
So what, precisely, are you trying to accomplish?

I read it as the OP has enough info stored in the ActionForm that the JSP
code can be dynamically generated. Then the generated JSP needs to be
processed by Tomcat so that <html:text> turns into <input type="text>, etc.

Thinking about a form bean that even knows there *is* a database, much less
about the field names and lengths, makes my head hurt. This completely
breaks encapsulation and demolishes the entire concept of separating the
(MVC) layers of the application.

There might be a way to dynamically generate JSP, I have no idea. But the
ActionForm should not know enough about the View to be able to do it.
 
A

Ariel Jakobovits

Well, let's say I have two tables in my database: BaseballTeams and
BaseballPlayers. Now, when someone uses a JSP form to input the
attributes of a new baseball team, I want to insert the new record
into the DB, retrieve the primary index of that new baseball team for
reference, and then present the user with a JSP form for them to enter
the names of the players in each position of the team, with a hidden
field that has the team_id to be input into the player table as a
foreign key.

So, first, the user submits the Team Info form, and as you say, an
Team_ActionForm is available to me in the servlet. I insert the team,
and get the primary key. Now, still in the servlet, I instantiate a
new class called TeamPlayers_ActionForm in my servlet that has fields
like shortstop, centerfield, etc. as well as a field called team_id to
hold the foreign key. After I set the value of team_id in
TeamPlayers_ActionForm, I place the object in the request scope and
forward to a JSP page.

What I want to do is say:

<jsp:useBean id="teamplayers" scope="request"
type="my.company.TeamPlayers_ActionForm" />

<%= teamplayers.outputForm() %>

This seems like a good idea because embedded in the
TeamPlayers_ActionForm is all kinds of info regarding the data I
expect to receive from the user, i.e. all the fields are listed, as
well as constraints that are written into the validate() function in
the ActionForm. If I already have constraints there, I should be able
to output a nice form that corresponds well with the expected input
(like field size, text vs. textarea, etc).

The problem is, when the JSP page runs through, it will call the
teamplayers.outputForm() function as expected, but if that function
outputs a String object that includes struts tags like <html:form>,
when the page is displayed to the user, there will be nothing there
because the source of the page will contain <html:form> not <form>
because the JSP page needs to be RE-PROCESSED by Tomcat (and struts)
which translate those <html:> tags into real html tags.

Now do you see what my predicament is?
 

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

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top