开发者

Struts2 Application hides my exceptions after adding Interceptor

开发者 https://www.devze.com 2023-01-25 01:37 出处:网络
So I have a Struts2 application that I\'m working on.On my front page I have a section that will display any exceptions my application throws.This worked well until I added a custom Interceptor.

So I have a Struts2 application that I'm working on. On my front page I have a section that will display any exceptions my application throws. This worked well until I added a custom Interceptor.

Here is my interceptor code:

public String intercept(ActionInvocation actionInvocation) throws Exception {

    String result = actionInvocation.invoke();

    return result;
}

This is the code in my Action class where the exception gets generated, it occurs where AuthService.Authorize() is called:

if(AuthService.Authorize(username, password)) {
        if(AuthServ开发者_运维问答ice.AdminAuthorized()) {
            return "admin";
        }
        return SUCCESS;
    }

This is inside of AuthService.Authorize(), it throws a null point exception when acc is accessed :

try {
            acc = profileRepository.WhereSingle("Username", "=", username);
        } catch (Exception e) {
            return false;
        }

        if (acc.Password.equals(password)) {

However, when the page is loaded. This is not populated:

<s:property value="%{exception.message}"/>

I have tested it and it would work if I was simply throwing an exception from the Action class. I am not calling a redirectAction or anything

Here is the top of my default package definition which all my other packages extend

<package name="default" namespace="/" extends="struts-default">

    <!-- Interceptors -->
    <interceptors>
        <interceptor name="conversation" class="global.ConversationInterceptor"/>
        <interceptor-stack name="dils-stack">
            <interceptor-ref name="defaultStack"/>
            <interceptor-ref name="conversation"/>                
        </interceptor-stack>
    </interceptors>

    <default-interceptor-ref name="dils-stack"/>

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

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


How is your interceptor stack defined for that action? The ExceptionMappingInterceptor should be defined first in the stack. Can you post the interceptor stack configuration from your struts.xml? Your custom interceptor should not be interfering (it does nothing).

Updated:

I was able to reproduce this issue, however it occurs for me with or without your custom interceptor.

The reason is that you are specifically looking for the exception message, which is not set for NullPointerExceptions that are automatically thrown (as in your case). You can confirm this by instead displaying the stack trace, such as: %{exceptionStack}

%{exception.message} is null for the NullPointerException, and so it displays nothing. If instead you were to throw an exception with a message (e.g., throw new RuntimeException("OMG!");), then you will see the message.

Also, note that you must specify more specific exception mappings before less specific mappings in your struts.xml. Because NullPointerException is more specific than Exception, you must list it first. Note that this doesn't really matter in your example, because they map to the same thing. Just know that your NPE will map to the first entry, not the second.

0

精彩评论

暂无评论...
验证码 换一张
取 消