Re: The Action, the bean and the JSP

2003-05-27 Thread Gareth Andrew
Torsten Schlabach wrote:

Dear List,

I am working with Struts for some weeks now, but there are just some
concepts that don't become clear to me neither from the docs nor from all that
tutorials. They all stop where my questions start. So first of all, I would like
to ask the list for confirmation on what I found out / understood so far.
Is it right that in a web browser environment:
a) everyhting starts with a request coming in
b) and the result of that is some web page beeing rendered in the browser
c) all that Struts (and other methodologies for application login in such an
environment) are about taking the input from the request to somehow generate
the output being sent to the browser
d) The request - response thing is stateless meaning that while processing a
request neither myself nor the framework should care about the preceeding or
potential following requests (with the little exception of *optionally*
keeing a session state in some session content)
Working from that assumption, I exlained to myself that this is what happens
inside Struts when a request comes in:
- The ActionServlet determines the appropriate action from the
struts-config.xml.
- In case that action has a form bean associated with it, it instantiates
that form bean and fills it with the request parameters.
- After that it calls the execute method on the Action passing the (now
filled) form bean instance in as a parameter.
- The code in the execute method can read information from the FormBean, do
calculations, lookups, whatever and write results back into the form bean.
- The execute method returns control to the ActionServlet letting it know
which JSP page to render (choosing either from local or global forwards).
- The JSP page is rendered using the FormBean (still the same one?!?) as a
data source and sent back to the broser?
Am I right or wrong until here?
 

Sounds fairly correct.

In case I am right (and in case I am wrong, too), please help me understand:

- Why do I need to use a *forward* to display the output page? (My
understand of forward would be: I am being sent somewhere else than I originally
intended to go.) It would be much more logical to me if my action in
stuts-config.xml had a output attribute (similar to the input attribute) that names
the page that I want to use to render the results of the action.
 

Your definition of forward is closer to the definition of redirect.  
Forwarding is just transferring control (usually to a JSP page to render 
the view).
The reason its possible to to forward to more than one location, rather 
than just a single output attribute is that the action may have 
different outputs depending on some logic (for example I have a search 
action which returns one of three forwards depending on whether 0,1 or 
multiple items are found).

- If the results page contains a form that has an action associated to it
that the user *might* fire by clicking submit on the rendered page (he or she
could as well use a link to go somewhere else) Struts will instantiate the
form bean for that potential request already before even rendering the page
containing the form that will enable the user to make that request later (or
leave it). What's the sense in here?
 

There are generally two actions involved in handling user input with forms.

Step 1.  The user follows a link to a page with a form - for example, 
Edit user details.

Request -- Action1 -- EditUserForm.jsp

Step 2.  The user hits the submit button after filling in the form.

Request -- Action2 -- confirmation.jsp

Both actions will be associated with same form  in the struts-config.xml 
- lets call it newUserForm.
Before calling Action1.execute() Struts will instantiate a new 
newUserForm and call execute. The action may then wish to preopulate 
this form with some data from the model ie. from the database.  In this 
case it will be the users current details.  The action then forwards to 
the EditUserForm.jsp via a local forward (if errors occurred when 
loading the user data or some flag was set or something then perhaps the 
action might forward to a different location :-).  The jsp can then 
render the filled in values as HTML form controls.
The Action form instantiated for Action2 will be populated with the 
values that were filled in by the user.  Action2 can then submit these 
to the database.

I am sure I got at least one concept wrong. Please enlighten me.

Torsten

 

Hope this helps (sorry if i spelled things out a bit slowly)

Gareth

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


RE: The Action, the bean and the JSP

2003-05-27 Thread Krishnakumar N
Hi,

  If I understand you right, you are wondering (a) why the formbean gets
instantiated while building the view and (b) how you can get your data into
the form bean for display.

  The normal processing is as follows: [user enters data in the html form]
- [HTTP request] - [form bean] which gets passed to the action class for
processing. Where you are incorrect is in assuming that in order to get
dynamic data into the next view, you need to populate the data into the form
bean of that view's form's action mapping. Not necessary. You can put the
data as beans in request/session scope under some name and you can use
struts' bean: or jstl's c: tags to output data from these beans (referring
to them by name) to html.

  The only case where you will need to meddle with the form bean of the next
view is where the form should come pre-populated to the user. Struts example
contains a simple scenario where this is done; editRegistration page. There
are also long discussions in the user archive regarding the *right* way to
do form pre-population when the action does not have a reference to the form
bean of the next view and you do not want to chain actions. For example,

http://marc.theaimsgroup.com/?l=struts-userm=105058812426966w=2
http://marc.theaimsgroup.com/?l=struts-userm=103712789408751w=2

HTH,
Krishna

-Original Message-
From: Torsten Schlabach [mailto:[EMAIL PROTECTED]
Sent: Tuesday, May 27, 2003 7:57 PM
To: Struts Users Mailing List
Subject: RE: The Action, the bean and the JSP


But in whatever page I want to render I need to have the information that I
put into the bean corresponding to the action. For example, I would have
done
a lookup of the user's full name and want to render a page saying: Welcome
Joe Sixpack! and containing a form allowing the user to enter something
(let's say: open a ticket) and submit it to the new ticket action.

Of course the bean for the new ticket action will be a bean with ticket
attributes; not with the user's login attributes any more. But what Struts
does
is this: It looks up in my JSP form (supposed to render Welcome Joe
Sixpack) that the user might submit this form to new ticket, therefore
instantiate a ticket bean and put this in the context of the JSP page; so
Joe Sixpack
will be gone.

I don't even have a chance to put Joe's name into the ticket bean as an
additional field because that ticket bean is instantiated and put in for
the
login bean after the execute() method of the login action has finished; so I
have no access to that new bean and the page has no access to the old bean.

Got it? Does that make sense?

Torsten

 You may need to render different views on basis of different conditions ..
 Say if the login is invalid , then invalidlogin.jsp
 If its a first time user then probably , enteruserpin.jsp ..
 If you have a single attribute called output then this may not be
 possible
 ..
 and hence the forward
 
 
 
 -Original Message-
 From: Torsten Schlabach [mailto:[EMAIL PROTECTED]
 Sent: Tuesday, May 27, 2003 7:17 PM
 To: [EMAIL PROTECTED]
 Subject: The Action, the bean and the JSP
 
 
 Dear List,
 
 I am working with Struts for some weeks now, but there are just some
 concepts that don't become clear to me neither from the docs nor from all
 that
 tutorials. They all stop where my questions start. So first of all, I
 would
 like
 to ask the list for confirmation on what I found out / understood so far.
 
 Is it right that in a web browser environment:
 a) everyhting starts with a request coming in
 b) and the result of that is some web page beeing rendered in the browser
 c) all that Struts (and other methodologies for application login in such
 an
 environment) are about taking the input from the request to somehow
 generate
 the output being sent to the browser
 d) The request - response thing is stateless meaning that while processing
 a
 request neither myself nor the framework should care about the preceeding
 or
 potential following requests (with the little exception of *optionally*
 keeing a session state in some session content)
 
 Working from that assumption, I exlained to myself that this is what
 happens
 inside Struts when a request comes in:
 
 - The ActionServlet determines the appropriate action from the
 struts-config.xml.
 - In case that action has a form bean associated with it, it instantiates
 that form bean and fills it with the request parameters.
 - After that it calls the execute method on the Action passing the (now
 filled) form bean instance in as a parameter.
 - The code in the execute method can read information from the FormBean,
 do
 calculations, lookups, whatever and write results back into the form bean.
 - The execute method returns control to the ActionServlet letting it know
 which JSP page to render (choosing either from local or global forwards).
 - The JSP page is rendered using the FormBean (still the same one?!?) as a
 data source and sent back to the broser?
 
 Am I right or wrong until here?