Configuring Urlrewrite basic Configuration in Maven

Source: Internet
Author: User

1. In the MAVEN project's Pom.xml file, add:

<!--URL Rewrite--
<dependency>
<groupId>org.tuckey</groupId>
<artifactId>urlrewritefilter</artifactId>
<version>3.1.0</version>
</dependency>


3. Add a urlrewrite filter to Web. xml
<filter>
<filter-name>UrlRewriteFilter</filter-name>
<filter-class>org.tuckey.web.filters.urlrewrite.UrlRewriteFilter</filter-class>
<init-param>

<!--set Automatic Update urlrewrite.xml information-
<param-name>confReloadCheckInterval</param-name>
<param-value>60</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>UrlRewriteFilter</filter-name>
<url-pattern>/*</url-pattern>
<dispatcher>REQUEST</dispatcher>
<dispatcher>FORWARD</dispatcher>
<dispatcher>INCLUDE</dispatcher>
</filter-mapping>


2. Create a new file named Urlrewrite.xml in the Web-inf directory under the Web project, with the following label format:
 

<?XML version= "1.0" encoding= "Utf-8"?><!DOCTYPE urlrewrite Public "-//tuckey.org//dtd urlrewrite 3.2//en" "\\urlrewrite3.2.dtd ">  <Urlrewrite>    <Rule>    <Note>Rewrite index.jsp into index.html</Note>    <Note>Example:/index.html</Note>    < from>/index.html</ from>    < totype= "Forward">/index.jsp</ to>  </Rule>    <Rule>    <Note>Rewrite all JSP resources in the view root directory to/xxx.action</Note>    <Note>Example:/index.action</Note>    < from>/([a-za-z0-9]+). Action</ from>    < totype= "Forward">/view/$1.jsp</ to>  </Rule>    <Rule>    <Note>Forward (forwarding mode) parameter transfer</Note>    <Note>Example:/user/param/fancy/8080.do</Note>    < from>/user/([a-za-z0-9]+)/([a-za-z0-9]+)/([a-za-z0-9]+]. Do</ from>    < totype= "Forward">/view/parameter/$1.jsp?username=$2&amp;Password=$3</ to>  </Rule>    <Rule>    <Note>Redirect (redirect mode), to write absolute address</Note>    <Note>Example:/admin/param/fancy/8080.do</Note>    < from>/admin/([a-za-z0-9]+)/([a-za-z0-9]+)/([a-za-z0-9]+]. Do</ from>    < totype= "Redirect">/urlrewrite-maven-example/view/parameter/$1.jsp?username=$2&amp;Password=$3</ to>  </Rule>  </Urlrewrite>


Urlrewrite.xml This configuration file must only be placed under the web-inf, put somewhere else, no matter how you configure in Web. XML is not possible, at least not now.

The so-called rewrite, there will be a regular match, here to use the regular expression, for convenience, I only use [a-za-z0-9]+, simple explanation:

A-Z: Match any character in a to Z, say a piece of crap, the same thing,

A-Z: matches any one character in a to Z

0-9: Match any one character from 0 to 9

+: Appears at least once, i.e., once or more

Together [a-za-z0-9]+ means: match an arbitrary combination of alphanumeric characters and numbers.

<rule>: Custom matching rules

<note>: Comment, explanation label

<from>: Define a specific matching rule

<to>: Target address after successful match

<to type= "" >:type value has two, one is forward (forwarding, parameter is not lost), one is redirect (redirect, address bar display address is the destination real address)

$: Match the value of the string in the first regular expression, $2,$3,$4 .... And so it is.

&amp; : Is the entity name of &, which represents & This can be found in W3school: http://www.w3school.com.cn/tags/html_ref_entities.html

The first rule: access to/index.html, which is actually a visit to/index.jsp

The second rule: access to the/xx.action, which actually accesses the xx.jsp in the view directory

The third rule: access to/user/xx/yy/zz.do is actually accessed under the parameter directory under the view directory Xx.jsp?username=yy&password=zz

The fourth rule: it is the same as the third one, but because of the redirect mode, the Address bar will show the real address.

Right-click in Pom.xml, Run as--and Maven install

The following is the generated target resource:

Copy the war file to your server, I use Tomcat, and then start Tomcat:

If you are using Urlrewrite for the first time, you may throw an exception:

This exception message is that the system cannot find the Urlrewrite3.2.dtd file under the Tomcat bin, and the workaround is simple:

Method One: Place the URLREWRITE3.2.DTD in the directory where the Urlrewrite.xml resides, that is, the Web-info directory

Method Two: Put the urlrewrite3.2.dtd into the Tomcat bin directory, this method once and for all, so I chose this

Urlrewrite3.2.dtd This file can be downloaded at the bottom of the article, the source package I did not put this file, no problem,

Configuring Urlrewrite basic Configuration in Maven

Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.