在 Struts2 的異常處理有兩種,一種是程式撰寫的邏輯出錯,另一種是找不到 action Name : There is no Action mapped for namespace...,避免系統出錯的改善作法如下:

 

程式撰寫的邏輯出錯:

在 Struts.xml 內加入

<global-results>
    <result name="error">/error.jsp</result>
</global-results>

<global-exception-mappings>
    <exception-mapping exception="java.lang.Exception" result="error"/>            
</global-exception-mappings>

建立 error.jp 檔案

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>An unexpected error has occurred</title>
</head>
<body>

    <h3>The application has malfunctioned(該應用程序出現錯誤)</h3>

    <p>Please contact technical support with the following information(請聯繫資訊單位並提供以下信息):</p>

    <h4>
        Exception Name:<s:property value="exception" />
    </h4>

    <h4>
        Exception Details:<s:property value="exceptionStack" />
    </h4>

</body>
</html>

 

一種是找不到 action Name:

有兩個方法可以解決

第一種是方法利用 default-action-ref:

在 Struts.xml 內加入

<default-action-ref name="UnderConstruction"/>

<action name="UnderConstruction">
   <result>/UnderConstruction.jsp</result>
</action>

建立 UnderConstruction.jp 檔案

<%@ page language="java" contentType="text/html; charset=UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<%@page import="org.apache.struts2.ServletActionContext"%>
<%@page import="com.opensymphony.xwork2.ActionContext"%>
<%@page import="java.util.*"%>
<%@page import="java.net.*"%>
<%@page import="java.text.MessageFormat"%>
<%@page import="com.gussh.common.utils.*"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>

<%

String username = (String)session.getAttribute("USERNAME");
String password = (String)session.getAttribute("PASSWORD");
String language = (String)request.getSession().getAttribute("loginLanguage");
String progModule = (String)session.getAttribute("progModule");
String actionName= "";
String actionURL= "";


ActionContext actionContext = ServletActionContext.getContext();
actionName = actionContext.getName();

actionURL="http://polinwei.blogspot.tw/login.do";

    
    
%>
<body>

<%if(username != null){%>
    <form name="ssoForm" action="<%=actionURL %>" target='_blank'>
    <input type="hidden" name="username" value="<%=username %>" />
    <input type="hidden" name="password" value="<%=password %>"  />
    <input type="hidden" name="language" value="<%=language %>"  />       
    </form>
    <script type="text/javascript">
    var formObj=document.ssoForm;
    formObj.method='post';
    formObj.submit();
    document.location.href="<%=request.getContextPath()%>/Index?reqCode=moduleSelect&progModule=<%=progModule%>"
    </script>
<% }else{ %>
    <script type="text/javascript">
        window.opener.top.document.location.href="<%=actionURL %>";
        window.opener=null;
        window.open("","_self");
        window.close();
    </script>
<% }%>
</body>
</html>

 

第二種作法則是利用 unknown-handler-stack:

在 Struts.xml 內加入

<struts>
.
.    
   <!-- 使用bean 定義一個UnknownHandler -->  
     <bean type="com.opensymphony.xwork2.UnknownHandler" name="myHandler" class="com.sc.actions.UnderConstructionAction" />
    
   <package name="default" namespace="/" extends="struts-default">
   .
   .
   </package>    
   <!-- Add addition packages and configuration here. -->

     
    <!-- 定義本系統的 UnknownHandler -->        
    <unknown-handler-stack>
       <unknown-handler-ref name="myHandler" />
    </unknown-handler-stack>      
</struts>   

 

class="com.sc.actions.UnderConstructionAction"

package com.gussh.sc.actions;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.dispatcher.ServletDispatcherResult;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.Result;
import com.opensymphony.xwork2.UnknownHandler;
import com.opensymphony.xwork2.XWorkException;
import com.opensymphony.xwork2.config.entities.ActionConfig;


public class UnderConstructionAction implements UnknownHandler {
    // 網頁會使用的 request, response, session
    public HttpServletRequest request = ServletActionContext.getRequest();
    public HttpServletResponse response = ServletActionContext.getResponse();
    public Map<String, Object> session = ActionContext.getContext().getSession();
   
    public ActionConfig handleUnknownAction(String namespace, String actionName)
            throws XWorkException {        

        ActionConfig actionConfig = null;
        request.setAttribute("actionName",actionName);    
        
        return actionConfig;

    }

    public Result handleUnknownResult(ActionContext actionContext,
            String actionName, ActionConfig actionConfig, String resultCode)
            throws XWorkException {
        actionContext.put("action", actionName);  
        actionContext.put("result", resultCode);
        request.setAttribute("actionName",actionName);
        return new ServletDispatcherResult("/templates/SSO/GUSSO.jsp");
    }

    public Object handleUnknownActionMethod(Object action, String methodName)
            throws NoSuchMethodException {
        // TODO Auto-generated method stub
        return null;
    }

}

 

 

參考:

https://struts.apache.org/docs/unknown-handlers.html

https://struts.apache.org/docs/exception-configuration.html

http://terryjs.iteye.com/blog/777999

arrow
arrow

    MIS 發表在 痞客邦 留言(0) 人氣()