hiding contents of the URL in struts - java

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

Related

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.

How to lookup configured Struts action

I want to do the following:
final Action myAction = getActionDefinedInStrutsConfig(param);
myAction.execute(params);
Is there a way to lookup the actions that the ActionServlet has initialized ?
I can create a new one like so:
final Action myAction = new ActionImpl();
myAction.execute(params);
but this way the new action is not properly initialized, the attached servlet is not set and getServlet() returns null.
A little clarification on why I need this:
The problem is that I currently have 2 login pages. One for normal users and one for admins. They should be separate systems completely, but the fact is they're currently not. I need to make a 'proxy' login page which decides which login page to redirect to according to the request. If I redirect to the URL however the UI would be drawn. I need to call the either the user or admin login actions to process my proxy page request. Also moving the logic inside a service, while being the correct approach, is not currently an option.
Ok, since I see what you mean here's what I would suggest:
Use your action for validatory purposes, i.e., retrieval of data from ActionForm and checking for validity. Once all information is done, send the info to a service.
The service (need not to be a web service, but a simple POJO) will have the business logic of the application, with relevant exceptions and return types. Once you call the relevant service with its method, retrieve the result and, finally,
Populate your necessary ActionForm or do a mapping.findForward().
This way, if you need another business logic that is used by another Struts Action, rather call the service that the 2nd action uses. This is an effective way for code-reuse and good OOP practise.
Hope this helps.
The hackable way would be to do this:
final Action myAction = new ActionImpl();
myAction.setServlet(getServlet());
/* ONLY if your form enctype is "multipart/request-data". */
myAction.setMultipartRequestHandler(getMultipartRequestHandler());
//Finally
myAction.execute(params);
You can define actions in your forwards:
<action parameter="command" path="/firstAction"
input="firstAction.tiles" name="someForm" scope="session" validate="true" type="com.mycompany.FirstAction">
<forward name="toSecond" path="/secondAction.do?command=someMethod" redirect="true"/>
</action>
<action parameter="command" path="/secondAction" input="secondAction.tiles" name="someForm"
scope="session" validate="true" type="com.mycompany.SecondAction">
<forward name="backToFirst" path="/firstAction.do?command=myMethod" redirect="true" />
</action>
Now you can use mapping.findForward("toSecond"), in your first action and mapping.findForward("backToFirst") on the other.

struts2 - understanding the value stack

I have a question regarding the struts2 value stack. Let's say I have an Action class called RegisterAction that has an execute method as follows:
public String execute() {
ValueStack stack = ActionContext.getContext().getValueStack();
stack.push(new String("test string"));
return SUCCESS;
}
My struts.xml looks like this:
<struts>
<package name="default" extends="struts-default">
<action name="*Register" method="{1}" class="vaannila.RegisterAction">
<result name="populate">/register.jsp</result>
<result name="input">/register.jsp</result>
<result name="success">/success.jsp</result>
</action>
<action name="*Test" method="{1}" class="vaannila.TestAction">
<result name="test">/test.jsp</result>
<result name="success">/success2.jsp</result>
</action>
</package>
</struts>
So control will flow to the success.jsp after the execute method executes in that class.
My questions are:
1) how do I get that value I pushed on the stack in the success.jsp?
2) Let's say in success.jsp I have a <s:submit method="testMethod" /> that redirects to an action class called TestAction. In other words, from the Register page, the user clicks submit, and in the execute method of the RegisterAction we push the "test string" on the stack. Then we go to success.jsp. The success.jsp has a submit button that directs us to TestAction#testMethod. In TestAction#testMethod, is the value I pushed on the stack in RegisterAction#execute still there? How can I get it? I stepped through the eclipse debugger but I don't see the value.
Thanks.
Ok, I figured this out.
1) The way I found to get objects on the value stack so we can access them from a jsp is like this:
Map<String, Object> context = new HashMap<String, Object>();
context.put("key", "some object");
context.put("key2", "another object");
ActionContext.getContext().getValueStack().push(context);
In other words, we can put a HashMap on the value stack containing the objects we need. Then, in the jsp, we can access the actual values like this:
<s:property value="key" />
<s:property value="key2" />
It will look through the value stack and find those values in the HashMap we pushed.
2)
An instance of the action class is associated with just one request. So when we go to another action and then end up at another jsp, the stuff we pushed on the value stack from the first action won't be there since the other action has it's own value stack.
reference: http://www.manning-sandbox.com/thread.jspa?messageID=93045
You guys can feel free to correct me if any of this is wrong or if there are smarter ways to do these things :).
Thanks.
Struts 2 adds your action to the top of the value stack when executed. So, the usual way to put stuff on the Value Stack is to add getters/setters for the values to your Action class. You still use the s:property tag to access the values.
A CRUD tutorial: http://struts.apache.org/2.1.6/docs/crud-demo-i.html
just define a property like
String string1 = "test string";
in your action.
in jsp you can access directly.
e.g
<s:property value="string1"/>
will print out
"test string"
Normally, as Nate says, you will use a field in your action, since the action is always on the ValueStack. However, this doesn't work if you're writing interceptor code since the interceptor will be gone by the time the template (JSP/freemarker etc) is invoked. There you need to put some kind of java bean-like object on the ValueStack, just as you do above.
Hi just for information
These is a downside of using getValueStack().getContext() sometimes the data was not available in .ftl (data was not showing in line2, but it was coming in line1) i really dont know the reason for this. But using .getValueStack().set("resDTO",resDTO); the problem was solved (data was getting populated for both the functions).
${resDTO.data} //line 1
var selonload='<#s.property value="resDTO.data" escape="false" />'; //line 2

Categories