• springMVC学习之url重写:urlrewrite with tuckey UrlRewriteFilter


     

     

    在开发网站时地址栏的一些信息是我们不希望让客户看到,所以在开发时候就会涉及到url重写的问题。

    下面介绍一种常用的url地址重写的方法。

    1.利用maven下载相关jar文件,pom文件配置如下:

    <dependency>
        <groupId>org.tuckey</groupId>
        <artifactId>urlrewritefilter</artifactId>
        <version>4.0.4</version>
    </dependency>

    2.在web.xml中配置UrlRewriteFilter,配置如下:

    <filter>
      <filter-name>UrlRewriteFilter</filter-name>
      <filter-class>org.tuckey.web.filters.urlrewrite.UrlRewriteFilter</filter-class>
      <!-- 因为urlrewrite.xml只能在web-inf下,所以我们这里可以省略掉urlrewrite.xml的路径配置。 -->
      <init-param>
        <param-name>logLevel</param-name>
        <param-value>WARN</param-value>
      </init-param>
    </filter>
      <filter-mapping>
      <filter-name>UrlRewriteFilter</filter-name>
      <url-pattern>/*</url-pattern>
      <dispatcher>REQUEST</dispatcher>
      <dispatcher>FORWARD</dispatcher>
    </filter-mapping>

    3.添加urlrewirte.xml文件,将文件放在WEB-INF目录下(貌似该文件只能放在WEB-INF下)。配置如下:

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE urlrewrite PUBLIC "-//tuckey.org//DTD UrlRewrite 3.2//EN" "\urlrewrite3.2.dtd">
    <urlrewrite>
      <rule>
        <note>重写loign.jsp成login.html</note>
        <!-- 对应controller中redireact的路径 -->
        <from>login.html</from>
        <!-- 对应实际工程中jsp文件 -->
        <to type="forward">/login.jsp</to>
      </rule>
    </urlrewrite>

    4.程序controller中的返回内容要与<from>中的内容一致,比如下列代码片返回值得要与urlrewrite中的一致:

    @RequestMapping(value="login.do")
    public String login(HttpServletRequest request, HttpServletResponse response, Role role) throws Exception {
    if (userService.selectRoleByNameAndPassword(role)) {
        return "index";
      } else {
        return "redirect:login.html";
      }
    }

     
    分类: Spring
  • 相关阅读:
    PHP的垃圾回收机制
    python使用httpHandler处理请求案例
    使用python中urllib.request.Request()来构建ua
    Pillow《转载》
    python数据格式化之pprint
    使用Mechanize实现自动化表单处理
    beautifulSoup《转》
    Python3网络爬虫(四):使用User Agent和代理IP隐藏身份《转》
    python urllib和urllib3包使用
    使用Mechanize实现自动化表单处理
  • 原文地址:https://www.cnblogs.com/kelelipeng/p/13093396.html
Copyright © 2020-2023  润新知