使用攔截器時,這是我的WelcomeAction
類的NullPointerException在Struts 2的
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
文件
<?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的新手,並試圖獲得理解。我在攔截器的主題和從this教程中學習。
你在哪一行得到異常?請添加你的堆棧跟蹤! –