0

Does the Strings that i use in the Action class which i access as the Properties by means of the tag get Stored in Value Stack?

I have a jsp say JSP1, which has a form with a textInput field that has name = user

On submitting the form, an Action class is called. It has access to it by using getters and Setters.

But in a Custom Interceptor, how can i access it?

I tried to get it from the value stack as

ValueStack stack = ai.getStack();
String s = stack.findString("user");
stack.set("user",sa.toUpperCase());

But it returns null.

Also i tried to have a String variable with getters and setters in the CustomInterceptor class. It too failed to serve the purpose.

How to get the value to the Interceptor?

VijayaRagavan
  • 221
  • 2
  • 15
  • Hard to understand what you're asking, sorry if this is misinterpreted:(In general) You have a form, which you set values. This form is passed to the web application as a request. Struts2 processes the request by setting the values onto the object (among other things). Then a view is rendered. So how to get a variable in the interceptor? Well it depends on where you interceptor is... is it _before_ the parameters are set, or after? If before you need to access the request directly: http://stackoverflow.com/questions/1578998/how-to-get-the-request-string-including-parameters if after... – Quaternion Feb 04 '14 at 17:50
  • then you can access your actions class and directly dig out what you need (and benefit from conversions should you require them). Someone else can more clearly demonstrate both methods. – Quaternion Feb 04 '14 at 17:52
  • As we get the values inputted in the form by getters and setters in the action class, is there a similar method to access the same values in Custom Interceptors? – VijayaRagavan Feb 05 '14 at 05:36
  • The interceptors do the setting from the request... after the parameters interceptor has run (your interceptor follows this one) then you can get the parameters from the Action _Action action = (Action) actionInvocation.getAction();_ You'll want to create an interface and cast the action to it, so you know what it can do... If the action must be _before_ the params interceptors then you'll need to dig in the request, because the values will not be set yet. – Quaternion Feb 05 '14 at 17:01
  • Sorry pal. I don't get it. Can you pls explain it with an example. – VijayaRagavan Feb 06 '14 at 05:05
  • I'll try to make time, but to avoid a future question can you please state what you are trying to do, then the example will probably be a lot more accurate. What is the purpose of your interceptor? – Quaternion Feb 06 '14 at 06:14
  • The value which i enter in a particular textfield should be converted to Upper case for further processing in the action class and later stored in a Db, for which i tried using a custom interceptor. – VijayaRagavan Feb 06 '14 at 08:29
  • What you propose could work, but interceptors are idea for applying "cross-cutting" logic, this seems to be a specific issue. Instead I would A) simply use .toUpperCase() in the setter. B) If this applies to multiple fields use a type converter: http://struts.apache.org/release/2.0.x/docs/type-conversion.html#TypeConversion-BuiltinTypeConversionSupport and using annotations http://struts.apache.org/release/2.3.x/docs/typeconversion-annotation.html don't have time yet to write out something properly yet – Quaternion Feb 06 '14 at 17:02
  • Well thats fine pal. Thanks a lot. – VijayaRagavan Feb 07 '14 at 05:54
  • 1
    I think you will get your answer on this link : [http://stackoverflow.com/questions/17650146/interceptor-cant-access-action-parameters][1] [1]: http://stackoverflow.com/questions/17650146/interceptor-cant-access-action-parameters – bobLeMarrant Oct 07 '14 at 09:41

1 Answers1

0

Invoke defaultStack or params interceptor before custom interceptor:

<interceptor-ref name="params"></interceptor-ref>

OR

<interceptor-ref name="defaultStack"></interceptor-ref>

Write above line before registering your custom interceptor with the specified action.

Community
  • 1
  • 1