I am new to Struts and have the following observation and question. I count no fewer than 3 ways of passing data from the business tier to a response jsp page. What is the best way of doing this? For example: let's say my application receives 2 parameters from a "welcome.jsp" page (via the appropriate ActionForm, call it "welcomeForm"), uses those 2 params in an sql query, and returns the query results to an "answer.jsp" It seems that, 1. one can set attributes on the HttpRequest using the query results and reference the request from "answer.jsp" 2. create a Data Transfer Object (DTO) JavaBean, populate this bean with the query results, and reference this bean in "answer.jsp" 3. Somehow set params on the "welcomeForm" with the query results and reference the updated form from "answer.jsp"
Are all of these methods valid? Are some better than others? (is, for example, #2 always best? why not?)
Hi Benjamin, The best way to do this roughly like this; 1) In your 'welcome' action which is invoked from your form on the welcome.jsp page you would send the parameters to a Business Delegate that would return the list of 'answer' objects (i.e. value objects or data transfer objects). 2) put these answer objects as a list into the ActionForm that will sit behind the answer.jsp page. 3) In your answer.jsp page use the values in the action form to populate a table. Something like this
Hope this helps. [ September 23, 2003: Message edited by: Bill Dudney ]
Thanks much for the info. So you wouldn't reference the DTO bean from the jsp via usebean? Would the ActionForm referenced by answer.jsp be one and the same ActionForm which had originally received the input params from the html page--here "welcomeForm"?
Joined: Sep 05, 2003
Hi Benjamin, You could put the dto's into the session then reference them via usebean but I think its better to not do that but instead put the into answerForm (i.e. answerForm.setAnswers(myListOfAnswers)) then the answer.jsp would reference answerForm in the typical way. A sort of sequence 'diagram' follows 1) welcomAction is invoked with the welcomeForm 2) you extract the parameters required by your biz delegate 3) you send the parameters to the biz del and get the answer list 4) you find the answerForm (this can be tricky, its referenced in my Jakarta Pitfalls presentation on my blog if you'd like more info). 5) you stuff the answer list intot he answerForm 6) you return the action forward that will take you to the answer.jsp page 7) the answer.jsp page uses the answer form to get at the newly placed list of answers I hope this makes sense
Joined: Apr 08, 2003
Thanks, Bill, Your method indeed makes sense. I'll try it. Also, I found out another way it might be done: instantiate an ordinary JavaBean, pass the bean to the delegate, populate the bean in the delegate, attach THIS ENTIRE BEAN (not simply the values it holds) to the HttpRequest object, access this javaBean in the jsp via the useBean directive
subject: Best Strategies of Passing output data to web tier