You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Yuriy Zubarev <yu...@yahoo.ca> on 2001/03/21 05:46:46 UTC

db and forms

Hello,

If I may I would like to inquire about db/form handling using Struts
framework in theoretical point of view. For instance I have 2 tables in my
db, let's say "Customers":

CustomerID int,
Name       varchar,
Password   varchar

and "Phones":

PhoneID    int,
CustomerID int,
Number     varchar

A customer can log on to a web site by specifying his Name and Password on
the index page and after that he gets to a main page where he can choose
between three links: "view/edit personal information", "view/edit phones"
and "add phone".

I (almost) understand how to organize logon process: I create a
LogonForm.java that represents form elements on the index page and
LogonAction.java where I get values from the form and use them to query db
to find out if there is such a user and if so I put UserID taken from
ResultSet into session and finally load the main page. As I mentioned the
main page contains three links which, as I guess, should point to Action
Forms, something like "editCustomer.do?Action=Edit",
"editPhone.do?Action=Edit" and "editPhone.do?Action=Create". Also I should
have jsp pages for creating/editing(viewing) customer and phone
information (customer.jsp, phones.jsp (for list of phones) and phone.jsp
(for particular phone)). And this is where my poor understanding of
overall process ends. What should happen when anyone clicks
"editCustomer.do?Action=Edit"? As far as I know I have to create java
files like this "CustomerForm.java" and "CustomerAction.java" and perhaps
"SaveCustomerAction.java". CustomerForm.java encapsulates data and methods
of the form (setter, getter), CustomerAction.java is used to fill in that
form (how?) and SaveCustomerAction.java to get data from the form and save
them into db, right? Then CustomerAction.java and SaveCustomerAction.java
have to contain db related java code where I make use a session element
that contains value of CustomerID, right? And what if I need to show some
page information of which is based on many ID (parameters)? Should I pass
them through URL or Struts offers another way? Well the more I writing the
more questions I have and I suppose nobody has time to answer them, but
perhaps someone could share couple of his jsp/java files (along with
struts-config.xml) that deal with relation db and forms similar to those I
tried to describe above. Struts-example it's a good thing but more
examples are even better.

I would really appreciate any help.

Thank you for your time.

Best of luck.


_______________________________________________________
Do You Yahoo!?
Get your free @yahoo.ca address at http://mail.yahoo.ca

Re: db and forms

Posted by Ted Husted <hu...@apache.org>.
Let's see, 

Say you have a view named {page}.jsp

For each HTML form in your view, you would usually have an ActionForm 
bean. This gives you a place to store the user's input. 

Most people would name these classes {page}Form. 

Every HTML form is submitted to an Action. (Before Java, the Actions
were usually CGI scripts.) The form can use either the POST or GET
methods, as you prefer. Since Actions are just URLs, you can also
hardcode an Action as a hyperlink. In Struts, you would usually write an
Action class to handle these requests.

Most people would name these Java classes {page}Action. 

If your page actually offers several different but related Actions, you
can either handle them in a single Action class, or offer a seperate
Action class for each. This is an implementation decision. 

The Struts Action class has full access to the servlet, request, and
response, so you can check the parameters to the Action, and proceed
accordingly. 

If you write a single Action, you might be checking for "action=edit" or
"action=delete", and calling the appropriate method to do each. 

If you write separate Actions, you might be submitting the request to
{page}EditAction or {page}DeleteAction instead. 

Back on the input form, the Struts HTML tags "know" about the framework
and the ActionForm beans. They can check the Struts-config file to find
out which bean goes with which form. This makes populating the forms
"auto-magical". 

Likewise, the Struts Action classes know about the ActionForm beans too.
They also check the Struts-Config file, and either retrieve the
appropriate ActionForm bean from the request, or create a fresh one for
you if it isn't there. 

The Action class can either handle the database access directly, or
serve as an adapter to a "business logic bean" that does that instead.
It's not uncommon for people to use the Action to handle the business
logic, if that logic is not being reused in another application.

There's an JDBC database access example "Struts with a Fruit Glaze" at <
http://www.husted.com/about/struts/ > that demonstrates one approach to
handling JDBC queries in Struts. 

There is also a new work-in-progress there, "WXXI-Gavel", that puts more
of the database handling in the Action. 

One way to handle authentication is to store a "logon" bean in session
context, and use that to retrieve whatever you need to know about an
authenticated user throughout the application. Some people check for
this in the view, others use it to route the pages through the
controller so that the views don't need to know about logons. 

HTH.

Yuriy Zubarev wrote:
> 
> Hello,
> 
> If I may I would like to inquire about db/form handling using Struts
> framework in theoretical point of view. For instance I have 2 tables in my
> db, let's say "Customers":
> 
> CustomerID int,
> Name       varchar,
> Password   varchar
> 
> and "Phones":
> 
> PhoneID    int,
> CustomerID int,
> Number     varchar
> 
> A customer can log on to a web site by specifying his Name and Password on
> the index page and after that he gets to a main page where he can choose
> between three links: "view/edit personal information", "view/edit phones"
> and "add phone".
> 
> I (almost) understand how to organize logon process: I create a
> LogonForm.java that represents form elements on the index page and
> LogonAction.java where I get values from the form and use them to query db
> to find out if there is such a user and if so I put UserID taken from
> ResultSet into session and finally load the main page. As I mentioned the
> main page contains three links which, as I guess, should point to Action
> Forms, something like "editCustomer.do?Action=Edit",
> "editPhone.do?Action=Edit" and "editPhone.do?Action=Create". Also I should
> have jsp pages for creating/editing(viewing) customer and phone
> information (customer.jsp, phones.jsp (for list of phones) and phone.jsp
> (for particular phone)). And this is where my poor understanding of
> overall process ends. What should happen when anyone clicks
> "editCustomer.do?Action=Edit"? As far as I know I have to create java
> files like this "CustomerForm.java" and "CustomerAction.java" and perhaps
> "SaveCustomerAction.java". CustomerForm.java encapsulates data and methods
> of the form (setter, getter), CustomerAction.java is used to fill in that
> form (how?) and SaveCustomerAction.java to get data from the form and save
> them into db, right? Then CustomerAction.java and SaveCustomerAction.java
> have to contain db related java code where I make use a session element
> that contains value of CustomerID, right? And what if I need to show some
> page information of which is based on many ID (parameters)? Should I pass
> them through URL or Struts offers another way? Well the more I writing the
> more questions I have and I suppose nobody has time to answer them, but
> perhaps someone could share couple of his jsp/java files (along with
> struts-config.xml) that deal with relation db and forms similar to those I
> tried to describe above. Struts-example it's a good thing but more
> examples are even better.
> 
> I would really appreciate any help.
> 
> Thank you for your time.
> 
> Best of luck.
> 
> _______________________________________________________
> Do You Yahoo!?
> Get your free @yahoo.ca address at http://mail.yahoo.ca

-- Ted Husted, Husted dot Com, Fairport NY USA.
-- Custom Software ~ Technical Services.
-- Tel 716 737-3463.
-- http://www.husted.com/about/struts/