Setting a string parameter inside struts.xml action from a properties file - java

I've been trying to implement an action which allows the user to download a file with a specific filename. This filename is set by passing the action a parameter through struts.xml this way:
<result name="success" type="stream">
<param name="contentType">application/octet-stream</param>
<param name="inputName">input_document</param>
<param name="contentDisposition">attachment;filename=foo.bar</param>
<param name="bufferSize">1024</param>
</result>
I have omitted the rest of the code as I just want to focus on this:
<param name="contentDisposition">attachment;filename=foo.bar</param>
That way it works perfectly and lets you download the foo file with .bar extension.
So here's the deal, I was curious to know wether if is it possible to retrieve the extension from a properties file and pass it through the parameter, for example, like this:
<param name="contentDisposition">attachment;filename=foo%{+ getText("EXTENSION_KEY_IN_PROPERTIES_FILE")}</param>
I know that getText("...") won't work but I just want you to understand what I'm searching for.
I'm currently working with some properties files for global parameters and localization stuff so it would be great if I could retrieve this file extension from one of them.

It should work if your action implements TextProvider
<param name="contentDisposition">attachment;filename=foo${getText('EXTENSION_KEY_IN_PROPERTIES_FILE')}</param>

Related

How to make fileUpload parameters dynamic in Struts 2

In a Struts 2 application we use fileUpload interceptor to get file from the user.
The fileUpload has some configurations maximumSize , allowedTypes , allowedExtensions that can be used as:
<interceptor-ref name="fileUpload">
<param name="maximumSize">200000</param>
<param name="allowedTypes">text/plain</param>
<param name="allowedExtensions">txt</param>
</interceptor-ref>
Is it possible to make these parameters dynamic !?
For example :
<param name="maximumSize">${maxsize}</param>
and let the action set its max file size.
It's not possible to make these parameters dynamic. But at runtime when interceptor is invoked you can get the value dynamically
String maxsize = TextParseUtil.translateVariables(maximumSize, actionInvocation.getStack());
The action is invoked after interceptors chain, so it can't set the value. However you can translate the value before chained result.

I18n redirection issue in struts2 web application

In my application Locale is dynamically selected by user from login page and I am saving selected locale in Cookie.
I am facing I18n related problem in case of redirection in my web application.
When I redirect a page by URL by appending current locale then my application language remains same as selected language(i.e working fine in this case), but when I redirect to next page by struts redirection defined in struts.xml file the locale changes to the default value(English in my case).
For example:
In case of- <result name="success" type="redirect"> , locale changes to default one.
In case I remove type=redirect then its working fine but my form will be submitted two times.
So is there any way to append locale to struts redirection at runtime?
Here is one of my interceptor stack:
<interceptor-stack name="sessionValidateStack">
<interceptor-ref name="auditTrail"></interceptor-ref>
<interceptor-ref name="sessionCheck"></interceptor-ref>
<interceptor-ref name="service">
<param name="code">DG</param>
<param name="interfaceType">WEB</param>
</interceptor-ref>
<interceptor-ref name="defaultStack"></interceptor-ref>
<interceptor-ref name="expHandler"></interceptor-ref>
</interceptor-stack>
Add request_locale=<your_language_key> in your parameters
Use redirect-action instead of redirect in action result type because in case of redirect action,control jumps to the different action(in same or other package).
Please refer link for the difference between redirect and redirect type result type.

Struts2 pass parameters to action in different webapp

I use Struts 2.3.16.3. I want an action from webapp 1 to pass parameters to an action in webapp 2. In the struts.xml of webapp 1 I define the following result:
<result name="success" type="redirect">
<param name="location">http://localhost:8080/Webapp2/index.action</param>
<param name="testParam">testValue</param>
</result>
I expect my browser to redirect me to this webpage (a page in webapp2) when the result equals 'success':
http://localhost:8080/Webapp2/index.action?testParam=testValue
However, my browser takes me to:
http://localhost:8080/Webapp2/index.action
completely ignoring the parameter.
If I change my result to have everything inside the location param then it works, but you can see this gets very clunky with multiple params:
<result name="success" type="redirect">
<param name="location">http://localhost:8080/Webapp2/index.action?testParam=${testValue}</param>
</result>
This correctly redirects my browser to the url:
http://localhost:8080/Webapp2/index.action?testParam=testValue
Why does the first method not work?
If the location starts with http:, https:, mailto:, file:, ftp: then it's used as a final location to redirect using response.sendRedirect(). Parameters in the result using <param> tag in this case are ignored.

Dynamically set value to static parameter in Struts 2

I was wondering, is it possible to dynamically set value to static parameter
<action name="TestApp_*" class="test.TestApp" method="{1}">
<param name="app_Id">Dynamic value here</param>
<result name="input">WEB-INF/jsp/test/testView.jsp</result>
</action>
I tried
<action name="TestApp_*" class="test.TestApp" method="{1}">
<param name="app_Id">${app_Id}</param>
<result name="input">WEB-INF/jsp/test/testView.jsp</result>
</action>
Not working. It shows ${app_Id} as regular text.
Note sure what exactly you mean by It shows ${app_Id} as regular text.Struts2 provide this flexibility where you can set param values dynamically.
All you need to set these values in your action class and you are all set to use them as a place holder.
public class MyAction extends ActionSupport {
private int app_Id;
public String execute() {
// you execute logic
this.app_Id= 123;
return SUCCESS;
}
// getter and setter for app_Id
}
And you can use this app_Id in your result configuration
<action name="TestApp_*" class="test.TestApp" method="{1}">
<param name="app_Id">${app_Id}</param>
<result name="input">WEB-INF/jsp/test/testView.jsp</result>
</action>
you can get more details parameters-in-configuration-results
You probably have written (erroneusly) the getter by hand, instead of letting the IDE do it for you.
A variable app_Id; must have a getter getApp_Id(), while you could have something like getapp_Id() or get_app_Id()... but how are we supposed to know it if you don't post your Action code ?!
Also, consider to change your variables (from now on) to respect the convention, that wants them camelCased, instead that underscore-separated... then appId for the variable and getAppId() for the getter.
try to do it like this way..
<result type="redirect">
<param name="location">WEB-INF/jsp/test/testView.jsp</param>
<param name="inputName">app_Id</param>
</result>
in the jsp page you can directly use this value like
<s:properties value="app_Id">
try this.
Look at the staticParams interceptor.
This interceptor populates the action with the static parameters defined in the action configuration. If the action implements Parameterizable, a map of the static parameters will be also be passed directly to the action. The static params will be added to the request params map, unless merge is set to false.
Parameters are typically defined with <param> elements within struts.xml.
It shows how to configure this interceptor to your action configuration.
For example:
<action name="someAction" class="com.examples.SomeAction">
<interceptor-ref name="defaultStack">
<param name="staticParams.parse">true</param>
<param name="staticParams.overwrite">false</param>
</interceptor-ref>
<param name="num">${numValue}</param>
<result name="success">good_result.ftl</result>
</action>
This enables the interceptor to parse parameter values for OGNL expression from the action configuration.

How to return json data selectively in a struts2 action class

I have several properties with getter and setter method in one action class.
Those properties do not perform the same task. Actually, they respond to different business-service requests, or they are related to different actions.
And my problem is like this:
I need to filter out the data and return only part of the properties within the property set because not all properties are necessary in a single request(action).
PS:Actually, I might have separated those actions or business logic into several classes instead of putting them into one action class. However, I think they all share the similar DAOs and services so I put them together in order to prevent redundant IOCs.
Struts2-JSON plugin allows you to exclude null properties
<result type="json">
<param name="excludeNullProperties">true</param>
</result>
or exclude certain parameters from being serialized
<result type="json">
<param name="excludeProperties">
login.password,
studentList.*\.sin
</param>
</result>
See documentation for more details

Categories