`
run_wang
  • 浏览: 161965 次
  • 性别: Icon_minigender_1
  • 来自: 武汉
文章分类
社区版块
存档分类
最新评论

Struts2中UrlRewriterFilter(url重写)的初步使用

阅读更多
之前做的网站基本成型,不过,听说要对搜索引擎友好点,还得要将网站弄成静态化,至少要做点“伪静态”的工作。而我对url rewrite早有所闻,于是乎就上其官网 下了个来玩。入门很简单,下载地址是http://tuckey.org/urlrewrite/dist/urlrewritefilter-2.6.zip ,下载来解压。将解压出来的urlrewrite-2.6.0.jar复制到项目中的WEB-INF\lib下,还有urlrewrite.xml复制到WEB-INF下。

接下来就是配置了,配置也是超级简单的。

web.xml里的配置如下,在urlrewrite.xml里面也有注释说明怎么添加的。 

        <!– UrlRewriteFilter –>
        <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>
        </filter-mapping>

注意将这一段配置放在struts2的配置上面。还要在struts2的配置里加上两行,具体如下:

    <!– 配置struts2 –>
        <filter>
            <filter-name>struts2</filter-name>
            <filter-class>
                org.apache.struts2.dispatcher.FilterDispatcher
            </filter-class>
        </filter>

        <filter-mapping>
            <filter-name>struts2</filter-name>
            <url-pattern>/*</url-pattern>
            <dispatcher>REQUEST</dispatcher>
            <dispatcher>FORWARD</dispatcher>
        </filter-mapping>



接下来是修改配置urlrewrite.xml里的内容,它的规则如下:

    <urlrewrite>

        <rule>
            <note>
                The rule means that requests to /test/status/ will be redirected to /rewrite-status
                the url will be rewritten.
            </note>
            <from>/test/status/</from>
            <to type="redirect">%{context-path}/rewrite-status</to>
        </rule>

    

    <outbound-rule>
         <note>
             The outbound-rule specifies that when response.encodeURL is called (if you are using JSTL c:url)
             the url /rewrite-status will be rewritten to /test/status/.

             The above rule and this outbound-rule means that end users should never see the
             url /rewrite-status only /test/status/ both in thier location bar and in hyperlinks
             in your pages.
         </note>
         <from>/rewrite-status</from>
         <to>/test/status/</to>
    </outbound-rule>

    

    </urlrewrite>

只要你自己把需要重写的URL规则写上去就好了,我的一个例子就是:

    <rule>
            <note>shop</note>
            <from>/shops.html</from>
            <to>/shops/findShops.action</to>
        </rule>

这样访问shops.html就是访问/shops/findShops.action。
分享到:
评论

相关推荐

    struts2中实现URL重写

    最近开发项目的时候,遇到Struts2需要URL重写的问题,总结了下资料,和大家分享!!

    使用urlrewrite重写struts2的action

    NULL 博文链接:https://namas.iteye.com/blog/1130854

    Struts2初步使用总结

    Struts2初步使用总结 初步介绍Struts2和用法

    struts2中的url

    关于web开发中最烦恼的一件事就是如何来确定url的问题,特别是在struts2中!看过这篇,立马让你豁然开朗!

    struts1和struts2的区别

    struts1和struts2的区别struts1和struts2的区别struts1和struts2的区别struts1和struts2的区别struts1和struts2的区别struts1和struts2的区别struts1和struts2的区别struts1和struts2的区别struts1和struts2的区别...

    struts2帮助文档

    struts2中的国际化 struts2转化器 struts2实现表单数据校验 struts2的基石-拦截器 struts2中实现IOC struts2中实现文件上传 struts2中实现CRUD struts2中的OGNL struts2的新表单标志的使用 struts2与AJAX一 struts2...

    struts2标签使用例子

    struts2标签使用例子 struts2标签使用例子 struts2标签使用例子struts2标签使用例子

    Struts2_s2-016&017&ognl2.6.11_patch漏洞补丁

    -- 为修复struts2 s2-016、s2-017漏洞,重写DefaultActionMapper --&gt; &lt;bean type="org.apache.struts2.dispatcher.mapper.ActionMapper" name="myDefaultActionMapper" class=...

    struts2(1-7)源码 struts2学习入门 源码学习

    struts 初步认识Struts2并部署验证 struts2 将要进行自定义类型转换的点数据从1个增加到3个 struts3 进行全局的类型转换 struts4 使用Struts2中内部类中的方法进行自定义类型转换,用到的类StrutsTypeConverter ...

    struts2 中 OGNL表达式的使用

    struts2 中 OGNL表达式的使用struts2 中 OGNL表达式的使用

    struts2中iterator 标签的使用详解

    struts2中iterator 标签的使用详解

    Struts2视频教程

    Struts2是一套非常优秀的Java WEB应用框架,实现优雅、功能强大、使用简洁。目前已有大量的企业项目采用Struts2来作为Web框架进行开发,掌握Struts2是每个进行Web开发的Java程序员的一项必备技能。 本视频对Struts2...

    struts2改写url后缀demo

    struts2实现改写在url后缀的方法,如XX.do---&gt;改写为xx.html等,方便试用,一目了然

    ssh2中的urlrewrite和Struts2的生成静态页面

    ssh2中的urlrewrite和Struts2的生成静态页面

    struts2-core.jar

    struts2-core-2.0.1.jar, struts2-core-2.0.11.1.jar, struts2-core-2.0.11.2.jar, struts2-core-2.0.11.jar, struts2-core-2.0.12.jar, struts2-core-2.0.14.jar, struts2-core-2.0.5.jar, struts2-core-2.0.6.jar,...

    struts2初始使用环境配置

    javaEE学习阶段,struts2初步使用环境配置。 javaEE学习阶段,struts2初步使用环境配置。

    struts2 总结工程大全

    struts2 总结工程大全struts2 总结工程大全struts2 总结工程大全struts2 总结工程大全struts2 总结工程大全struts2 总结工程大全struts2 总结工程大全struts2 总结工程大全struts2 总结工程大全struts2 总结工程大全...

    struts2案例 struts2 struts2源码

    struts2源码 struts2案例 struts2使用包

    struts2jar.zip

    struts2实例 可用于struts初步的学习和研究 和示例等

    struts2环境搭建+struts2 新闻发布系统+struts2 留言板

    struts2环境搭建+struts2 新闻发布系统+struts2 留言板 struts2环境搭建:基础框架搭建,简单易上手,...struts2 留言板:struts2+jquery 直接导入myecplise中即可使用,很好用,可以在此基础上添加并完善项目将更完美。

Global site tag (gtag.js) - Google Analytics