File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Struts and the fly likes Design flow questions Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Design flow questions" Watch "Design flow questions" New topic
Author

Design flow questions

Raj Bhandari
Ranch Hand

Joined: Oct 19, 2005
Posts: 97
Hi all,
I have a couple of design related questions. Please give your advice:

1. The user fills a form in web browser and say the form has 10 fields.
On submit, In my action class I do this:

DynaValidatorForm dvForm = (DynaValidatorForm) form;

String field1 = (String) dvForm.get("field1");
String field2 = (String) dvForm.get("field2");
.........
String field10 = (String) dvForm.get("field10");

//Then I call my model class's Insert method
service.Insert(field1, field2, ... field10);

My question: Is there a better way so that I don't have to pass all the fields as arguments to the Insert method ? or Is this the only right way to do it.

2. I want to let the user know that the insert was successful in a new jsp. How should I pass this success message from the action to the jsp... this is what I'm thinking:

In model class:
-------------------

public boolean Insert(String field1, String field2, .......String field10){

//perform insert code here..

if(success)
return true;
else
return false;
}

In action class:
------------------

boolean test= false;
String test2=null;

test=service.Insert(field1, field2, ... field10);

if(test)
test2="insert successful";

//Place results here
dvForm.set("insert_results",test2);
request.setAttribute("formbean", dvForm);

In jsp page:
--------------

<bean:write name="formbean" property="insert_results"/>

So, this does work but I would like your comment on this flow ie. from model return a boolean to action which returns string to jsp. Is there a better way ?

Thanks!
[ April 26, 2006: Message edited by: Raj Bhandari ]
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
One possibility would be to use a Transfer Object, which is sometimes called a Data Transfer Object (DTO) and can also be called a Value Object (VO). This object is a simple JavaBean whose main purpose is to transfer data back and forth between the model and the controller or view.

Your DTO might look like this:

public class MyDTO {
private String field1;
private String field2;
// getters and setters
}

Then change your insert() method to accept only one parameter of type MyDTO.

Since Struts uses the Jakarta Commons Bean Utilities, they are already in your class path. You can use one of their handy features to populate the bean. The following code:

MyDTO dto = new MyDto();
BeanUtils.copyProperties(dvForm, myDto);

will copy field1, field2, etc from your ActionForm to the DTO. As long as the fields are named the same on both beans, it will copy them. I know this works for a subclass of ActionForm, and I'm pretty sure it works for a DynaActionForm as well, but I'm not sure; you'll have to try it.
[ April 26, 2006: Message edited by: Merrill Higginson ]

Merrill
Consultant, Sima Solutions
Raj Bhandari
Ranch Hand

Joined: Oct 19, 2005
Posts: 97
Hi Merrill,

Thanks for your answer. It is very helpful as always...
Can you please also reply to my question 2 above.
I appreciate your guidance!
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
In Java, there really isn't a need to return a boolean just to indicate whether a process was successful or not. The standard method of indicating that something went wrong inside a method is to throw an exception. if a method completes without throwing an exception, it is considered to have completed successfully.

I'd suggest making your insert() method return void and then putting the call to insert() inside a try/catch block. At the end of the try block, send a message to the user that all is well. In the catch block, forward to an error page, or send a message to the user that things did not go well.

Regarding means of communicating this information to the page, Struts has a standard method for doing this: putting ActionMessage objects into the ActionMessages object and calling the saveMessages() method. The messages are then displayed using the <html:messages> tag in the jsp. For information on how to do this, go to this link and look for the heading "Displaying Messages". Also check out this link.
Raj Bhandari
Ranch Hand

Joined: Oct 19, 2005
Posts: 97
Hi Merrill,

Once again thanks for the insight your reply gives. The links that you provide are very helpful.
Can you tell me if my design for this business logic below is ok or not ?

When user submits form, do the following:

1.insert submitted data into database
2. send email of submittal confirmation
3. If successful, display successful message in jsp with information of the record id generated in step 1.
4. If failure, then display the error message... whether it was error in insert part or email part

What I did is as follows:

In model
---------


In Action
---------



As you can see, my model class doesn't do any try/catch (hope that is right now) and my action class does a try/catch...

This all works perfectly fine, but I need comments on these 3 points:

1. is the design okay now for the way my model throws exception and action catches it ?

2. should I put the decision to call SendEmail in the Model class itself or move it to the action class... ? what if tomorrow say, they don't want to send email.. then I have to change it in model. This model class is turning out to be just for doing an Insert and sending email.. not sure if I should do it this way.

3. how do I pass the record id generated in the model's insert method to the action and then then jsp if now my insert method returns nothing.

Thanks!
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
Originally posted by Raj Bhandari:

1. is the design okay now for the way my model throws exception and action catches it ?

Yes, the design is fine. In a web application, the only place where you're ready to deal with an exception is in the Action class. It's OK for your model classes to throw the exceptions.

However, the way that you're handling exceptions in your Action class is not good. You don't want to just print a System.out.println and re-throw the exeption. This will show an ugly stack trace to the user and give the impression of an unreliable site. You want to give the user either a nicely formatted error page, or a message telling them exactly what went wrong and what to do. Use the principles in the links I gave you to send a message back to the JSP rather than just re-throwing the exception.
Originally posted by Raj Bhandari:

2. should I put the decision to call SendEmail in the Model class itself or move it to the action class... ? what if tomorrow say, they don't want to send email.. then I have to change it in model. This model class is turning out to be just for doing an Insert and sending email.. not sure if I should do it this way.

In general, a method should only do one thing, and the name of the method should reflect accurately what it does. Your method named "insert" both inserts a record and sends an email, so it's not accurately named. Suppose you have another applicatoin that needs to insert the same record. That other application may not want to send an email.

The point is, you make your methods more reusable if you restrict them to one function. I'd suggest taking the send email out of your insert method, and calling it from the Action.
Originally posted by Raj Bhandari:

3. how do I pass the record id generated in the model's insert method to the action and then then jsp if now my insert method returns nothing.

Change the return type to String and have the insert method return the ID of the new item. If the id is being generated by the database, you will have to query the database for a record meeting the criteria of the one you just entered to get the Id.
Raj Bhandari
Ranch Hand

Joined: Oct 19, 2005
Posts: 97
Hi Merrill,

Thanks for your reply. Its helping me tremendously.
Regarding the way I'm handling exceptions in Action class that you mentioned, I totally agree and understand. Actually I used the System.out.println to just see if its actually catching something. I know I should use log4j... Also I'm rethrowing the exceptions because in the struts-config.xml, I have globally declared them and forward those exceptions to an exception.jsp which display the error properly formatted. I read all this from the link you provided which was extremely helpful.
Thanks once again!
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
Cool.. good for you!

I guess I shouldn't be so surprised that you actually read the link I gave you
 
Consider Paul's rocket mass heater.
 
subject: Design flow questions
 
Similar Threads
Presentation question
Please help with GUI
how can use conditional validations on predefined validations in struts1.2
Passing data between beans
valid when