dynamic global result in struts2 xml - struts2

My need is to have a global result in struts.xml in having the dynamic value for the action to be redirected to.
Like,
<global-results>
<result name="customResult" type="redirectAction">
<param name="actionName">${customValue}</param>
<param name="namespace">/</param>
</result>
</global-results>
This customResult is being returned from one of my interceptor. customValue is the member of the same interceptor with getter and setter.
I am aware that action specific results can have dynamic values in it, provided the dynamic param shuould get its value assigned in the execute method of the particular action. And, that dynamic param should be the member of that action class.
Since here it is needed in global result, I made the dynamic param to be the member of the interceptop where I return that particular global result. But, this ${customValue} is not getting values while redirecting. It simple gets redirected to localhost:9080/myapp/.action.
Please suggest

The action properties will be put into the value stack that is why when you have an expression (like ${customValue}) in configuration, the values will be retrieved with appropriate getters. In order to achieve same behavior inside an interceptor you can put desired values directly into the value stack. The value stack can be retrieved from the invocationContext.
invocation.getInvocationContext().getValueStack()
.set("customValue", "some_action");

Related

struts 2 : how to pass a global parameter from one action to another

I have a case like this:
JSP -> ACTION 1 -> Redirect-> Action 2 -> JSP
While redirecting we loose all the parameters. But there is one parameter that I'd like to pass from one action to another. The parameter is always the same for all actions.
I know that it is possible to write the following code and it works :
<action name="myAction" class="myActionClass" method="doThis">
<result name="success" type="redirect">doThat.action?myParam=${myParam}</result>
</action>
The only thing is that I'd like to pass the parameter automatically from one action to another when I have a redirect without writing anything in my action tag, but I don't know how to code it.
Is it possible with struts 2 to do that?
Someone suggested to put it in global-results, I tried, but still haven't succeeded.
You can set the property in to the session as an attribute and the same attribute can be taken back from the session irrespective of the action class you are using.
session.setAttribute(name,value);
session.getAttribute(name)

Struts2 : How to use an attribute of an action class in another action class?

I have 2 Action classes : LoadingFormAction.java and FormValidationAction.java. In the first one, I create the functions which help in uploading data from an Excel file, and in the second I need to validate the loaded data according to the other fields values. LoadingFormAction.java control the view Form.jsp and FormValidationAction.java the view FormValidation.jsp. My question is : How to evaluate the attributes of the first action and use them in the second Action?? Is it obligatory to pass it as a session parameter? Thank you a lot.
<action name="LoadingFormActionName" class="com.test.LoadingFormAction">
<result name="success" type="redirectAction">
<param name="actionName">FormValidationActionName</param>
<param name="yourProperty">${yourProperty}</param>
</result>
</action>
Where yourProperty is the parameter of your LoadingFormAction class which you want to pass
to FormValidationAction class. You need to declare getter/setter for this variable in both the classes. Also, LoadingFormActionNameand FormValidationActionName are the corresponding action names.

Struts2 wildcard action mapping with ognl expressions as field-names

I am trying to populate a parameter named 'member.id' in an action class using regular expression and wildcard action mapping with Struts2. The action mapping is something like this:
<action name = "validationEmailHTML/{sac:[^/]*}/{member.id:[0-9]*}" class = "controller.signUp.ValidationEmailContentController">
<interceptor-ref name="securityStack"/>
<result name="success">/signup/validationemail.jsp</result>
<result name="dbconnectionerror">/error/500.jsp</result>
<result name="unknownerror">/error/500.jsp</result>
</action>
The first parameter 'sac' gets populated correctly but the 'member.id' is being ignored. Am I missing something here or is it because ognl expressions to specify field-names is not an option when regex is used as a pattern-matcher?
It seems that it is impossible to use OGNL expressions for parameter names when the regular expression pattern-matcher is used in struts2. The only possible way is to use simple value members in your action class and write the code to set the value of properties of complex objects inside the class methods.

Can you do a struts2 action redirect using POST instead of GET?

<action name="actionA" class="com.company.Someaction">
<result name="success" type="redirect-action">
<param name="actionName">OtherActionparam>
<param name="paramA">${someParams}</param>
<param name="paramB">${someParams}</param>
<param name="aBoatLoadOfOtherParams">${aBoatLoadOfOtherParams}</param>
</result>
</action>
In the above action map, I am redirecting from SomeAction to OtherAction. I am having issues, because unfortunately I need to pass a large amount of data between the two actions. IE7 will only allow GET requests to be like 2k, so its blowing up when I'm just over that limit when the response calls a get request to the other action.
Is it possible for me to set this redirect, to end up with a POST being called to the other action?
As the docs states:
The only way to pass data [after a redirection] is through
the session or with web parameters (url?name=value) [i.e., query string for a GET request]
Perhaps a case for action chaining? I'm not sure, and it's not usually recommended, but it seems that you scenario is rather unusual, so it might pay to take a look.
In this case, we are not really making a redirection, i.e., we are not going back to the client, but keeping everything inside the server. Supposedly, then, the full interceptor stack is executed again - and the posted data should impact on the new action, one would hope...

Referencing actionerror from httpheader result in struts.xml

In my TestClass action I am setting an action error using addActionError method. I have an action defined in the struts.xml as following
<action name="TestAction" class="TestClass">
<result name="input">/jsp/test.jsp</result>
<result name="error" type="httpheader">
<param name="error">409</param>
<param name="errorMessage">${SOME-EXPRESSION}</param>
</result>
</action>
The intent is to have the error message display whatever was added using addActionError. According to the org.apache.struts2.dispatcher.HttpHeaderResult documentation, I should be able to use Ognl expressions within errorMessage parameter.
So, is it possible to put something in place of ${SOME-EXPRESSION} that will reference actionerror in this scenario.(I tried ${actionerror} but it didn't work)
I know that I can have a workaround by declaring my own field (for example "errorText") in the action class and using that insteda of addActionError referencing it using ${errorText} inside the param tags. But before I go that route, want to make sure that's the only way.
Action errors are stored in a list, so you'll have to show something like ${actionErrors[0]}. But keep in mind this way it will only show your first added error, not all those you have included using addActionError.

Resources