Sunday, January 15, 2012

How to forward a request from Struts2 action to a JSP



Usually you'll never want to forward an entire request from a Struts2 action to jsp. But if in case, it's really required (I required it once) and have less time in h, then here's what is to be done.

Action (MyAction.java)



Struts.xml



JSP (params.jsp)



The above code will pass the entire request received by calling myaction.action to the JSP page params.jsp, which will then print all the parameters which were present in the request.
Now you may be thinking, don't we have to create variables (getters & setters) for all variables in MyAction.java - the answer is NO, you don't have to create any variables with their getters & setters, because using Struts2 Dispatcher result, we've forwarded the entire HttpRequest as it is to the JSP.

Let me know if you require any further clarifications/explanations. I'll be glad to help.