Recently the company's previous project needs to upgrade the revision, one of the modules is to use Struts2 do not need to change, but need to be removed from the previous project, see the Crossing network, find all the static address of the link access, thought is freemarker realization, Careful study will find that it is not so, the original is pseudo-static, the program used Urlrewriter. In fact, the use of Urlrewriter is very simple, see below you will understand.
Urlrewriter Introduction: URL rewrite to translate our dynamic Web address into static address, such as HTML, shtml, but also can hide the true path of the page, such as: sometimes need to xxx.com/news/type1/001.jsp Turns the display path to xxx.com/news_type1_001.jsp.
"First step" Download Jar package
In http://tuckey.org/urlrewrite/#download下载最新的jar文件, the latest is the Beat3.2 i downloaded is urlrewritefilter-2.6.zip. Unzip to get Urlrewrite-2.6.0.jar and Urlrewrite.xml
"Step Two" new Web project Testurlrewrite,
Create a new index.jsp under Webroot, and modify its code as follows:
Java code
- <%@ page contenttype= "text/html; Charset=utf-8 "%>
- <! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" >
- <title> Test testurlrewrite</title>
- <body>
- <%
- String id = (string) request.getparameter ("id");
- Out.println ("
- %>
- </body>
<%@ page contenttype= "text/html; Charset=utf-8 "%><! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" >
"Fourth Step" release program
Publish the program to Tomcat, and then enter the following address in your browser:
Http://localhost:8080/TestUrlRewrite/index.jsp?username=niejingyu, page display effect 1 shows:
"Fifth Step" add Urlrewrite File
Copy the Urlrewrite-2.6.0.jar to Webroot\web-inf\lib.
Copy the Urlrewrite.xml to Webroot\web-inf, where the Urlrewrite.xml code looks like this:
XML code
- <?xml version= "1.0" encoding= "Utf-8"? >
-
- <! DOCTYPE urlrewrite Public "-//tuckey.org//dtd urlrewrite 3.1//en" &NBSP;
-
- & nbsp; "Http://tuckey.org/res/dtds/urlrewrite3.1.dtd" >&NBSP;
-
- < urlrewrite>
-
- <rule>
-
- <from>/(. *) . html</from>
-
- <to>index.jsp?id=$1</to>
-
- </rule>
-
- </urlrewrite>
<?xml version= "1.0" encoding= "Utf-8"? ><! DOCTYPE urlrewrite Public "-//tuckey.org//dtd urlrewrite 3.1//en" "Http://tuckey.org/res/dtds/urlrewrite3.1.dtd "><urlrewrite> <rule> <from>/(. *) .html</from> <to>index.jsp?id=$1 </to> </rule></urlrewrite>
*.html is a string of regular expressions. If you don't understand this, you can learn about the application of the Java Express expression.
$ $ is also a group capture in a regular expression's language-matching regular.
"Sixth step" modifying the Web. xml file
Add the following to Web. xml:
XML code
- <filter>
- <filter-name>UrlRewriteFilter</filter-name>
- <filter-class>
- Org.tuckey.web.filters.urlrewrite.UrlRewriteFilter
- </filter-class>
- <init-param>
- <param-name>logLevel</param-name>
- <param-value>DEBUG</param-value>
- </init-param>
- </filter>
- <filter-mapping>
- <filter-name>UrlRewriteFilter</filter-name>
- <url-pattern>/*</url-pattern>
- </filter-mapping>
<filter> <filter-name>UrlRewriteFilter</filter-name> <filter-class> Org.tuckey.web.filters.urlrewrite.UrlRewriteFilter </filter-class> <init-param> < param-name>loglevel</param-name> <param-value>DEBUG</param-value> </init-param ></filter><filter-mapping> <filter-name>UrlRewriteFilter</filter-name> < Url-pattern>/*</url-pattern></filter-mapping>
"Seventh Step" reboot Tomcat
Access http://localhost:8080/TestUrlRewrite/2.html, page effects:
Here is the match/(. *). HTML, which forwards the request to index.jsp?id=$1. You can also go to xxx.action?id=$1 as needed
Jixian Farmhouse Network Chengdu Farm House Network Chongqing Farm Network Beijing Farmhouse Network Chang-zhu-Tan farm stay Net Love Farm network
Urlrewrite in Java