我們可以在web.xml文件中添加如下代碼,當用戶訪問不存在的頁面是,可以自動跳轉到自己定義的錯誤頁面中
<!-- 配置錯誤處理 -->
<error-page>
<error-code>404</error-code>
<location>/jsp/error.jsp</location>
</error-page>
<error-page>
<error-code>500</error-code>
<location>/jsp/error.jsp</location>
</error-page>
當然,上面是應對一些不存在的頁面,當我們使用struts2框架是,用戶通過亂輸入*****.action訪問時,當攔截器找不到對應的action時,會拋出一些後臺的異常代碼,可以進行如下操作:
直接在struts.xml的<package>中添加
<span style="color:#000000;"><default-action-ref name="index"></default-action-ref> </span>
這是默認執行的action,當攔截器找不到url傳過來的*********.action時,會執行默認的 index
<span style="color:#000000;"> <action name="index" class="com.user" method="execute">
<result name="success">index.jsp</result>
</action></span>