Struts best practices + jsp access + Form Property or request setAttrib

J

javaguy44

Hi all,

I was wondering if someone might be able to answer a best practices
question for me.

Let's say I have an action class, and my ActionForward method is like:

public ActionForward execute(ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response) {

DynaValidatorForm dvf = (DynaValidatorForm) form;

Customer cust = new Customer();
cust.setCustName("John Doe");
cust.setCustID("23");

At this point, how I would I want to pass my Customer bean to my jsp?
I am still new to struts, and thanks to some help I can now pass my
bean using the bean form property declared in Struts-Config or via a
request attribute. So my question is best practices wise, should I
post to the jsp using request.setAttribute like this:

request.setAttribute("RESULT", cust);

Or should I declare the bean in struts-config.xml and post like this:
dvf.set("customer", cust);

Thanks,
Javaguy
 
C

Chris Riesbeck

Let's say I have an action class, and my ActionForward method is like:

public ActionForward execute(ActionMapping mapping,
...

Customer cust = new Customer();
cust.setCustName("John Doe");
cust.setCustID("23");

At this point, how I would I want to pass my Customer bean to my jsp?
I am still new to struts, and thanks to some help I can now pass my
bean using the bean form property declared in Struts-Config or via a
request attribute. So my question is best practices wise, should I
post to the jsp using request.setAttribute like this:

request.setAttribute("RESULT", cust);

Or should I declare the bean in struts-config.xml and post like this:
dvf.set("customer", cust);

None of the above, assuming Customer is a model bean, not
a form bean. Store the customer on the session:

request.getSession().setAttribute("customer", customer);

Your JSP can then retrieve it with useBean. This is for
servlets and JSP in general, not specific to Struts.
 
J

javaguy44

Hi Chris,

Thanks for your answer...but it's not really what I'm looking for. I
am aware of how to access the data from the jsp. And setting the
session attrib isn't relevant either.

Customer is a form bean; I was just writing a simple scenario.

My point is that even if Customer is a form bean, I don't have to use
Customer as a form bean and can the request setAttribute.

What I want to know is which is this...thinking about design patterns
/ best practices, which is the better way to do it?
 
C

Chris Riesbeck

Hi Chris,

Thanks for your answer...but it's not really what I'm looking for. I
am aware of how to access the data from the jsp. And setting the
session attrib isn't relevant either.

Customer is a form bean; I was just writing a simple scenario.

My point is that even if Customer is a form bean, I don't have to use
Customer as a form bean and can the request setAttribute.

What I want to know is which is this...thinking about design patterns
/ best practices, which is the better way to do it?

If Customer is a form bean, then you shouldn't be passing it to
your output JSP. Requests and forms represent input data.
Using either in the JSP you send as a response ties your views
together, rather than going view to model to view. That bypasses
the model and reduces the independence of the views.

Your response JSP should present some view of your webapp's
internal state. Even if the JSP is just an acknowledgement to
an order, it should display what the webapp has actually
stored away about the order, not what the user entered.
If there's any discrepancy, intentional or not, you want the user
to see it.

Analogy: people misspell my name all the time, even when
I hand them a business card. I want to see what they wrote
down. It does not good if they hand me back my card.

The only time the form data should come back to the user
is when there are errors the user needs to fix. And this
happens automatically when your action mapping links a form
to an input page. It's not something your code does.
 

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

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,186
Members
46,740
Latest member
JudsonFrie

Latest Threads

Top