You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Fabio Miranda Hamburger <fa...@ns.isi.ulatina.ac.cr> on 2006/12/09 22:09:54 UTC

struts action servlet interaction with jsp files

Hello,

I am migrating some PHP application to struts to take advantage of MVC.

Check this situation:

I have a struts action in fooBarStrutsAction.java. After a request, this 
action perform a database transaction and arrange the final result in an 
array. Now I want to display this array in a browser.

How can I 'send' this array from fooBarStrutsAction.java, and its method: 
public ActionForward execute(...) TO a jsp file, let's say display.jsp? 
and by the way, how to access that array within the JSP file.

I think I am missing a crucial concept here, thanks for clarify this.

Best regards,

  ---
Fabio Andres Miranda
Ingenieria de sistemas informaticos
Universidad Latina - Costa Rica
http://ns.isi.ulatina.ac.cr/~fabmirha

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: struts action servlet interaction with jsp files

Posted by Eric Rank <er...@lo-fi.net>.
Fabio,

You're right, I think you are missing something rudimentary. However,  
I remember when I made the transition from PHP to Java web apps,  
things didn't seem too obvious right off the bat.

The key to doing what you want is the request object  
(HttpServletRequest). This is an argument that comes in to the  
execute method of your struts actions. It contains parameters, both  
GET and POST. It gives you access to your sessions and cookies. It  
also is the object that you'll load up with variables that you want  
to use in your JSP.

For example:

Your struts action would have an execute method like this:

public ActionForward execute(
				ActionMapping mapping,
				ActionForm form,
				HttpServletRequest request,
				HttpServletResponse response)throws Exception{
		
		ActionForward forward = mapping.getInputForward();
		String myString = "This is my value. It could be any object. It may  
have even come from the database";
		request.setAttribute("stringForJSP",myString); //this is how you  
"send" your variables to your jsp or other ActionForward
		return forward;
	}

In your JSP you have access to the attributes you put into the  
request object:


<h1>${stringForJSP}</h1>

or with a scriptlet

<h1><%=request.getAttribute("stringForJSP")%></h1>



Eric Rank





On Dec 9, 2006, at 2:09 PM, Fabio Miranda Hamburger wrote:

>
> Hello,
>
> I am migrating some PHP application to struts to take advantage of  
> MVC.
>
> Check this situation:
>
> I have a struts action in fooBarStrutsAction.java. After a request,  
> this action perform a database transaction and arrange the final  
> result in an array. Now I want to display this array in a browser.
>
> How can I 'send' this array from fooBarStrutsAction.java, and its  
> method: public ActionForward execute(...) TO a jsp file, let's say  
> display.jsp? and by the way, how to access that array within the  
> JSP file.
>
> I think I am missing a crucial concept here, thanks for clarify this.
>
> Best regards,
>
>  ---
> Fabio Andres Miranda
> Ingenieria de sistemas informaticos
> Universidad Latina - Costa Rica
> http://ns.isi.ulatina.ac.cr/~fabmirha
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
>


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: struts action servlet interaction with jsp files

Posted by "Frank W. Zammetti" <fz...@omnytex.com>.
Note however that there is no JSTL equivalent for most of the Struts 
HTML taglib... the Struts HTML tags are Struts-aware, i.e., will 
automatically pull data from your form beans to populate the fields... 
JSTL can't do that automatically.

Aside from the HTML taglib, Laurie is of course right, you'll find JSTL 
pretty well provides what the other Struts taglibs do, and more.

Frank

Laurie Harper wrote:
> Simply put, the Struts tags pre-date JSTL. The JSTL tags provide 
> equivalent functionality in many areas and more besides. Since they are 
> a standard and more complete solution than the equivalent Struts tags, 
> their use is recommended.
> 
> The Struts tags have been kept for backwards compatibility and for those 
> who are unable to adopt JSTL.
> 
> HTH,
> 
> L.
> 
> Rick Schumeyer wrote:
>> Can I ask, what is the reason for avoiding the Struts tags and using 
>> the JSTL tags?  (It begs the question, what is the purpose of the 
>> struts tags?)
>>
>> Wendy Smoak wrote:
>>> On 12/9/06, Fabio Miranda Hamburger <fa...@ns.isi.ulatina.ac.cr> 
>>> wrote:
>>>
>>>> How can I 'send' this array from fooBarStrutsAction.java, and its 
>>>> method:
>>>> public ActionForward execute(...) TO a jsp file, let's say display.jsp?
>>>> and by the way, how to access that array within the JSP file.
>>>>
>>>> I think I am missing a crucial concept here, thanks for clarify this.
>>>
>>> In your action,
>>>   request.setAttribute( "myArray", myArray );
>>>
>>> In your JSP use JSTL's <c:forEach> tag to display the values.
>>>
>>> (The Struts <logic:iterate> tag can be used instead.  While we
>>> recommend JSTL over the equivalent Struts tags, you might want to keep
>>> things simple at first and avoid introducing another library.)
>>>
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
> 
> 
> 
> 

-- 
Frank W. Zammetti
Founder and Chief Software Architect
Omnytex Technologies
http://www.omnytex.com
AIM/Yahoo: fzammetti
MSN: fzammetti@hotmail.com
Author of "Practical Ajax Projects With Java Technology"
  (2006, Apress, ISBN 1-59059-695-1)
Java Web Parts - http://javawebparts.sourceforge.net
  Supplying the wheel, so you don't have to reinvent it!

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: struts action servlet interaction with jsp files

Posted by Laurie Harper <la...@holoweb.net>.
Simply put, the Struts tags pre-date JSTL. The JSTL tags provide 
equivalent functionality in many areas and more besides. Since they are 
a standard and more complete solution than the equivalent Struts tags, 
their use is recommended.

The Struts tags have been kept for backwards compatibility and for those 
who are unable to adopt JSTL.

HTH,

L.

Rick Schumeyer wrote:
> Can I ask, what is the reason for avoiding the Struts tags and using the 
> JSTL tags?  (It begs the question, what is the purpose of the struts tags?)
> 
> Wendy Smoak wrote:
>> On 12/9/06, Fabio Miranda Hamburger <fa...@ns.isi.ulatina.ac.cr> 
>> wrote:
>>
>>> How can I 'send' this array from fooBarStrutsAction.java, and its 
>>> method:
>>> public ActionForward execute(...) TO a jsp file, let's say display.jsp?
>>> and by the way, how to access that array within the JSP file.
>>>
>>> I think I am missing a crucial concept here, thanks for clarify this.
>>
>> In your action,
>>   request.setAttribute( "myArray", myArray );
>>
>> In your JSP use JSTL's <c:forEach> tag to display the values.
>>
>> (The Struts <logic:iterate> tag can be used instead.  While we
>> recommend JSTL over the equivalent Struts tags, you might want to keep
>> things simple at first and avoid introducing another library.)
>>


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: struts action servlet interaction with jsp files

Posted by Rick Schumeyer <rs...@ieee.org>.
Can I ask, what is the reason for avoiding the Struts tags and using the 
JSTL tags?  (It begs the question, what is the purpose of the struts tags?)

Wendy Smoak wrote:
> On 12/9/06, Fabio Miranda Hamburger <fa...@ns.isi.ulatina.ac.cr> 
> wrote:
>
>> How can I 'send' this array from fooBarStrutsAction.java, and its 
>> method:
>> public ActionForward execute(...) TO a jsp file, let's say display.jsp?
>> and by the way, how to access that array within the JSP file.
>>
>> I think I am missing a crucial concept here, thanks for clarify this.
>
> In your action,
>   request.setAttribute( "myArray", myArray );
>
> In your JSP use JSTL's <c:forEach> tag to display the values.
>
> (The Struts <logic:iterate> tag can be used instead.  While we
> recommend JSTL over the equivalent Struts tags, you might want to keep
> things simple at first and avoid introducing another library.)
>

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: struts action servlet interaction with jsp files

Posted by Wendy Smoak <ws...@gmail.com>.
On 12/9/06, Fabio Miranda Hamburger <fa...@ns.isi.ulatina.ac.cr> wrote:

> How can I 'send' this array from fooBarStrutsAction.java, and its method:
> public ActionForward execute(...) TO a jsp file, let's say display.jsp?
> and by the way, how to access that array within the JSP file.
>
> I think I am missing a crucial concept here, thanks for clarify this.

In your action,
   request.setAttribute( "myArray", myArray );

In your JSP use JSTL's <c:forEach> tag to display the values.

(The Struts <logic:iterate> tag can be used instead.  While we
recommend JSTL over the equivalent Struts tags, you might want to keep
things simple at first and avoid introducing another library.)

-- 
Wendy

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org