{"id":1429,"date":"2009-07-25T09:32:39","date_gmt":"2009-07-25T00:32:39","guid":{"rendered":"http:\/\/fukata.org\/?p=1429"},"modified":"2017-08-11T22:59:08","modified_gmt":"2017-08-11T22:59:08","slug":"jc-struts2-exception","status":"publish","type":"post","link":"https:\/\/blog.fukata.org\/archives\/1429\/","title":{"rendered":"[JC]Struts2\u3067\u306e\u4f8b\u5916\u51e6\u7406\u306b\u3064\u3044\u3066"},"content":{"rendered":"

\"ExceptionSequence\"<\/a>Action\u306a\u3069\u3067Exception\u304c\u767a\u751f\u3057\u305f\u969b\u306b\u3001\u81ea\u52d5\u7684\u306b\u30a8\u30e9\u30fc\u30da\u30fc\u30b8\u306b\u9077\u79fb\u3067\u304d\u308b\u3088\u3046\u306b\u3057\u3066\u304a\u304f\u3068\u4f59\u8a08\u306a\u30a8\u30e9\u30fc\u51e6\u7406\u3092\u5185\u90e8\u3067\u5404\u5fc5\u8981\u304c\u306a\u304f\u306a\u308b\u306e\u3067\u4fbf\u5229\u304b\u3068\u601d\u3044\u4f5c\u6210\u3057\u307e\u3057\u305f\u3002<\/p>\n

\u4eca\u56de\u306e\u8981\u4ef6\u3068\u3057\u3066\u3001\u4e0a\u8a18\u306b\u52a0\u3048\u3001\u30a8\u30e9\u30fc\u306e\u5185\u5bb9\u3092\u30e1\u30c3\u30bb\u30fc\u30b8\u30b3\u30fc\u30c9\u3067\u7ba1\u7406\u3057\u3066\u3044\u308b\u306e\u3067\u305d\u308c\u3092\u5229\u7528\u3057\u3001\u3055\u3089\u306b\u610f\u56f3\u3057\u306a\u3044\u30a8\u30e9\u30fc\u306e\u5834\u5408\u306f\u30c7\u30d5\u30a9\u30eb\u30c8\u306e\u30e1\u30c3\u30bb\u30fc\u30b8\u30b3\u30fc\u30c9\u3092\u5272\u308a\u5f53\u3066\u308b\u3068\u3044\u3046\u3082\u306e\u3067\u3059\u3002<\/p>\n

\u4f5c\u6210\u3057\u305f\u306e\u306f\u3001\u300ccom.opensymphony.xwork2.interceptor.ExceptionMappingInterceptor<\/strong>\u300d\u3092\u7d99\u627f\u3057\u305f\u81ea\u4f5c\u4f8b\u5916\u30a4\u30f3\u30bf\u30fc\u30bb\u30d7\u30bf\u30fc\u30af\u30e9\u30b9\u3060\u3051\u3067\u3059\u3002\u5b9f\u969b\u3001\u51e6\u7406\u3082\u307b\u3068\u3093\u3069\u5909\u308f\u3063\u3066\u3044\u307e\u305b\u3093\u3002<\/p>\n

\u3044\u3064\u3082\u306e\u7528\u306b\u4f5c\u6210\u3057\u305f\u30bd\u30fc\u30b9\u306f\u3001\u4e0b\u8a18\u306b\u63b2\u8f09\u3057\u3066\u3044\u308b\u306e\u3067\u826f\u3051\u308c\u3070\u898b\u3066\u304f\u3060\u3055\u3044\u3002<\/p>\n

\u4f5c\u6210\u3057\u305f\u30af\u30e9\u30b9\u4ee5\u5916\u306e\u6ce8\u610f\u70b9\u3068\u3057\u3066\u3001\u30a4\u30f3\u30bf\u30fc\u30bb\u30d7\u30bf\u30fc\u30b9\u30bf\u30c3\u30af\u306b\u30c7\u30d5\u30a9\u30eb\u30c8\u306e\u300cdefaultStack\u300d\u3092\u5229\u7528\u3057\u3066\u3044\u308b\u5834\u5408\u3001exception\u3068\u3044\u3046\u901a\u5e38\u306e\u4f8b\u5916\u51e6\u7406\u7528\u30a4\u30f3\u30bf\u30fc\u30bb\u30d7\u30bf\u30fc\u304c\u5b58\u5728\u3059\u308b\u305f\u3081\u3001\u305d\u308c\u3092\u9664\u5916\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002\u5b9a\u7fa9\u6e08\u307f\u306e\u30b9\u30bf\u30c3\u30af\u304b\u3089\u9664\u5916\u3059\u308b\u65b9\u6cd5\u304c\u898b\u3064\u304b\u3089\u306a\u304b\u3063\u305f\u306e\u3067\u3001defaultStack\u304b\u3089exception\u3092\u9664\u3044\u3066struts.xml\u306b\u3066\u518d\u5b9a\u7fa9\u3057\u3066\u3044\u307e\u3059\u3002<\/p>\n

<\/p>\n

\u25a0JcExceptionInterceptor<\/p>\n

\r\npackage org.fukata.jc_common.interceptor;\r\n\r\nimport java.util.List;\r\n\r\nimport org.apache.commons.lang.StringUtils;\r\nimport org.fukata.jc_common.exception.JcSystemException;\r\n\r\nimport com.opensymphony.xwork2.ActionInvocation;\r\nimport com.opensymphony.xwork2.config.entities.ExceptionMappingConfig;\r\nimport com.opensymphony.xwork2.interceptor.ExceptionHolder;\r\nimport com.opensymphony.xwork2.interceptor.ExceptionMappingInterceptor;\r\n\r\npublic class JcExceptionInterceptor extends ExceptionMappingInterceptor {\r\n\r\n\tprivate static final long serialVersionUID = 1L;\r\n\t\r\n\tprotected String defaultMessageCode;\r\n\t\r\n\tpublic String getDefaultMessageCode() {\r\n\t\treturn defaultMessageCode;\r\n\t}\r\n\r\n\tpublic void setDefaultMessageCode(String defaultMessageCode) {\r\n\t\tthis.defaultMessageCode = defaultMessageCode;\r\n\t}\r\n\r\n\t@Override\r\n\tpublic String intercept(ActionInvocation invocation) throws Exception {\r\n        String result;\r\n\r\n        try {\r\n            result = invocation.invoke();\r\n        } catch (Exception e) {\r\n            if (isLogEnabled()) {\r\n                handleLogging(e);\r\n            }\r\n            List exceptionMappings = invocation.getProxy().getConfig().getExceptionMappings();\r\n            String mappedResult = this.findResultFromExceptions(exceptionMappings, e);\r\n            if (mappedResult != null) {\r\n            \t\r\n            \t\/\/ \u30e1\u30c3\u30bb\u30fc\u30b8\u30b3\u30fc\u30c9\u306e\u683c\u7d0d\r\n            \tException ex = e; \r\n            \tif (isDefaultMessageCode(e)) {\r\n            \t\tex = new JcSystemException(getDefaultMessageCode());\r\n            \t}\r\n            \t\r\n                result = mappedResult;\r\n                publishException(invocation, new ExceptionHolder(ex));\r\n            } else {\r\n                throw e;\r\n            }\r\n        }\r\n\r\n\t\treturn result;\r\n\t}\r\n\r\n\t\/**\r\n\t * \u30c7\u30d5\u30a9\u30eb\u30c8\u306e\u30e1\u30c3\u30bb\u30fc\u30b8\u30b3\u30fc\u30c9\u3092\u9069\u7528\u3059\u308b\u304b\u3069\u3046\u304b\r\n\t * @param e\r\n\t * @return\r\n\t *\/\r\n    private boolean isDefaultMessageCode(Exception e) {\r\n    \tif (e instanceof JcSystemException) {\r\n\t\t\tif (StringUtils.isBlank(e.getMessage())) {\r\n\t\t\t\treturn true;\r\n\t\t\t}\r\n\t\t} else {\r\n\t\t\treturn true;\r\n\t\t}\r\n    \treturn false;\r\n\t}\r\n\r\n\tprivate String findResultFromExceptions(List<\/exceptionmappingconfig> exceptionMappings, Throwable t) {\r\n        String result = null;\r\n\r\n        \/\/ Check for specific exception mappings.\r\n        if (exceptionMappings != null) {\r\n            int deepest = Integer.MAX_VALUE;\r\n            for (Object exceptionMapping : exceptionMappings) {\r\n                ExceptionMappingConfig exceptionMappingConfig = (ExceptionMappingConfig) exceptionMapping;\r\n                int depth = getDepth(exceptionMappingConfig.getExceptionClassName(), t);\r\n                if (depth >= 0 && depth < deepest) {\r\n                    deepest = depth;\r\n                    result = exceptionMappingConfig.getResult();\r\n                }\r\n            }\r\n        }\r\n\r\n        return result;\r\n    }\r\n}\r\n<\/pre>\n

\u25a0applicationContext.xml<\/p>\n

\r\n\t\r\n\t\t<\/property>\r\n\t<\/bean>\r\n<\/pre>\n

\u25a0struts.xml<\/p>\n

\r\n\t\t\r\n\t\t\r\n\t\t\t<\/interceptor>\r\n\t\t\t<\/interceptor>\r\n\r\n\t\t\t\r\n\t\t\t\t<\/interceptor>\r\n\t\t\t\t<\/interceptor>\r\n\t\t\t\t<\/interceptor>\r\n\t\t\t\t<\/interceptor>\r\n\t\t\t\t<\/interceptor>\r\n\t\t\t\t<\/interceptor>\r\n\t\t\t\t<\/interceptor>\r\n\t\t\t\t<\/interceptor>\r\n\t\t\t\t<\/interceptor>\r\n\t\t\t\t<\/interceptor>\r\n\t\t\t\t<\/interceptor>\r\n\t\t\t\t<\/interceptor>\r\n\t\t\t\t<\/interceptor>\r\n\t\t\t\t<\/interceptor>\r\n\t\t\t\t<\/interceptor>\r\n\t\t\t\t<\/interceptor>\r\n\t\t\t\t\tdojo\\..*,^struts\\..*\r\n\t\t\t\t<\/interceptor>\r\n\t\t\t\t<\/interceptor>\r\n\t\t\t\t\r\n\t\t\t\t\tinput,back,cancel,browse\r\n\t\t\t\t<\/interceptor>\r\n\t\t\t\t\r\n\t\t\t\t\tinput,back,cancel,browse\r\n\t\t\t\t<\/interceptor>\r\n\t\t\t\r\n\t\t<\/interceptors>\r\n\r\n\t\t<\/default>\r\n\r\n\t\t\r\n\t\t\t\/jsp\/Error.jsp<\/result>\r\n\t\t<\/global>\r\n\r\n\t\t\r\n\t\t\t<\/exception>\r\n\t\t<\/global>\r\n<\/pre>\n

<\/exceptionmappingconfig><\/p>\n","protected":false},"excerpt":{"rendered":"

Action\u306a\u3069\u3067Exception\u304c\u767a\u751f\u3057\u305f\u969b\u306b\u3001\u81ea\u52d5\u7684\u306b\u30a8\u30e9\u30fc\u30da\u30fc\u30b8\u306b\u9077\u79fb\u3067\u304d\u308b\u3088\u3046\u306b\u3057\u3066\u304a\u304f\u3068\u4f59\u8a08\u306a\u30a8\u30e9\u30fc\u51e6\u7406\u3092\u5185\u90e8\u3067\u5404\u5fc5\u8981\u304c\u306a\u304f\u306a\u308b\u306e\u3067\u4fbf\u5229\u304b\u3068\u601d\u3044\u4f5c\u6210\u3057\u307e\u3057\u305f\u3002 \u4eca\u56de\u306e\u8981\u4ef6\u3068\u3057\u3066\u3001\u4e0a\u8a18\u306b\u52a0\u3048 … \u7d9a\u304d\u3092\u8aad\u3080<\/a><\/p>\n","protected":false},"author":2,"featured_media":9223372036854775807,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[1980],"tags":[341,346],"_links":{"self":[{"href":"https:\/\/blog.fukata.org\/wp-json\/wp\/v2\/posts\/1429"}],"collection":[{"href":"https:\/\/blog.fukata.org\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.fukata.org\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.fukata.org\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.fukata.org\/wp-json\/wp\/v2\/comments?post=1429"}],"version-history":[{"count":0,"href":"https:\/\/blog.fukata.org\/wp-json\/wp\/v2\/posts\/1429\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blog.fukata.org\/wp-json\/wp\/v2\/media\/9223372036854775807"}],"wp:attachment":[{"href":"https:\/\/blog.fukata.org\/wp-json\/wp\/v2\/media?parent=1429"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.fukata.org\/wp-json\/wp\/v2\/categories?post=1429"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.fukata.org\/wp-json\/wp\/v2\/tags?post=1429"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}