struts2与urlrewrite的整合
来源:互联网 发布:土摩托 知乎 编辑:程序博客网 时间:2024/05/16 10:12
关于为什么要把.jsp或者.do?param1=value1¶m2=value2.......这样的链接地址修改成普通的html地址,
作用1:
便于搜索引擎搜索。
作用2:
隐藏技术实现,提高网站的移植性,对于移植性这点会带来很大的方便,当底层定义的相关.do方法变化或者修改后不必到每个页面去修改相关的链接,而是直接修改urlrewrite.xml
其他的相关介绍网上一把一把的,下面具体说实现。环境struts2+spring+hibernate
第一步:
下载JAR包,我这用的是urlrewrite-2.6.0.jar
第二步:
修改项目的web.xml,在web.xml加入
- <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>
注意与其他filter间的顺序,加在OpenSessionInViewFilter之 后,org.apache.struts2.dispatcher.FilterDispatcher之前,不然会出现could not initialize proxy - the owning Session was closed异常
将struts2过滤器进行修改,修改后如下
- <filter-mapping>
- <filter-name>struts2</filter-name>
- <url-pattern>/*</url-pattern>
- <dispatcher>REQUEST</dispatcher>
- <dispatcher>FORWARD</dispatcher>
- </filter-mapping>
一定要加
<dispatcher>REQUEST</dispatcher>
<dispatcher>FORWARD</dispatcher>
不然会导致404错误
第三步:
将官方下载urlrewrite.xml放进WEB-INF目录下注释掉不相关类容,加入自己的规则
第四步:
具体的规则说明
假设你的站点为:http://127.0.0.1:8080/app
规则1:不带参数的情况
<rule>
<note/>
<from>^/testUrl.html</from>
<to>/ajax/testUrl.do</to>
</rule>
当你请求http://127.0.0.1:8080/app/testUrl.html实际是访问的http://127.0.0.1:8080/app/ajax/testUrl.do
规则2:带一个数字参数的情况
<rule>
<note>comment-productId.html</note>
<from>^/comment/-([0-9]+).html$</from>
<to>/user/queryComment.do?productId=$1</to>
</rule>
当你请求http://127.0.0.1:8080/app/comment-100.html实际是访问的http://127.0.0.1:8080/app/user/queryComment.do?productId=100
规则3:带多个数字参数,既有数字又有字母的情况
<rule>
<note>comment-productId-pgMethod-cPage.html</note>
<from>^/comment/-(/d+)/-(/w+)/-(/d+)/.html$</from>
<to>/user/queryComment.do?productId=$1& pgMethod=$2& cPage=$3</to>
</rule>
当你请求http://127.0.0.1:8080/app/comment-100-n-1.html
实际是访问的http://127.0.0.1:8080/app/user/queryComment.do?productId=100&pgMethod=n&cPage=1
- struts2与urlrewrite的整合
- struts2与urlrewrite的整合
- Struts2 + urlrewrite 整合的问题
- Struts2与URLRewrite的集成
- 剖析struts2.1.6与URLRewrite的结合
- Struts2与Spring的整合
- FCKeditor与struts2的整合!
- Struts2与Spring的整合
- struts2 与spring 的整合
- SPRING与STRUTS2的整合
- struts2 与 ckeditor3的整合
- spring 与struts2 的整合
- Struts2与Spring的整合
- struts2与spring的整合
- MyBatis_spring与struts2的整合
- spring与struts2的整合
- struts2与spring的整合
- 关于UrlRewrite与struts2.x结合
- 关于如何判断 grep 返回结果
- 配置mpich2运行环境
- MyBatis入门应用
- Matlab通过JDBC连接SQLite数据库
- unable to write to temp file for saving operation
- struts2与urlrewrite的整合
- FusionCharts Suite XT去水印
- Android Xutils 框架
- C 字符串的定义,初始化输出
- Hibernate查询
- 2015的三分之一
- Mac上的抓包工具Charles
- Android登录界面账号密码输入框选中时,选中背景变换的实现方法
- 【BZOJ 3991】 [SDOI2015]寻宝游戏