Hide the .jsp url extension in struts2 project - struts2

I am working in the struts2 web application.I want to do the mapping of my url such that the extensions like ".jsp" should be eliminated from the url.
So below is the snippet code of my web.xml.I want to do that my url show /login instead of /login.jsp.
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<filter>
<filter-name>URLFilter</filter-name>
<filter-class>example.MyFilter</filter-class>
<init-param>
<param-name>onError</param-name>
<param-value>/login.jsp</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>URLFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

Just access all your JSPs through Actions (and put them somewhere below WEB-INF to enforce this policy). It's easy to do with the "default action" of Struts2:
<action name="login">
<result>/WEB-INF/pages/login.jsp</result>
</action>
I don't know how well this integrates with ServletFilter, maybe you'll have to turn the one mentioned in your web.xml into a Struts2 Interceptor.

You can change url extension using property file as shown in below link :
http://www.aoiblog.com/change-url-extension-in-struts2/

Related

Primefaces 4, Omnifaces 1.7 JSF handle session timeout fail

faces-config.xml
<factory>
<exception-handler-factory>org.omnifaces.exceptionhandler.FullAjaxExceptionHandlerFactory</exception-handler-factory>
</factory>
web.xml
<error-page>
<exception-type>javax.faces.application.ViewExpiredException</exception-type>
<location>/Login/login.xhtml</location>
</error-page>
<session-config>
<session-timeout>1</session-timeout>
</session-config>
<filter>
<filter-name>facesExceptionFilter</filter-name>
<filter-class>org.omnifaces.filter.FacesExceptionFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>facesExceptionFilter</filter-name>
<servlet-name>Faces Servlet</servlet-name>
</filter-mapping>
After a session timeout i want to be redirectet to the login page. I use Form based authentication. When i login wait one minute and then hit a button i often get the following page:
error page:
<partial-response id="j_id1">
<redirect url="/CuViS/Manager/show.xhtml"/>
<changes>
<update id="j_id1:javax.faces.ViewState:0">-2290296337409893276:-7107311277476366039</update>
</changes>
</partial-response>
Where the url is the current page where i clicked the button.
When i remove the omnifaces configuration the same behavior appears. What I am doing wrong?

Tuckey UrlRewriteFilter and Struts2 gives 404 on first request

About 30 of my rewritten links work fine but I have a couple that don't work the first time they are called but they do work the second time!
This is the web.xml snippets:
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
<init-param>
<param-name>actionPackages</param-name>
<param-value>uk.co.prodia.prosoc.struts2.action</param-value>
</init-param>
</filter>
<filter>
<filter-name>UrlRewriteFilter</filter-name>
<filter-class>org.tuckey.web.filters.urlrewrite.UrlRewriteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>UrlRewriteFilter</filter-name>
<url-pattern>/*</url-pattern>
<dispatcher>REQUEST</dispatcher>
<dispatcher>FORWARD</dispatcher>
</filter-mapping>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
<dispatcher>REQUEST</dispatcher>
<dispatcher>FORWARD</dispatcher>
</filter-mapping>
and here is the urlrewrite.xml that gives a 404 the first time it is called:
<rule>
<from>^/forgotten-password$</from>
<to>/unsecured/forgotten-password!input.action</to>
</rule>
and the struts2 config for that action:
<package name="unsecured" extends="struts-default" namespace="/unsecured">
<action name="forgotten-password" class="actionForgottenPassword">
<result name="input">/WEB-INF/view/unsecured/forgotten-password.jsp</result>
<result name="passwordNotFound">/WEB-INF/view/unsecured/forgotten-password.jsp</result>
</action>
</package>
However if I make the urlrewrite.xml pass through the unsecured package which contains the forgotten-password action then it works:
<rule>
<from>^/unsecured/forgotten-password$</from><!--works through /unsecured -->
<to>/unsecured/forgotten-password!input.action</to>
</rule>
It seems that missing of the Struts2 package name from the urlrewrite causes the 404 the first time the URI is used.
Does this make any sense to anyone or shall I just stay in crazy town?
The problem seems to be caused by the jsessionid in the URL on the first page load of the context. I am a bit useless with regexp but this seems to work:
<rule>
<from>^/forgotten-password(;jsessionid=.*)?$</from>
<to>/unsecured/forgotten-password!input.action</to>
</rule>
I added the (;jsessionid=.*)? to the end of each of the rules and it seems to work.

org.apache.struts2.dispatcher.filterdispatcher not allowing other filters in the chain to get triggered?

I am using struts 2. In my web.xml I am having the following configuration,
<filter>
<description>filter for application</description>
<display-name>AppBasicFilter</display-name>
<filter-name>AppBasicFilter</filter-name>
<filter-class>com.myapp.AppBasicFilter</filter-class>
</filter>
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
</filter>
<filter-mapping>
<filter-name>AppBasicFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
For this configuration, I am getting my action classes triggerd properly. But Neither before nor after action class execution, my AppBasicFilter is not getting triggerd.
I tried to decompile and see the struts 2 FilterDispatcher coding, I saw that if mapping is present, its not doing FilterChain.doFilter() in it.
This problem is because of that. can someone please help me?
I came to know that its a problem with FilterDispatcher. So I have to work with StritsPrepareAndExecuteFilter which is available in struts 2.1 or greater. But unfortunately my project was using struts 2.0. So I migrated to struts 2.1 and made it work.

How to extend an XML File using ant xmltask by copying tags from a source-file to the dest-file in specific positions

Im trying to create a special web.xml for local development.
I have some tags stored in a separate xml-file which need to be selected and pasted to specific positions inside the final web.xml.
To achieve this I am using the copy and the paste action in the following manner:
<xmltask source="templates.xml">
<copy path="//data/init-param" buffer="initParamBuffer"/>
</xmltask>
<xmltask source="web.xml" dest="web.xml">
<paste path="//web-app/filter[contains(./filter-name,'MyFilter')]
/init-param[contains(./param-name,
'MyInitParameter')]"
position="after" buffer="initParamBuffer"/>
</xmltask>
My intention is to gather ALL init-param Tags from the source File and paste them after the Tag selected in the paste operation.
Also the Part where Im selecting a Tag which contains a tag with a specified content using the contains() function is not working smoothly either.
Maybe there is a better way to form this xpath expression...
update:
As I have written before, I do not know the best approach to this problem. I have read about the possibility to transform using stylesheets, but since the ant-xmltask promised to be a more sleak sollution I have tried this first.
As far as I have come with this approach, it is possible to insert/write tags into the web.xml using this approach. I have succeeded in inserting single init-param tags at locations that where sligtly off, with a less complex expression:
<paste path="//web-app/filter[1]/init-param"
position="after" buffer="initParamBuffer"/>
So my Problem was:
A: I want to select more than one tag into the buffer
B: I want to insert the content of that buffer after a tag specified by a name (not index).
Here is an example for the sources (templates.xml) to insert into the web.xml:
<data>
<init-param>
<param-name>newparam1</param-name>
<param-value>1</param-value>
</init-param>
<init-param>
<param-name>newparam2</param-name>
<param-value>2</param-value>
</init-param>
</data>
Here is part of an web.xml where the above section is to be pasted:
<web-app>
<filter-mapping>
<filter-name>SomeFilter</filter-name>
<url-pattern>/somePath/*</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>MyFilter</filter-name>
<url-pattern>/myPath/*</url-pattern>
</filter-mapping>
<filter>
<filter-name>SomeFilter</filter-name>
<filter-class>foo.bar.SomeFilter</filter-class>
<init-param>
<param-name>SomeInitParameter</param-name>
<param-value>4711</param-value>
</init-param>
</filter>
<filter>
<filter-name>MyFilter</filter-name>
<filter-class>foo.bar.MyFilter</filter-class>
<init-param>
<param-name>MyInitParameter</param-name>
<param-value>0815</param-value>
</init-param>
</filter>
</web-app>
And here is the result I would hope to achieve:
<web-app>
<filter-mapping>
<filter-name>SomeFilter</filter-name>
<url-pattern>/somePath/*</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>MyFilter</filter-name>
<url-pattern>/myPath/*</url-pattern>
</filter-mapping>
<filter>
<filter-name>SomeFilter</filter-name>
<filter-class>foo.bar.SomeFilter</filter-class>
<init-param>
<param-name>SomeInitParameter</param-name>
<param-value>4711</param-value>
</init-param>
</filter>
<filter>
<filter-name>MyFilter</filter-name>
<filter-class>foo.bar.MyFilter</filter-class>
<init-param>
<param-name>MyInitParameter</param-name>
<param-value>0815</param-value>
</init-param>
<init-param>
<param-name>newparam1</param-name>
<param-value>1</param-value>
</init-param>
<init-param>
<param-name>newparam2</param-name>
<param-value>2</param-value>
</init-param>
</filter>
</web-app>
Use XPath Or:
Path|Path
From:
<paste path="//web-app/filter[1]/init-param" position="after" buffer="initParamBuffer"/>
To:
<paste path="//web-app/filter[1]/init-param|//web-app/filter[2]/init-param" position="after" buffer="initParamBuffer"/>
XPath is a Query language operating on the XML Infoset of XML documents. It can only return results or select nodes, but it cannot change the structure of the source XML document or modify it in any way.
A suitable language, designed especially for processing XML documentsand producing new XML documents, is XSLT.
The task specified in your question can be done in a very easy way using XSLT.
Please, provide a complete (but minimal) source XML document, the complete wanted result and specify any rules that the transformation should follow. Then many people will be able to give you correct and useful solutions.

Stripes: all URLs resolved through StripesDispatcher and forwarded to pre-compiled JSPs

Is it possible to have the StripesDispatcher be the sole determiner of webserver urls by looking at the #UrlBinding annotations on action beans AND also having those action beans forward to pre-compiled JSPs / servlets WITHOUT needing to define and maintain <servlet> <servlet-mapping> pairs in web.xml? Basically, I just want to have to only maintain the #UrlBinding annotations as the sole determiners of available webapp paths.
Perhaps there is a way to point Jasper to where my servlets are and load them all up automatically without having to explicitly define each and every one?
The particular way in which this is achieved is not important, only that I leave the land of explicit servlet web.xml dependencies.
Maybe I don't understand your question, but I'll give it a go. AFAIK the only mapping you need in a Stripes app's web.xml to use #URLBinding as the 'source of truth' for URLs in your web-app:
<filter>
<filter-name>StripesFilter</filter-name>
<filter-class>net.sourceforge.stripes.controller.StripesFilter</filter-class>
<init-param>
<param-name>ActionResolver.Packages</param-name>
<param-value>com.your.action.beans.package</param-value>
</init-param>
<init-param>
<param-name>Extension.Packages</param-name>
<param-value>com.your.extension.packages</param-value>
</param-value>
</init-param>
</filter>
...
<servlet>
<servlet-name>DispatcherServlet</servlet-name>
<servlet-class>net.sourceforge.stripes.controller.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
...
<filter-mapping>
<filter-name>StripesFilter</filter-name>
<servlet-name>DispatcherServlet</servlet-name>
<dispatcher>REQUEST</dispatcher>
<dispatcher>FORWARD</dispatcher>
</filter-mapping>
<servlet-mapping>
<servlet-name>DispatcherServlet</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
With this, there is no need to change anything in web.xml when you add/remove action beans and/or JSPs.

Resources