URLRewrite就是我们通常说的地址重写,用户得到的全部都是经过处理后的URL地址。其优点有:
(1)提高安全性,可以有效的避免一些参数名、ID等完全暴露在用户面前,如果用户随便乱输的话,不符合规则的话直接会返回个404或错误页面,这比直接返回500或一大堆服务器错误信息要好的多。
(2)美化URL,去除了那些比如*.do之类的后缀名、长长的参数串等,可以自己组织精简更能反映访问模块内容的URL。
(3)更有利于搜索引擎的收入,通过对URL的一些优化,可以使搜索引擎更好的识别与收录网站的信息。
实现过程:
1 参考http://blog.csdn.net/haishu_zheng/article/details/51490299,用第二种方法创建一个名为urlrewrite_demo的Maven工程。
2 文件目录结构如下图所示
3 在pom.xml中添加四个jar包:
spring-webmvc
spring-context-support
urlrewritefilter
freemarker
添加完之后的完整内容为
- <project xmlns=”http://maven.apache.org/POM/4.0.0”xmlns:xsi=“http://www.w3.org/2001/XMLSchema-instance”
- xsi:schemaLocation=“http://maven.apache.org/POM/4.0.0http://maven.apache.org/xsd/maven-4.0.0.xsd”>
- <modelVersion>4.0.0</modelVersion>
- <groupId>urlrewrite_demo</groupId>
- <artifactId>urlrewrite_demo</artifactId>
- <version>0.0.1-SNAPSHOT</version>
- <packaging>war</packaging>
- <name>urlrewrite_demo</name>
- <description />
- <properties>
- <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
- </properties>
- <dependencies>
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-webmvc</artifactId>
- <version>3.2.9.RELEASE</version>
- </dependency>
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-context-support</artifactId>
- <version>3.2.9.RELEASE</version>
- </dependency>
- <dependency>
- <groupId>org.tuckey</groupId>
- <artifactId>urlrewritefilter</artifactId>
- <version>4.0.3</version>
- </dependency>
- <dependency>
- <groupId>org.freemarker</groupId>
- <artifactId>freemarker</artifactId>
- <version>2.3.23</version>
- </dependency>
- <dependency>
- <groupId>javax</groupId>
- <artifactId>javaee-api</artifactId>
- <version>7.0</version>
- <scope>provided</scope>
- </dependency>
- </dependencies>
- <build>
- <plugins>
- <plugin>
- <artifactId>maven-compiler-plugin</artifactId>
- <version>2.3.2</version>
- <configuration>
- <source>1.7</source>
- <target>1.7</target>
- </configuration>
- </plugin>
- <plugin>
- <artifactId>maven-war-plugin</artifactId>
- <version>2.2</version>
- <configuration>
- <version>3.1</version>
- <failOnMissingWebXml>false</failOnMissingWebXml>
- </configuration>
- </plugin>
- </plugins>
- </build>
- </project>
<project xmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>urlrewrite_demo</groupId>
<artifactId>urlrewrite_demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>urlrewrite_demo</name>
<description />
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>3.2.9.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>3.2.9.RELEASE</version>
</dependency>
<dependency>
<groupId>org.tuckey</groupId>
<artifactId>urlrewritefilter</artifactId>
<version>4.0.3</version>
</dependency>
<dependency>
<groupId>org.freemarker</groupId>
<artifactId>freemarker</artifactId>
<version>2.3.23</version>
</dependency>
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-api</artifactId>
<version>7.0</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>2.2</version>
<configuration>
<version>3.1</version>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
</plugins>
</build>
</project>
4 web.xml中的完整内容为
- <?xml version=”1.0”encoding=”UTF-8”?>
- <web-app xmlns:xsi=“http://www.w3.org/2001/XMLSchema-instance”
- xmlns=“http://xmlns.jcp.org/xml/ns/javaee”
- xsi:schemaLocation=“http://xmlns.jcp.org/xml/ns/javaeehttp://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd”
- id=“WebApp_ID” version=“3.1”>
- <display-name>urlrewrite_demo</display-name>
- <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>
- <servlet>
- <servlet-name>dispatcher</servlet-name>
- <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
- <init-param>
- <param-name>contextConfigLocation</param-name>
- <param-value>/WEB-INF/classes/config/spring/applicationContext-mvc.xml</param-value>
- </init-param>
- <load-on-startup>1</load-on-startup>
- </servlet>
- <servlet-mapping>
- <servlet-name>dispatcher</servlet-name>
- <url-pattern>/service/*</url-pattern>
- </servlet-mapping>
- </web-app>
<?xml version="1.0"encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaeehttp://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
id="WebApp_ID" version="3.1">
<display-name>urlrewrite_demo</display-name>
<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>
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/classes/config/spring/applicationContext-mvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/service/*</url-pattern>
</servlet-mapping>
</web-app>
5 /src/main/reources/config/spring/applicationContext-mvc.xml中的完整内容为
- <?xml version=“1.0” encoding=“UTF-8”?>
- <beans xmlns=“http://www.springframework.org/schema/beans”
- xmlns:xsi=“http://www.w3.org/2001/XMLSchema-instance”
- xmlns:context=“http://www.springframework.org/schema/context”
- xmlns:aop=“http://www.springframework.org/schema/aop”
- xmlns:p=“http://www.springframework.org/schema/p”
- xmlns:tx=“http://www.springframework.org/schema/tx”
- xmlns:mvc=“http://www.springframework.org/schema/mvc”
- xsi:schemaLocation=”http://www.springframework.org/schema/beans
- http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
- http://www.springframework.org/schema/context
- http://www.springframework.org/schema/context/spring-context-3.2.xsd
- http://www.springframework.org/schema/aop
- http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
- http://www.springframework.org/schema/tx
- http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
- http://www.springframework.org/schema/mvc
- http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd”>
- <!– 有 @Controller注解的表现层控制器组件 –>
- <context:component-scan base-package=“com.zheng.controller” />
- <!– 配置freeMarker模板加载地址 –>
- <bean id=“freemarkerConfig” class=“org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer”>
- <!– 视图解析器在/WEB-INF/views/路径下扫描视图文件 –>
- <property name=“templateLoaderPath” value=“/WEB-INF/views/” />
- <property name=“freemarkerSettings” ref=“freemarkerConfiguration” />
- </bean>
- <!– 配置Freemarker属性文件路径 –>
- <bean id=“freemarkerConfiguration” class=“org.springframework.beans.factory.config.PropertiesFactoryBean”>
- <property name=“location” value=“classpath:config/properties/freemarker.properties”/>
- </bean>
- <!– 配置freeMarker视图解析器 –>
- <bean id=“freemakerViewResolver” class=“org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver”>
- <property name=“viewClass” value=“org.springframework.web.servlet.view.freemarker.FreeMarkerView”/>
- <property name=“suffix” value=“.ftl” />
- <property name=“contentType” value=“text/html; charset=UTF-8” />
- </bean>
- </beans>
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">
<!-- 有 @Controller注解的表现层控制器组件 -->
<context:component-scan base-package="com.zheng.controller" />
<!-- 配置freeMarker模板加载地址 -->
<bean id="freemarkerConfig" class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
<!-- 视图解析器在/WEB-INF/views/路径下扫描视图文件 -->
<property name="templateLoaderPath" value="/WEB-INF/views/" />
<property name="freemarkerSettings" ref="freemarkerConfiguration" />
</bean>
<!-- 配置Freemarker属性文件路径 -->
<bean id="freemarkerConfiguration" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
<property name="location" value="classpath:config/properties/freemarker.properties"/>
</bean>
<!-- 配置freeMarker视图解析器 -->
<bean id="freemakerViewResolver" class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.freemarker.FreeMarkerView"/>
<property name="suffix" value=".ftl" />
<property name="contentType" value="text/html; charset=UTF-8" />
</bean>
</beans>
6 /src/main/resources/config/properties/freemarker.properties中的完整内容为
- tag_syntax=auto_detect
- classic_compatible=true
- whitespace_stripping=true
- template_update_delay=2
- default_encoding=UTF-8
- output_encoding=UTF-8
- url_escaping_charset=UTF-8
- locale=zh_CN
- date_format=yyyy-MM-dd
- time_format=HH:mm:ss
- datetime_format=yyyy-MM-dd HH\:mm\:ss
tag_syntax=auto_detect
classic_compatible=true
whitespace_stripping=true
template_update_delay=2
default_encoding=UTF-8
output_encoding=UTF-8
url_escaping_charset=UTF-8
locale=zh_CN
date_format=yyyy-MM-dd
time_format=HH:mm:ss
datetime_format=yyyy-MM-dd HH\:mm\:ss
7 /src/main/Java/com/zheng/controller/TestController.java中的完整代码为
- package com.zheng.controller;
- import org.springframework.stereotype.Controller;
- import org.springframework.web.bind.annotation.RequestMapping;
- @Controller
- @RequestMapping(“/login”)
- public class TestController {
- @RequestMapping(“/toLoginIndex”)
- public String toLoginIndex(){
- return “/test”;
- }
- }
package com.zheng.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
@RequestMapping("/login")
public class TestController {
@RequestMapping("/toLoginIndex")
public String toLoginIndex(){
return "/test";
}
}
8 /src/main/webapp/WEB-INF/views/test.ftl中的完整代码为:
<html>
<head>
<title>Insert title here</title>
</head>
<body>
Hello, World!
</body>
</html>
9 /src/main/webapp/WEB-INF/urlrewrite.xml中的完整代码为
- <?xml version=“1.0” encoding=“utf-8”?>
- <!DOCTYPE urlrewrite PUBLIC ”-//tuckey.org//DTD UrlRewrite 4.0//EN”
- “http://www.tuckey.org/res/dtds/urlrewrite4.0.dtd”>
- <urlrewrite decode-using=“UTF-8”>
- <rule>
- <from>/$</from>
- <to>/service/login/toLogin</to>
- </rule>
- </urlrewrite>
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE urlrewrite PUBLIC "-//tuckey.org//DTD UrlRewrite 4.0//EN"
"http://www.tuckey.org/res/dtds/urlrewrite4.0.dtd">
<urlrewrite decode-using="UTF-8">
<rule>
<from>/$</from>
<to>/service/login/toLogin</to>
</rule>
</urlrewrite>
注意:这里的<from>和<to>决定了
http://localhost:8080/urlrewrite_demo 映射为
http://localhos:8080/urlrewrite_demo/service/login/toLoginIndex
假如<from>/$</from>改为<from>/abc</from>,则
http://localhost:8080/urlrewrite_demo/abc 映射为
http://localhos:8080/urlrewrite_demo/service/login/toLoginIndex
10 将urlrewrite_demo添加进Tomcat7中并运行
在浏览器中输入http://localhost:8080/urlrewrite_demo
显示结果为
在浏览器中输入http://localhost:8080/urlrewrite_demo/service/login/toLoginIndex
显示结果为
11 源码下载地址
CSDN:http://download.csdn.net/detail/haishu_zheng/9540381
Github:https://github.com/zhenghaishu/urlrewrite_demo