urlrewritefilter作用及配置例子
现在在做的问答系统,主要实现类似于百度问答形式的模式,并将其运用于校园特色模块相结合,更具校园内部问答风格。其中,本次新增的功能点使用urlrewritefilter技术。
一、作用:动态URL静态化,便于搜索引擎爬虫抓取你的动态网页。 隐藏技术实现,提高网站的移植性 。
二、配置方法及Demo
操作系统:win 7 32位旗舰版
软件环境:Myeclipse 9.0+Tomcat 7.x
第一步:导入urlrewrite-2.6.0.jar ,配置web.xml
<!-- 配置UrlRewriteFilter -->
<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>WARN</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>UrlRewriteFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<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>*.htm</url-pattern>
<dispatcher>REQUEST</dispatcher>
<dispatcher>FORWARD</dispatcher>
<dispatcher>INCLUDE</dispatcher>
</filter-mapping>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>*.jhtm</url-pattern>
<dispatcher>REQUEST</dispatcher>
<dispatcher>FORWARD</dispatcher>
<dispatcher>INCLUDE</dispatcher>
</filter-mapping>
第二步:在WEB—INF文件夹下面建立urlrewrite.xml
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE urlrewrite PUBLIC "-//tuckey.org//DTD UrlRewrite 2.6//EN"
"http://tuckey.org/res/dtds/urlrewrite2.6.dtd">
<urlrewrite>
<rule>
<note>首页</note>
<from>^/index.html$</from>
<to>/user/userList</to>
</rule>
<rule>
<note>首页</note>
<from>^/$</from>
<to>/user/userList</to>
</rule>
<rule>
<note>管理员后台登录</note>
<from>^/admin/login.htm$</from>
<to>/admin/login/toLogin.htm</to>
</rule>
<rule>
<note>管理员后台</note>
<from>^/admin(\/*)$</from>
<to>/admin/login/toLogin.htm</to>
</rule>
</urlrewrite>
第三步:在struts.xml中添加如下代码
<package name="login" namespace="/admin/login" extends="json"> <action name="toLogin" class="indexAction" method="toLogin"> <result name="success"> /WEB-INF/page/admin/login/login.jsp </result> </action> <action name="doLogin" class="indexAction" method="doLogin"> </action> </package>
第四步:编写对应的action:
/**
* 后台登陆
*
* @author 李正 2013-5-5 10:04:51
*/
@Controller
@Scope("prototype")
public class IndexAction extends BaseAction {
private static final long serialVersionUID = 1L;
@Autowired
private ILoginService loginService;
@Autowired
private IPermissionService perPermissionService;
/**
* 跳转到登录页面
*
* @return
*/
public String toLogin() {
return SUCCESS;
}
/**
* 跳转到后台首页
*
* @return
*/
public String index() {
getRequest().setAttribute("admiName",
CookieUtil.getCookieValue(Constant.ADMIN_NAME, getRequest()));
return SUCCESS;
}
/**
* 退出系统
*
* @return
*/
}
说明:当输入访问的URL:http://localhost:8087/iask/admin,此时,后缀名称“.jsp”是没有的,该地址通过urlrewrite.xml过滤解析成http://localhost:8087/iask/admin/login/toLogin.htm,该地址再通过sturs.xml文件解析namespace为/admin/login/,然后再找到对应action处理即可。
处理后效果:访问的url为http://localhost:8087/iask/admin
第五步:测试
测试输入的URL:http://localhost:8087/iask/admin1
http://localhost:8087/iask/adminTest
http://localhost:8087/iask/admin454 均显示错误404
输入:http://localhost:8087/iask/admin,测试通过
测试:http://localhost:8087/iask/admin/index.htm?r=0.49279468820504096 测试成功

浙公网安备 33010602011771号