struts2 僞靜態
最近想在項目中使用僞靜態技術,項目使用的是struts2框架。最近了一下幾種方法
第一:url重新的方式。url重寫就是將瀏覽器的一個請求在服務器端重定向到另一個可以處理請求的url上。
例如:http://www.moonteam.net/news/1.htm 重定向到http://www.moonteam.net/news/viewNews.action?id=1
這樣的好處是地址統一,且利於搜索引擎。詳見:http://www.sunxin.org/forum/thread/20748.html
第二:struts本身就支持風格化的url
步驟:(1)、增加struts的擴展名<constant name="struts.action.extension" value="action,htm" />,struts2擁有了兩個擴展名action和htm
(2)、 在web.xml中配置struts2的核心控制器將*.ation,*.htm的請求交給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>*.action</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>*.htm</url-pattern>
</filter-mapping>
(3)、配置具體的action
<package name="news" extends="struts-default" namespace="/index">
<action name="*" class="com.moonteam.struts2.action.ViewNewsAction" >
<param name="paramName" >{1}</param>
<result name="success">index.jsp</result>
</action>
</package>
這樣一個http://www.moonteam.net/news/1.htm 就映射到了http://www.moonteam.net/news/viewNews.action?id=1