在Struts 2中使用拦截器时出现NullPointerException [英] NullPointerException when using an interceptor in Struts 2

查看:140
本文介绍了在Struts 2中使用拦截器时出现NullPointerException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的 WelcomeAction Class

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;
    }
}

这是我的 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>

我尝试使用拦截器,但是当我取消注释时 NullPointerException 被抛出。我是Struts2的新手并试图获得理解。我是关于拦截器的主题,并从这个教程中学习。

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.

推荐答案

Struts2框架通过拦截器。为此,核心软件包在<$ c中提供默认配置 $ c> struts-default.xml ,可灵活扩展和覆盖默认设置。此配置在启动时加载并应用于您使用的每个操作,除非该操作具有自己的自定义设置。如果要将这些设置用于包配置中的每个操作,则应考虑创建拦截器堆栈。请参见我们如何配置拦截器以用于每个操作

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.


请查看Struts 2文档,了解上面提到的
拦截器的详细信息。

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.

你有 NullPointerException 因为你没有引用 params 动作配置中的拦截器。你应该知道,如果你引用一个具体动作配置的拦截器,它将使用这个拦截器,而不是默认使用的 defaultStack ,如果没有拦截器被引用动作配置。

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.

这篇关于在Struts 2中使用拦截器时出现NullPointerException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆