Implement remove suffix and url rewrite in struts2
Source: Internet
Author: User
First, this will take two packages, one is smarturls (Struts2 plugin, download address: http://code.google.com/p/smarturls-s2/downloads/list), There is urlrewritefilter (download address: http://tuckey.org/urlrewrite/dist/urlrewritefilter-2.6.zip).
To remove the struts2 suffix, configure the filter like this:
<filter> <filter-name>struts2</filter-name> <filter-class> Org.texturemedia.smarturls.smarturlsfilter</filter-class>
</filter> Use this instead of STRUTS2 's core filter, Filterdispatcher. You don't have to worry about struts2, because this filter is inherited from Filterdispatcher. At the same time add the struts.action.extension= in the struts.properties, without setting any value.
Then it's configuring Urlrewritefilter, as follows
<filter> <filter-name>UrlRewriteFilter</filter-name> <filter-class>org.tucke y.web.filters.urlrewrite.urlrewritefilter</filter-class> </filter> when configuring Filter-mapping, Note that the urlrewritefilter should be in front. Then Urlrewritefilter rewrite how to use you can refer to the document, here is no longer repeat.
When used to find the rewrite, Filterchain only a urlrewritefilter, and did not go through the smarturls to the requested resources, the natural is a 404 error. So I see Urlrewritefilter's source code found that if the rewrite URL succeeds, it will invoke the RequestDispatcher forward method to forward to the resource, The rewrite does not succeed or it is not overridden to invoke Filterchain.dofilter. So Struts2 's filter-mapping should be configured like this.
<filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> <dispatcher>FORWARD</dispatcher> </filter-mapping> then it's OK.
Finally, this should be in support of servlet2.4 above the specification of the container, because the servlet2.3 specification inside, filter only support request filter, do not support the filtering such as forwarding
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.