Struts2 clearing parameters after validation - java

I have a URL="deliverable.do?id=123". After clicking the submit button, it goes to the validate method and find some missing field.
Then, when it goes back showing the problem (which is what I expect), the new URL is being displayed without the parameters URL="deliverable.do".
BTW, I cannot make a redirect, because if I do it, I will lose the previous information entered in the form.
<action name="deliverable" class="DeliverableAction">
<result name="input">/WEB-INF/deliverable.ftl</result>
<result name="success" type="redirectAction">
<param name="actionName">deliverable</param>
<param name="id">${deliverable.id}</param>
</result>
</action>
I understand that the validation method will return INPUT if some problem occurs, however, is there a way to keep the parameters in the URL?

My guess is that your form action attribute doesn't have parameters in the URL and the method is post. When you submitted a form you didn't specify any parameters in the URL. In most cases when you doing post request you don't want to use parameters in the URL, so you can use hidden field with the name of the parameter. After the form is submitted you can get the parameter in the same way as any other request parameters. This parameter won't be passed with the URL but in the body of the request, so request methods to retrieve parameters work, and struts can populate this parameter to the action bean if it has a public setter.
<s:form action="deliverable" method="post">
<s:hidden name="id" value="123"/>
...
</s:form>

Related

Is it possible to pass a <param /> from struts.xml as hidden parameter?

I wondered if it is possible to pass a hidden parameter to an action defining it inside struts.xml. I've read the struts.xmlDocument Type Definition HERE and it seems that it's not supported.
I need to pass an user's name and surname from an action to another and I don't want this two parameters to appear on the URL (I seek a POST request but from struts.xml I only know how to send GET ones).
Another option would be to send them programmatically form my action's code, but I can't use any <s:form/> as I'm not calling it from any JSP, so it has to be done straight from the action's java code.
My current struts.xml action code looks like this (parameters showing on URL):
<action name="myAction"
class="path.to.my.action.class"
method="doSomething">
<param name="name">${name}</param>
<param name="surname">${surname}</param>
<result name="success">success.jsp</result>
</action>
EDIT - Clarifying my question:
I want to archieve the same thing you can archieve using a <s:form/>with hidden parameters like this:
<s:form theme="simple" action="myAction">
<s:hidden name="name"/>
<s:hidden name="surname"/>
<s:submit value="%{'Submit'}"/>
</s:form>
That would trigger myAction and pass through the two parameters, nameand surnamebut the user would not be able to see those parameters.

Intermediate redirection in struts2

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

Redirecting to another action with unknown amount of parameters in Struts 2

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.

unable to set forwarding in Struts

I have a slight problem with forwarding using Struts.
Now when users accessing my page like this -> http://mypage/
they are automatically forwarded to /index.jsp.
But I'd also like to have index.jsp to be linked to name "sg".
So when they access page like this :
http://mypage/ > they will be forwarded to http://mypage/sg
which is http://mypage/index.jsp.
As I've already mentioned above I'm using Struts to handle all these action. The below example is what I have in my struts.xml file. But it's working rather partially. When I access the page as stated above I'm getting redirected to http://mypage/sg and it also gives me 404 - Not Found.
However when I try manually accessing the url (http://mypage/sg), it works perfectly.
<package name="index" namespace="/" extends="default">
<action name="">
<result>/sg</result>
</action>
<action name="/sg">
<result>/index.jsp</result>
</action>
</package>
When I access the page as stated above I'm getting redirected to http://mypage.com/sg and it also gives me 404 - Not Found.
Answer :
If you want to call another action as result of one action then you need to mention attribute type of result tag
<action name="">
<result type="redirect">/sg</result>
</action>
This will redirect to action sg.
The redirect result type:
The redirect result type calls the standard response.sendRedirect() method, causing the browser to create a new request to the given location.

hiding contents of the URL in struts

I want to hide the parameters which are attached in my url .
My action is being hit from another website (URL redirection)
https://myserver/web/myaction.do?username=jhon&password=1234
on this action I simply redirect to a jsp
<action name="myaction" class="ncom.company.project.Head">
<result>/pages/HelloWorld.jsp</result>
</action>
on my jsp username and password are visible , i want to hide them
I guess you mean the parameters should not be visible in the URL, right? If so, you should do a real redirect, what you're doing is a actually forward.
In Struts 2 you'd use the "redirect" result type to send the redirect to the browser. The parameters should still be in your session/action context.
May I suggest that you use a POST request. As far as I can see from your question you're sending a GET request. That's why you can see the parameters in the URL. I don't think this is a struts-specific problem. It's part of HTTP. Just make the other website to use a POST request.
You might wanna take a look here too to find out the usage of both methods.
You can do 2 things here either the one like redirect but in that case it means creating a new request object and a new action instance in value-stack.
I believe that once application hits the myaction and once you have done the work you can use redirectAction by passing any any parameters you need (if any).
But just be clear that even redirectAction will create a new Request Cycle you can use redirectAction like
<action name="myaction" class="ncom.company.project.Head">
<result type="redirectAction">
<param name="actionName">redirectAction</param>
// any parameter you want
</result>
</action>
<action name="redirectAction">
<result>/pages/HelloWorld.jsp</result>
</action>
here are more details for the redirectAction
Redirect Action
hope it will help you

Categories