Strut2 having multiple packages cause problems - java

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.

Related

Is prefix needed for Struts2 namespace in forms and links?

From the Apache documentation:
While the prefix appears in the browser URI, the tags are "namespace
aware", so the namespace prefix does not need to be embedded in forms
and links.
struts.xml:
<package name="testpkg" namespace="/test" extends="struts-default">
<action name="doTest" class="otes.test.TestAction">
<result>/success.jsp</result>
</action>
</package>
index.jsp: (http://localhost:8080/nsdemo/)
<h2>Using HTML tags:</h2>
<h3>doTest without namespace</h3> <!-- 404 error -->
<h3>doTest with namespace</h3> <!-- works -->
<h2>Using Struts2 tags:</h2>
<h3><s:a href="doTest">doTest without namespace (s:a href)</s:a></h3> <!-- 404 error -->
<h3><s:a href="test/doTest">doTest with namespace (s:a href)</s:a></h3> <!-- works -->
<!-- 404 error -->
<s:url action="doTest" var="myAction" />
<h3><s:a href="%{myAction}">doTest without namespace (s:url action)</s:a></h3>
<!-- works -->
<s:url action="test/doTest" var="myAction" />
<h3><s:a href="%{myAction}">doTest with namespace (s:url action)</s:a></h3>
Does this mean I really have to specify the namespace in my forms and links?
(I am using Struts 2.3.20 if that matters.)
First of all: the tags referred in the docs are Struts2 tags (e.g. <s:url>, <s:a>).
And namespace aware means that if you already executed some action in particular namespace then in the JSP you don't need to prefix S2 links and forms with the current namespace.
E.g. if you have this package configuration:
<package name="testpkg" namespace="/test" extends="struts-default">
<action name="index">/index.jsp</action>
<action name="doTest" class="otes.test.TestAction">
<result>/success.jsp</result>
</action>
</package>
and executed the index action (http://localhost/app/test/index.action). Then in index.jsp you can write
<s:a action="doTest">test</s:a>
and that url will take you to the same namespace.
If you want to change namespace there is namespace attribute in some tags that you can use.
E.g. you are in some page (http://localhost/app/index.action) - note no namespace in url, then following link will execute doTest in /test namespace.
<s:a action="doTest" namespace="/test">test</s:a>
BTW don't add action extension to actions in S2 tags.
This is wrong:
<s:form action="doTest.action">
This is correct:
<s:form action="doTest">
The rule is simple: if you defined a package with the namespace attribute then its actions belong to this namespace. And when you use url or a, or form tags you should specify a namespace attribute along with action name which is in action attribute. If you specify both parameters Struts can easy map your URL to the action from the package with namespace. Don't use action extension in these attributes. Struts is using UrlHelper class to build the url and if it can't find the action mapping it will return the string as is. Also if you are using href attribute then Url helper is not involved, so the string remains as is.
This code should work:
<s:url namespace="/test" action="doTest" var="myAction" />
<h2><s:a href="%{#myAction}">doTest with href (s:url action)</s:a></h2>
<h2><s:a namespace="/test" action="doTest">doTest with namespace and action (s:url action)</s:a></h2>

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.

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

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.

Struts2 - have two packages in struts.xml point to dynamic web root

First off, please forgive me for my lack of understanding... I'm still learning :)
I have 2 packages in my struts.xml that extend a base package and I want to be able to access them by typing in my browser something similar to http://site.com/application/1/ThisAction.action and /application/2/ThisAction.action (examples).
I created two directories in my webapp folder, named '1' and '2' and I am able to navigate to both packages using the urls above. What I want to do is actually put my jsps within the jsp directory, instead of webapps. so instead of my two folders residing inside /webapp, they should reside in /webapp/jsp/.
I tried changing the namespace of the two packages to something like /1/jsp/ instead of simply '/1', but i get nothing. It just keeps telling me there is no action mapped to that action name.
Does anyone have any insight on how I can accomplish this? Google's not giving me a lot of help, but it may just be that I'm not searching for the right thing.
Here's a quick sample of what I'm referring to:
<struts>
<!-- Base-->
<package name="base" extends="struts-default" abstract = "true" namespace="/base">
<global-results>
<result name="cancel" type="redirectAction">CancelAction</result>
<result name="close">closewindow.jsp</result>
<result name="error">/jsp/wizard/GeneralError.jsp</result>
</global-results>
</package>
<package name="1" extends="base" namespace="/1">
In Struts 2 you do not need to put your JSP's in various folders according to the URL that you use to access them. Rather the package and the action comes together to create the URL and the result will determine the next view. So you start by writing your action:
public class MyActionClass ...{
...
public String actionMethod() {
//Your action code here
return SUCCESS;
}
}
Next you will create an entry in struts.xml that points to this action.
<package name="default" extends="struts-default">
<!--Interceptors, Global Results etc.-->
<action name="myaction" class="my.package.MyActionClass" method="actionMethod">
<result>/WEB-INF/path/to/yourpage.jsp</result>
</action>
...
</package>
Now, to access this action in the default package you simply use the URL: http://yourserver/myaction.action.
If you create a second package with a different name like this:
<package name="2" extends="default" namespace="/2" >
<action name="myaction" class="my.package.MyActionClass" method="actionMethod">
<result>/WEB-INF/path/to/yourpage.jsp</result>
</action>
...
</package>
Then you can access that action with the URL: http://yourserver/2/myaction.action.
So you can go ahead and put your JSP in the directory called jsp if you wish and you only have to modify the result to point to the correct place.

Categories