Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

struts 2 passing parameter to interceptor

 
shashi wagh
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In my struts 2 application I have a LoginInterceptor
I want to pass some parameter through the struts.xml file
How can I pass the parameter to the interceptor.
In other words I want the parameter which I send to the action from struts.xml

e.g.


I want menuId in interception which I have configure for this action.

How do I get the value In interceptor.

I have already try for getter setter for menuId in interceptor.
 
Tom Rispoli
Ranch Hand
Posts: 349
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think these parameters will get places into the request and can be retrieved from there. Also, if you make bean properties for these values in your action class I think they will be placed into the action class as well.
 
shashi wagh
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am getting value in action class by getter and setter
but I want the value in Interceptor

even I have use

String menuId = request.getParameter( "menuID");

in my Interceptor.
 
Tom Rispoli
Ranch Hand
Posts: 349
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your intercept method should recieve a parameter of type ActionInvocation. If you call .getAction on this object you can get a reference to your action class and then you can get the values from there.
 
shashi wagh
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Tom it's working fine.
 
Krishnan Rathnakumar
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, shashi wagh

i need a example for what you got a solution, because i am also have a same problem.
how can i get a parameter values in interceptor.

please help me.

thanks in advance
 
Rajukpo kumar
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Krishnan,

You got any example regarding same..?

It will be very helpful if you could share that if so.

Thanks,
Raju.
 
Anirban Chowdhury
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Struts 2.0.11 version

MyInterceptor implements Interceptor , StrutsStatics{

public String intercept(ActionInvocation invocation) throws Exception {
final ActionContext context = invocation.getInvocationContext ();
HttpServletRequest request = (HttpServletRequest) context.get(HTTP_REQUEST);
Map params = request.getParameterMap();
//Retrieve the map and check..
for (Iterator iterator = params.keySet().iterator(); iterator.hasNext();) {
String key = (String) iterator.next();
String[] obj = (String[])(params.get(key));
if(obj.length>0)
System.out.println(key+"<>"+obj[0]);


}

}
 
Rajukpo kumar
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Anirban,

Thanks for your reply...

I followed the way that i mentioned below.



Regards,
Raju
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic