Help:Action forward to jsp directly in struts

A

avrootshell

I have a Action class which takes care of business logic in struts.
Depending on some condition i'm thinking of forwarding the action
directly to jsp instead of giving path to action mapping.

I was trying to do something like this.


ActionForward af = new ActionForward();
af.setRedirect(true);
af.setPath("/jsp/some.jsp);
return af;

Bcoz i know i can do,[af.setPath = "../app/search");]
It means i can specify action mapping path here.

But this doesnt seem to be working .

One way to do would be like this:
return actionMapping.forward("success");
And in the action mapping in struts-config , i can forward it to jsp.

But i dont want to do this way. Bcoz there are so many conditions and i
dont want so many forwards in struts config .
Is there any way i can forward request to jsp directly?
Any kind of help is appreciated.
 
M

Manish Pandit

You can use the request dispatcher like this:

request.getRequestDispatcher("path to your jsp").forward(request,
respose);

Just make sure you do not commit your response before calling this
method.

-cheers,
Manish
 
A

avrootshell

Thanks for ur help Manish,

I need to return actionmapping.

so do you suggest, instead of

return mapping.forward("success");
just include

request.getRequestDispatcher("path to your
jsp").forward(request,respose);

I'm sorry , i'm new to struts . SO this type of questions may arise.
 
M

Manish Pandit

You're welcome - and its okay - Struts can be confusing at times.

You do not need to return an actionmapping. When you use request
dispatcher, the server will direct your request and response to the URL
you specify. The control is *never* going to come back to the execute()
method. For compilation's sake, you can do a return null. However, to
reiterate - make sure you do not commit your response before calling
the dispatcher - this will lead to an illegal state exception. Normally
I've used this API to process the request, set some request attributes
for the JSP being called and thats it. I never touched the response
throughout this process.

On another note, if you wish the control comes back to execute(), look
at the include() method of RequestDispatcher interface.

-cheers,
Manish
 

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,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top