unable to set forwarding in Struts - java

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.

Related

Strut2 having multiple packages cause problems

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.

Struts-config.xml configuration mapping

In struts-config I have action like this :
<action
path="/action/basket"
type="com.xxx.BasketAction"
name="basketForm"
scope="session"
unknown="false"
validate="false"
>
<forward
name="displayItems"
path="EshelfItems"
redirect="false"
/>
<forward
name="displayItems-redirect"
path="/action/basket.do?fn=display"
redirect="true"
/>
<forward
name="displayBasket"
path="/basket.jsp"
redirect="false"
/>
</action>
Where I can find the mapping for /action/basket.do?fn=display ?
According to http://struts.apache.org/1.x/faqs/works.html :
In the framework configuration file(s), you associate paths with the
controller components of your application, known as Action classes
(i.e. "login" ==> LoginAction class). This tells the ActionServlet
that for the incoming request 'http://myhost/myapp/login.do' it should
invoke your controller component, LoginAction.
Note the extension .do in this URL. The extension causes your
container (i.e. Tomcat) to call the ActionServlet, which sees the word
"login" as the thing you want to do. The configuration is referenced,
and your LoginAction is executed.
Check your BasketAction class where the fn=display request parameter is checked.
If you can't find the action class for a given mapping then debugging your
RequestProcessor's process method can be also useful.
Ya your question is not clear..
When you hit /action/basket.do?fn=display its going to use the following mapping in your struts config.
<action
path="/action/basket"
And then it will execute
com.xxx.BasketAction
and in that class if you want you can read the param fn=display and then do a forward and that will work as mapped in <forward>...</forward> mapping of your struts config

Struts 1.3 action forward parameter

I'm working on a little project which uses Struts 1.3 and I encountered the following problem.
After some business logic takes place in an Action i want to forward the control to another Action which is mapped in struts-config.xml.
Usually this is the way I'm solving this:
struts-config.xml
<action path="/boardCreate" type="com.example.BoardCreateAction" name="BoardCreateForm" input="/board.jsp">
<forward name="success" path="/board.do" redirect="true" />
</action>
Java action class
return mapping.findForward("success");
This will take make a redirect to the board.do action which is also mapped there.
My problem is that I want to redirect the control to something like:
<forward name="success" path="/board.do?id=1" redirect="true" />
Notice the id=1 parameter. Is this any other way except rebuilding my own action forward for this? I can't find any documentation debating this matter. Thanks!
ActionRedirect redirect = new ActionRedirect(mapping.findForward("success"));
redirect.addParameter("id", theId);
return redirect;
See http://tool.oschina.net/uploads/apidocs/struts-1.3.10/org/apache/struts/action/ActionRedirect.html

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

Stop user from accessing a Struts2 action directly?

I have the following actions defined in my struts.xml
<action name="Search" method="prepareLookUpvalues" class="com.mycompany.actions.FrSearchAction">
<result name="success" type="tiles">search.layout</result>
</action>
<action name="List" class="com.mycompany.actions.FrSearchAction">
<result name="success" type="tiles">results.layout</result>
<result name="input" type="tiles">search.layout</result>
</action>
<action name="SearchDetails" class="com.mycompany.actions.FrSearchDetailsAction">
<result name="success" type="tiles">details.layout</result>
</action>
<action name="Logoff" class="com.mycompany.actions.LogoffAction" >
<result name="success" type="tiles">logoff.layout</result>
</action>
Assuming that a user goes directly to my page home http://localhost:8080/fr/Search.action everything works OK, but it has been discovered hat some users are accessing http://localhost:8080/fr/List.action directly without first going to the search page which is causing problems.
When a user goes to the search page and enters criteria and submits, it is only then that the "List" action should be called via the struts form's action attribute. I basically want to stop users from being able to access the "List", "SearchDetails", and "Logoff" actions directly unless those actions are invoked from my JSPs or code.
I'm new to maintaining/developing Struts2 applications and I haven't found clear answers to this. Any suggestions would be greatly appreciated!
There's a few details missing so the answer will be a bit vague, but the list action probably pulls values from a form submission to search? Or pulls state from session? Or...?
Anyways, however that may be stored, simply check and then redirect the user to Search if the state is not set as expected.
For details on doing redirects in struts2, see, for e.g. http://www.roseindia.net/struts/struts2/actions/struts-2-redirect-action.shtml
This isn't an elegant solution but you could try checking for the referer to see who called the action. You action class will need to implement ServletRequestAware.
String referrer = request.getHeader("referer");
if (referrer.equals("http://localhost:8080/fr/Search.action")) {
// do the action
} else {
// handle unwanted access
}
Remember that the referer is a client-controlled value and can be spoofed or removed.

Categories