NullPointerException when using an interceptor in

2020-03-30 04:10发布

This is my WelcomeAction Class

package com.codinghazard.actions;

public class WelcomeAction {

    private String operandA;
    private String operandB;
    private Character operator; 
    private int sum;


    public String execute() {
        if ((operandA!="") && (operandB!=""))
        {
            int a=Integer.parseInt(operandA);
            int b=Integer.parseInt(operandB);
            switch (operator) 
            {
            case '1':
                sum=a+b;
                break;
            case '2':
                sum=a-b;
                break; 
            case '3':
                sum=a*b;
                break;
            case '4':
                try
                {
                    sum=a/b;
                }
                catch(ArithmeticException ae)
                {
                    return "ERROR";
                }
                break;                  

            }
            return "SUCCESS";
        }
        return "ERROR";
    }


    public String getOperandA() {
        return operandA;
    }


    public void setOperandA(String operandA) {
        this.operandA = operandA;
    }


    public String getOperandB() {
        return operandB;
    }


    public void setOperandB(String operandB) {
        this.operandB = operandB;
    }


    public Character getOperator() {
        return operator;
    }


    public void setOperator(Character operator) {
        this.operator = operator;
    }


    public int getSum() {
        return sum;
    }


    public void setSum(int sum) {
        this.sum = sum;
    }
}

this is my struts.xml file

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">

<struts>
 <!-- http://localhost:8080/test/user/login -->
    <package name="user" namespace="/user" extends="struts-default">
        <action name="calculator">
        <interceptor-ref name="timer" />
            <result>pages/Calculator.jsp</result>
        </action>
        <action name="Welcome" class="com.codinghazard.actions.WelcomeAction">
        <%--
        <interceptor-ref name="timer" />
        --%>
            <result name="SUCCESS">pages/Result.jsp</result>
            <result name="ERROR">pages/error.jsp</result>

        </action>

    </package>

</struts>

I tried to user interceptor but when I uncomment it NullPointerException is thrown. I am new to Struts2 and trying to get an understanding. I am on the topic of interceptors and learning from this tutorial.

2条回答
Root(大扎)
2楼-- · 2020-03-30 04:44

The Struts2 framework leverages the code required to support its features via interceptors. For this purpose the core package provides the default configuration in the struts-default.xmlwith the flexibility to extend and override the default settings. This configuration is loaded on startup and applies to every action you use unless the action has its own custom settings. If you want to use these settings to every action in your packages configuration then you should consider creating the interceptor stack. See How do we configure an Interceptor to be used with every Action.

Please look into Struts 2 documentation for complete detail on above mentioned interceptors.

This is very useful suggestion from the link of the tutorial. However the tutorial itself intended for middle or advanced readers. The beginners should start from Getting Started.

You got NullPointerException because you didn't reference params interceptor in the action configuration. And you should know that if you reference an interceptor for a concrete action config, it will use this interceptor and not the defaultStack which is used by default if no interceptor is referenced by the action configuration.

查看更多
做个烂人
3楼-- · 2020-03-30 04:59
 <action name="Welcome" class="com.codinghazard.actions.WelcomeAction">

    <interceptor-ref name="timer" />
    <interceptor-ref name="defaultStack"/>
        <result name="SUCCESS">pages/Result.jsp</result>
        <result name="ERROR">pages/error.jsp</result>

    </action>

try above.

Whenever u ll use any extra interceptor which is not available in default interceptor list of struts2 then explicitly you have to add default stack to avail the functionality of default inteceptos as params or workflow.

查看更多
登录 后发表回答