Struts action result redirecting to wrong URL - struts2

Our application is hosted under a folder in a domain. When struts action result returns a JSP page, it is removing the that folder. So the JSP page is not loading
Our App is hosted in domain.com/vendor
Here vendor is a folder under the domain
When action result return a JSP page, URL is
domain.com/project1/private/attendance.jsp
Here "vendor" folder is missing
<package name="private" namespace="/private" extends="struts-default">
<action name="attendanceRegister" class="com.emob.web.PunchAction"
method="listAttendance">
<result name="success">/private/attendance.jsp</result>
</action>
</package>
Please let me know the configuration so that the folder is returned in result URL

Related

Hide directory from URL

I am new in Struts. When I create my first Struts project and run it then URL of the project is the following:
10.1.21.85:8080/shravan/aboutus/about.jsp
But here in URL all paths of JSP pages display i.e my about.jsp page is inside shravan/aboutus folder so how I hide this directory structure in the URL?
First you should keep your JSPs private by putting them inside WEB-INF directory.
Then you have to map an action in struts.xml to show the desired jsp.
<struts>
<package name="default-package" extends="struts-default">
<action name="about-us">
<result>/WEB-INF/jsp/aboutus/about.jsp</result>
</action>
</package>
</struts>
And now your url should look something like this:
http://10.1.21.85:8080/shravan/about-us.action
You have to check in web.xml how is your struts configured to map the urls.
It could be:
/*.action, /*.do or simply /*

struts 2 and sitemesh localization

i am using struts 2 and sitemesh. I can change locale in my login page without problem just by setting a request parameter named with 'request_locale' parameter. But in all other pages i am using sitemesh for decorating header and footer. And i tried to set same parameter for other pages but no luck. These are some of the other codes that i tried in my Login Action:
request.getSession(false).setAttribute("WW_TRANS_I18N_LOCALE", newLocale);
ActionContext.getContext().setLocale(newLocale);
session.put(I18nInterceptor.DEFAULT_SESSION_ATTRIBUTE, newLocale);
When i write out these settings i see everything is ok.
But when i write out
<c:out value="${pageContext.request.locale.language}"/>
it is always showing browsers Accept-Language and it doesnt make any localization.
How can i make localization to work with sitemesh?
I tried to redirect from login action to an action named home with request locale parameter and then this action redirects to index.jsp. Everything is ok now.
And i set default action as home action so when user enters root page from address bar it is activing home action so i18n interceptor and intercoptors are becaming active.
This is login.action in struts.xml:
<action name="login" class="wateron.login.LoginAction" >
<result type="redirectAction">
<param name="actionName">home?request_locale=${request_locale}</param>
<param name="namespace">/</param>
</result>
</action>
And this is home action in struts.xml :
<package name="default" extends="struts-default" namespace="">
<default-action-ref name="home" />
<action name="home">
<result>/index.jsp</result>
</action>
</package>
Thanks for reference question :
How to use interceptor on the default page?

url links in struts2 tiles gets namespace added on second time after click. How do I stop this?

I am using struts 2, tiles, and namespaces and am having a problem that after I click a link the menu appends the namespace a second time. I am using the struts tag url with an action and a namespace. When the page is first displayed, the links in the menu are correct land have the namespace before the action. After I click on one of the links, the page redisplays and although the source looks okay, when I hover over it firefox shows the url with the namespace in there twice. I.E.
localhost/ar/customermaint becomes localhost/ar/ar/customermaint. If I have clicked on a link that has a different namespace such as "ss" first then it becomes localhost/ss/ar/customermaint.
any idea what I'm doing wrong? Or is it a problem with tiles and namespaces.
I had faced this issue before. Each time when the action is called, the namespace gets appended in the URL and it looked ugly
Example: http://localhost:8080/RmAirlines/user/user/user/user/user/login for calling the action login from a namespace user each time
Setting the namespace to "/" isn't an ideal solution either.
Solution: Suppose the namespace of the package is "/user" and there's an action named "login" inside it (in your struts.xml). Then where ever you call the action, refer it as action="/user/login" (or action="/user/login.action") - i.e. putting a "/" before the namespace will save your day!
Example:
struts.xml
<package name="userManagement" namespace="/user" extends="struts-default">
<action name="login" class="com.rm.airlines.action.Login">
<result name="success">/user.jsp</result>
<result name="login">/login.jsp</result>
</action>
</package>
login.jsp
<s:form action="/user/login">
<s:textfield label="Email" key="email" />
<s:password label="Password" key="password" />
<s:submit />
</s:form>
To avoid this, always follow a simple principle.
For all namespace declaration, use a slash "/" followed by the namespace name.
In this way, it will not append the earlier namespace in new url.
What struts assume, if you don't specify "/" in new namespace, it tries to find it in currently used namespace. If you specify "/" before namespace name, it takes it as a root namespace.
Example:-
<package name="default" namespace="/nsp" extends="struts-default">
<action name="validate" class="login.LoginAction">
<result name="success">/home.jsp</result>
<result name="login">/login.jsp</result>
</action>
</package>
<package name="cricket" namespace="/cricket" extends="struts-default">
<action name="findcaptain" class="cricket.CaptainAction">
<result>/showCaptain.jsp</result>
</action>
</package>
Look in each of my package declaration, I am using namespace name prefixed by a slash. i.e. /nsp and /cricket). And that's how we could avoid this namespace appending issue.

no effect of "resultpath" constant in struts.xml

I have all the jsp pages (ex: index.jsp) in the WebContent folder and my application is working smoothly.
Now, I created a folder "pages" inside WebContent, where I transferred all my jsp pages.
And I added the following line inside the <struts> tags:
<constant name="struts.convention.result.path" value="/pages" />
But still, its not searching for jsp inside the "pages" folder.
I am getting:
HTTP Status 404 - /MyApplicationName/index.jsp
Where am I doing a mistake?
EDIT: The current structure of my application is as follows:
The welcome page is an action instead of a jsp page. The result page of this action is index.jsp. This action populates the dropdown menu and some other fields in index.jsp. Now, I want to keep index.jsp along with other jsp pages in the folder "WebContent/pages". I have over 30 jsp pages in my application, and I don't want to mention the full path of every jsp in the result-tag. Thats why I want to make use of the struts2 constant "struts.convention.result.path"
My question is, why the code I have mentioned above is not working?
Accessing index.jsp directly bypasses all S2 request handling.
There's no S2 or S2 Convention plugin involved, you're just accessing a JSP page.
If index.jsp is first page of your application then you need to change its path to /pages/index.jsp in <welcome-file> tag of web.xml. And for rest I agree with #Dave Newton :)
Probably your mistake was in struts.xml.
When you create a new-Folder under web-content make with the same name (folder-name) of
xml file under src and include it in struts.xml file it will work fine.
For eg: suppose you created example folder under web-content, now create example.xml file
under src folder and in struts.xml file include example.xml
example.xml:
<struts>
<package name="example" namespace="/example" extends="default">
<action name="your action name" class="your class" method="your method">
</action>
</package>
</struts>
struts.xml:
<struts>
<constant name="struts.enable.DynamicMethodInvocation" value="false" />
<constant name="struts.devMode" value="false" />
<action name="" class="" method="">
</action>
</package>
<include file="example.xml"/>

In Struts2 HOWTO handle a url with same namespace but with or without the trailing slash similarly

In struts2 I am writing an app where I need to make sure that the url redirection works the same whether or not there is a trailing slash at the end.
E.g. example.com/app should behave same way as if user entered example.com/app/. Currently I changed mapping in struts.xml like so -
<struts>
<package name="default" namespace="/" extends="secure">
<interceptors> ... <interceptors>
<action name="app">
<result type="redirectAction">/app/</result>
</action>
</package>
</struts>
and
<struts>
<package name="app" namespace="/app" extends="secure">
<interceptors> ... <interceptors>
<action name="" class="com.example.action.app.LoginAction" method="display">
<interceptor-ref name="store">
<param name="operationMode">RETRIEVE</param>
</interceptor-ref>
<interceptor-ref name="basic" />
<result type="redirectAction">home</result>
<result name="display">/jsp/base/content/login/loginForm.jsp</result>
</action>
</package>
</struts>
But this seems hackish since if I go to example.com/app it will show example.com//app/.html in the URL.
Any help appreciated.
Answer was derived in comments under the answer.
Quaternion:
Personally I would write all my urls with out the trailing slash...
and then I would use something external to the application to rewrite
urls as appropriate, perhaps iptables could determine if there is a
trailing slash and if so always strip it.
Mohana Rao SV:
As suggested above follow without tailing slash. And override
StrutsPrepareAndExecuteFilter one of the filter job is from the url it
has to identify the namespace and action name and invoke respective
action. So here remove tailing slash from url.
Quaternion:
In namespace "/" you have an action called app. That is all there is
to it to invoke CONTEXT_ROOT/app (that is what struts2 expects), you
don't ever expect to see a "/" on the end of the url, so you want to
find a method that parses the url before struts2 resolves the mapping.
What you have described only requires something to remove a trailing
"/" if it exists. I'd look to iptables because I've used it before or
some other url rewriter... Mahana would keep it all part of the web
app and use a filter, methods differ but the effect is the same.

Resources