Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
296 views
in Technique[技术] by (71.8m points)

struts2 - Struts 2 - How to redirect exceptions of type Exception to a particular page, but not handle a particular exception?

I wish to redirect all errors of type Exception to the result "error". For that, I did this :

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

But I don't want to handle a particular exception, specifically the org.springframework.security.access.AccessDeniedException which should be allowed to propagate further. How can I achieve that ?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Use instanceof operator and rethrow desired exception from exception handler.

I managed it with an interceptor (here is what I used to try it out):

package com.kenmcwilliams.interceptor;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.Interceptor;
import java.util.logging.Level;
import java.util.logging.Logger;

public class Rethrower implements Interceptor{
    @Override
    public void destroy() {
    }

    @Override
    public void init() {
    }

    @Override
    public String intercept(ActionInvocation invocation){
        System.out.println("Start rethrower!");
        String result = "success";
        try {
            result = invocation.invoke();
        } catch (Exception ex) {
            Logger.getLogger(Rethrower.class.getName()).log(Level.SEVERE, null, ex);
        }
        Object exception = ActionContext.getContext().getValueStack().findValue("exception");
        if (exception instanceof RuntimeException){
            System.out.println("DOING RETHROW!");
            RuntimeException e = (RuntimeException)exception;
            throw e;
        }
        System.out.println("After rethrower!");
        return result;
    }
}

Here is the struts.xml (to save time looking up struts dtd):

<?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.devMode" value="true" />
    <constant name="struts.ui.theme" value="simple" />
    <package  name="kenmcwilliams"  namespace="/" extends="struts-default">
        <interceptors>
            <interceptor name="rethrower" class="com.kenmcwilliams.interceptor.Rethrower"/>
            <interceptor-stack name="rethrow-stack">
                <interceptor-ref name="rethrower"/>
                <interceptor-ref name="defaultStack"/>
            </interceptor-stack>
        </interceptors>
        <global-results>
            <result name="error" >/WEB-INF/content/error.jsp</result>
        </global-results>
        <global-exception-mappings>
            <exception-mapping exception="java.lang.Exception" result="error"/>
        </global-exception-mappings>
        <action name="mybomb" class="com.kenmcwilliams.kensocketchat.action.Bomb">
            <interceptor-ref name="rethrow-stack"/>
            <result>/WEB-INF/content/bomb.jsp</result>
        </action>
    </package>
</struts>

Finally the action (it just throws a runtime exception):

package com.kenmcwilliams.kensocketchat.action;

import com.opensymphony.xwork2.ActionSupport;

public class Bomb extends ActionSupport{
    @Override
    public String execute() throws Exception{
        throw new RuntimeException();
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...