Im developing a web application(J2EE,Struts2,JSP,Tomcat) I want to hide the .jsp extension from webpages.
here is some piece of my web.xml:
<filter>
<filter-name>STSDispatcher</filter-name>
<filter-class>
org.apache.struts2.dispatcher.FilterDispatcher
</filter-class>
</filter>
<filter-mapping>
<filter-name>STSDispatcher</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<welcome-file-list>
<welcome-file>Login.jsp</welcome-file>
</welcome-file-list>
I Googled around and found this solution:
<servlet>
<servlet-name>myFoo</servlet-name>
<jsp-file>myJSPfile.jsp</jsp-file>
</servlet>
<servlet-mapping>
<servlet-name>myFoo</servlet-name>
<url-pattern>/main</url-pattern>
</servlet-mapping>
I tried this like below, but didn't work:
for example I have "alert.jsp" and this is my web.xml:
<servlet>
<servlet-name>alert</servlet-name>
<jsp-file>/alert.jsp</jsp-file>
</servlet>
<servlet-mapping>
<servlet-name>alert</servlet-name>
<url-pattern>/alert</url-pattern>
</servlet-mapping>
but I got this error: "There is no Action mapped for namespace / and action name alert. - [unknown location]"
What should I do?
Thanks in advance.
You can put the directory structure with the JSPs in WEB-INF, then use a servlet to forward to the JSPs according to the requested URL.
The servlet will be mapped to a wildcard pattern, like:
<servlet-mapping>
<servlet-name>ForwardToJspServlet</servlet-name>
<url-pattern>*.page</url-pattern>
</servlet-mapping>
or
<servlet-mapping>
<servlet-name>ForwardToJspServlet</servlet-name>
<url-pattern>/pages/*</url-pattern>
</servlet-mapping>
and the logic of the servlet will be converting the requested URL to the path of the JSP and then forwarding.
For example, the user requests the URL /yourApp/documents/documentAdd.page and you forward to the JSP under /WEB-INF/JSP/documents/documentAdd.jsp.
I think you get the error because your struts2 filter intercept all the request coming to the server and tries to find a mapping in your actions but since /alert
actually points to a servlet you see the error.
A solution would be to have the mapping in your struts config file like:
<action name="alert">
<result>/alert.jsp</result>
</action>
This way when you request /alert
you get the page with no extension (name of your action).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With