struts 中访问不存在的action时,默认的会显示如下页面:
这显然不友好。能不能跳转到统一的action进行处理呢?
struts 可以做到。
struts.xml配置文件如下:
- <?xml version="1.0" encoding="UTF-8" ?>
- <!DOCTYPE struts PUBLIC
- "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
- "http://struts.apache.org/dtds/struts-2.3.dtd">
- <struts>
- <constant name="struts.enable.DynamicMethodInvocation" value="false" />
- <constant name="struts.devMode" value="true" />
- <constant name="struts.ui.theme" value="simple" />
- <constant name="struts.objectFactory" value="spring" />
- <constant name="struts.xwork.chaining.copyMessages" value="true" />
- <constant name="struts.multipart.maxSize" value="58871540" /><!--
- 50 MB -->
- <constant name="struts.xwork.chaining.copyErrors" value="true" />
- <package name="default" namespace="/" extends="struts-default">
- <interceptors>
- <!-- <interceptor name="authorityInterceptor"
- class="com.shop.jn.interceptor.AuthorityInterceptor"></interceptor> -->
- <interceptor name="loggerInterceptor"
- class="com.shop.jn.interceptor.LoggerInterceptor"></interceptor>
- <interceptor name="loginTimesLimitInterceptor"
- class="com.shop.jn.interceptor.LoginTimesLimitInterceptor"></interceptor>
- <interceptor name="jsonIntercept"
- class="com.shop.jn.interceptor.JSONInterceptor"></interceptor>
- <!-- minimum interceptor stack -->
- <interceptor-stack name="minStack">
- <interceptor-ref name="defaultStack"></interceptor-ref>
- <interceptor-ref name="loggerInterceptor"></interceptor-ref>
- <!-- -->
- </interceptor-stack>
- <!-- authority check when access resource -->
- <interceptor-stack name="authorityStack">
- <interceptor-ref name="defaultStack"></interceptor-ref>
- <!-- <interceptor-ref name="authorityInterceptor"></interceptor-ref> -->
- <interceptor-ref name="loggerInterceptor"></interceptor-ref>
- </interceptor-stack>
- <!-- <interceptor-stack name="loginTimesLimitStack"> <interceptor-ref
- name="defaultStack"></interceptor-ref> <interceptor-ref name="loggerInterceptor"></interceptor-ref>
- <interceptor-ref name="loginTimesLimitInterceptor"></interceptor-ref> </interceptor-stack> -->
- </interceptors>
- <default-interceptor-ref name="authorityStack"></default-interceptor-ref>
- <!-- <default-action-ref name="index" /> -->
- <default-action-ref name="notFound" />
- <global-results>
- <result name="error">/error.jsp</result>
- <result name="login2">/user/loginInput.jsp</result>
- </global-results>
- <action name="notFound" class="com.shop.jn.action.error.NotFoundErrerAction">
- <result name="success">/not_found.jsp</result>
- </action>
- <!--<global-exception-mappings> <exception-mapping exception="java.lang.Exception"
- result="error" /> </global-exception-mappings> --><!--<action name="index"> <result type="redirectAction"> </result> </action> -->
- </package>
- <package name="goods22" extends="struts-default" namespace="/goods">
- <action name="*Goods" class="{1}GoodsAction">
- <result type="redirectAction">
- <param name="namespace">/goods</param>
- <param name="actionName">viewGoods</param>
- </result>
- <result name="input">/input.jsp</result>
- </action>
- <!-- this is a exception ,must not use wildcard -->
- <action name="viewGoods" class="viewGoodsAction">
- <result>/goods/viewGoods.jsp</result>
- </action>
- <action name="addInputGoods" class="addInputGoodsAction">
- <result>/goods/addGoods.jsp</result>
- </action>
- <action name="updateInputGoods" class="updateInputGoodsAction">
- <result>/goods/addGoods.jsp</result>
- </action>
- </package>
- <package name="supermarket22" extends="default" namespace="/supermarket">
- <action name="*Supermarket" class="{1}SupermarketAction">
- <result type="redirectAction">
- <param name="namespace">/supermarket</param>
- <param name="actionName">viewSupermarket</param>
- </result>
- </action>
- <!-- this is a exception ,must not use wildcard -->
- <action name="viewSupermarket" class="viewSupermarketAction">
- <result>/supermarket/viewSupermarket.jsp</result>
- </action>
- <action name="updateInputSupermarket" class="updateInputSupermarketAction">
- <result>/supermarket/addSupermarket.jsp</result>
- </action>
- </package>
- </struts>
在上述struts.xml文件中,我配置了default-action-ref,默认action的名称是”notFound“,对应的Java类是”com.shop.jn.action.error.NotFoundErrerAction“:
- package com.shop.jn.action.error;
- import com.opensymphony.xwork2.ActionSupport;
- public class NotFoundErrerAction extends ActionSupport {
- private static final long serialVersionUID = 6218614965524501080L;
- @Override
- public String execute() throws Exception {
- System.out.println("您访问的action 不存在!");
- return super.execute();
- }
- }
访问notFound action时,跳转到的页面是:not_found.jsp,
not_found.jsp 内容如下:
- <?xml version="1.0" encoding="UTF-8" ?>
- <%@ page language="java" contentType="text/html; charset=UTF-8"
- pageEncoding="UTF-8"%>
- <%
- String path = request.getContextPath();
- String basePath = request.getScheme() + "://"
- + request.getServerName() + ":" + request.getServerPort()
- + path + "/";
- %>
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
- <title>Insert title here</title>
- </head>
- <body>
- <h1>
- <font color="red" >not found</font>
- </h1>
- </body>
- </html>
在浏览器中显示界面如下:
继续说明struts.xml文件:
package goods22继承了struts-default(struts默认的package),命名空间是”/goods“;而package supermarket22 继承了default(default中配置了默认的action),命名空间是”/supermarket“。
default 是我自定义的package。
我们在浏览器地址栏中输入:http://localhost:8080/shop_goods/supermarket/xxx.action
上述url对应的action是不存在的!
结果如下:
达到了我们的需求。
但是我 在浏览器地址栏中输入:
http://localhost:8080/shop_goods/goods/xxx.action
时,结果如下:
没有达到我们的需求,为什么没有跳转到默认action呢?
因为上述url对应的命名空间(namespace)是/goods,而该package 并没有继承default package。访问http://localhost:8080/shop_goods/supermarket/xxx.action 就可以到达默认action,因为supermarket 继承了default package 。
如何解决上述问题呢?
方式一:
删除package default 的namespace,这样不论package 目录结构如何只要找不到action,就会跳转到默认daction;
struts.xml 部分代码如下:
- <package name="default" extends="struts-default">
- <interceptors>
- <!-- <interceptor name="authorityInterceptor"
- class="com.shop.jn.interceptor.AuthorityInterceptor"></interceptor> -->
- <interceptor name="loggerInterceptor"
- class="com.shop.jn.interceptor.LoggerInterceptor"></interceptor>
- <interceptor name="loginTimesLimitInterceptor"
- class="com.shop.jn.interceptor.LoginTimesLimitInterceptor"></interceptor>
- <interceptor name="jsonIntercept"
- class="com.shop.jn.interceptor.JSONInterceptor"></interceptor>
- <!-- minimum interceptor stack -->
- <interceptor-stack name="minStack">
- <interceptor-ref name="defaultStack"></interceptor-ref>
- <interceptor-ref name="loggerInterceptor"></interceptor-ref>
- <!-- -->
- </interceptor-stack>
- <!-- authority check when access resource -->
- <interceptor-stack name="authorityStack">
- <interceptor-ref name="defaultStack"></interceptor-ref>
- <!-- <interceptor-ref name="authorityInterceptor"></interceptor-ref> -->
- <interceptor-ref name="loggerInterceptor"></interceptor-ref>
- </interceptor-stack>
- <!-- <interceptor-stack name="loginTimesLimitStack"> <interceptor-ref
- name="defaultStack"></interceptor-ref> <interceptor-ref name="loggerInterceptor"></interceptor-ref>
- <interceptor-ref name="loginTimesLimitInterceptor"></interceptor-ref> </interceptor-stack> -->
- </interceptors>
- <default-interceptor-ref name="authorityStack"></default-interceptor-ref>
- <!-- <default-action-ref name="index" /> -->
- <default-action-ref name="notFound" />
- <global-results>
- <result name="error">/error.jsp</result>
- <result name="login2">/user/loginInput.jsp</result>
- </global-results>
- <action name="notFound" class="com.shop.jn.action.error.NotFoundErrerAction">
- <result name="success">/not_found.jsp</result>
- </action>
- <!--<global-exception-mappings> <exception-mapping exception="java.lang.Exception"
- result="error" /> </global-exception-mappings> --><!--<action name="index"> <result type="redirectAction"> </result> </action> -->
- </package>
方式二:
让所有的package 都继承我自定义的package default