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.
Related
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.
i'm working with a struts2 application.
I have a form that once his is submit trigger an action :
<action name="AddDataAction" class="saisie.AddAction" method="add">
I need to create an intermediate page where the user need to log again before doing this action.
The tough thing is that I can only modify struts.xml to create that intermediate state and that after the user log in, it must execute this addDataAction method with the parameters from the form (to be save in a database).
I thought about interceptor but could they redirect to a jsp and call an action to verify the user login while conserving the data of the form to be save if the user log correctly ?
You can use result type redirectAction to achieve this.
<action name="OtherAction" class="saisie.OtherAction" method="otherMethod">
<result name="redirect" type="redirectAction">
<param name="actionName">loginAction</param>
<param name="yourParamName">${yourParamName}</param>
</result>
</action>
You can keep the parameter values by adding them to the result. Note the use of ${}. What this does is it calls the corresponding getters and adds them to the url. Then on the next page these values will be mapped to the corresponding variables using setters
You can read more about redirectAction results here
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.
I have a login action which after successful execution redirects to the previous page (I store the previous page in my session so I can fetch it later). In Struts2, I can find two ways to do this redirection:
<action name="login" class="com.myapp.login.Login">
<result name="redirect" type="redirect">${previousAction.requestURL}</result>
</action>
In this example, the getPreviousAction().getRequestURL() method (this is a selfmade method, its not native to Struts2) will be invoked and this will return the URL of the previous page as intended, for example:
somenamespace/index.action
There is also another type of redirection:
<action name="login" class="com.myapp.login.Login">
<result type="redirectAction">
<param name="actionName">${previousAction.name}</param>
<param name="namespace">/${previousAction.namespace}</param>
</result>
</action>
I want to use this `redirectAction result type because it is much cleaner. But, I have a problem when query parameters are part of the URL. For example:
somenamespace/index.action?name=john&age=50
I know I can add these params hardcoded in my struts.xml, but the problem is my login action should redirect to any previously invoked action, and I do not know beforehand which query parameters the previous actions had. This is different from the typical usecase where you know exactly to which action you're redirecting to
A very bad solution I found was adding every param possible (the collection of all params of all my actions in struts.xml) and then use the option:
<param name="suppressEmptyParameters">true</param>
You can save action name, namespace, and parameters from the ActionMapping.
ActionMapping mapping = ServletActionContext.getActionMapping();
You can also save query string instead of parameter map.
String params = request.getQueryString();
To add parameters dynamically to redirectAction result you should use OGNL in a dynamic parameter.
<param name="actionName">${previousAction.name +'?'+ parameters}</param>
Supposed you have a getter for parameters and initialized it from session where you saved previous query string, action name, and namespace.
This is my package structure in Struts.xml file
<package name="default" namespace="/" extends="struts-default">
<!-- Default action name <default-action-ref name="Index" /> -->
<action name="Index" method="index"
class="com.convergent.struts2.actions.UserAction">
<result name="success" type="dispatcher">/WEB-INF/html/index.jsp</result>
</action>
</package>
<include file="struts-admin.xml"></include>
My Index.jsp is access through this url
http://localhost:8888/Index
In Index page there is hyper link that redirect that user to Setting Page. Setting action is in the 'Admin' namespace so it is access as:
Setting
In setting page there is a hyper link to redirect the user to index.jsp page. action is called like this:
go back
As you can seen 'Index' action is in the default package having namespace '/'. So to handle this action namespace is changed and user is redirected to the index.jsp page. My problem is that although user is redirected to index.jsp page but the web url looks like
http://localhost:8888/Admin/Index
I want this url to
http://localhost:8888/Index
I don't know how to solve this problem. can anyone suggest me ?
"As you can see 'Index' action is in the default name space." - no it is in the default package and the namespace "/".
Advice: 1) Don't create namespaces without leading a leading '/', it generally isn't want you want. For basic use of namespaces see http://struts.apache.org/release/2.1.x/docs/namespace-configuration.html although that page does not cover the creation of namespaces without a leading slash, and the interesting behaviour you've experienced.
2) Use the namespace attribute of the struts2 url tag. When using Struts the tag reference is your friend: http://struts.apache.org/release/2.3.x/docs/tag-reference.html
As Roman mentioned, using the anchor tag will be a touch more straight forward.
Using just the url tag you would have:
go back
When this is a bit clearer:
<s:a namespace="/" action="Back">go back</s:a>
To solve this problem what i do rather than redirecting the user to index.jsp I first redirect the user to another action like this
go back
To handle this action mapping in struts-admin.xml is:
<action name="Back">
<result name="success" type="redirectAction">
<param name="namespace">/</param>
<param name="actionName">Index</param>
</result>
</action>
This way I solve the problem.