无法从 ActionForm 转换为 AddExpenseForm

Cannot cast from ActionForm to AddExpenseForm

我是 Struts 的新手,我想了解它是如何工作的。我知道我必须强制转换我的表单才能访问请求参数 class 并且我还参考了各种其他论坛试图找出答案但它没有用。无论我尝试什么,我都会不断收到同样的错误。提前致谢。

我的操作Class:

package action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.ActionForm;

import com.opensymphony.xwork2.ActionSupport;

import dao.ExpenseDAO;
import dao.MemoryExpenseDAO;
import forms.AddExpenseForm;
import value.Expense;

public class AddExpenseAction extends ActionSupport{
    private static final long serialVersionUID = 1L;

    public String execute(ActionForm form,HttpServletRequest request, HttpServletResponse response) throws Exception{



     {
            AddExpenseForm addExpenseForm = (AddExpenseForm) form;

            Expense e = new Expense();
            e.setAmount(addExpenseForm.getAmount());
            e.setDate(addExpenseForm.getDate());
            e.setReason(addExpenseForm.getReason());

            ExpenseDAO dao = MemoryExpenseDAO.getDAO();              
            dao.insertExpense(e);

            HttpSession session = request.getSession();                
            session.setAttribute("expense", e);
            return SUCCESS;


        }
        }
      }

我的表格:

package forms;

public class AddExpenseForm {

    private String date;
    private Double amount;
    public String getDate() {
        return date;
    }
    public void setDate(String date) {
        this.date = date;
    }
    public Double getAmount() {
        return amount;
    }
    public void setAmount(Double amount) {
        this.amount = amount;
    }
    public String getReason() {
        return reason;
    }
    public void setReason(String reason) {
        this.reason = reason;
    }
     private String reason;
   }

我的Struts-配置:

    <?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-config>
     <form-beans>
        <form-bean name="AddExpenseForm" type="forms.AddExpenseForm"/>
        <form-bean name="FindExpenseByDate" type="forms.FindExpenseByDate"/>
     </form-beans>
     <action-mapping>
        <action name="AddExpenseAction" class="action.AddExpenseAction">
            <result name="success">/DisplayExpenses.jsp</result> 
            <result name="error">/error.jsp</result>
        </action>
        <action name="FindExpensesAByDate" class="action.FindExpensesAByDate">
            <result name="success">/FindExpensesByDate.jsp</result>
            <result name="error">/error.jsp</result> 
        </action>
     </action-mapping>]]
</struts-config>

enter image description here

此外,我对我的 struts-config 文件还有一个疑问,为什么我无法使用正向标记,如下面的代码所示。我附上了图片中的错误,说 "Attribute "type" must be declared for element type "action"."

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

    <!-- Data Sources -->
    <data-sources>
    </data-sources>

    <!-- Form Beans -->
    <form-beans>
        <form-bean name="AddExpenseForm" type="forms.AddExpenseForm">
        </form-bean>
        <form-bean name="FindExpensesByDateForm" type="forms.FindExpensesByDateForm">
        </form-bean>
    </form-beans>

    <!-- Global Exceptions -->
    <global-exceptions>
    </global-exceptions>

    <!-- Global Forwards -->
    <global-forwards>
    </global-forwards>

    <!-- Action Mappings -->
    <action-mappings>
        <action name="AddExpenseForm" path="/AddExpense" type="actions.AddExpenseAction">
            <forward name="success" path="/DisplayExpense.jsp">
            </forward>
        </action>

您的 AddExpenseForm class 没有扩展 ActionForm

关于不相关的配置问题:

actions.AddExpenseAction 不存在。那不是你声明它的包。


不相关:

除非你有非常具体的学习理由 Struts1,否则不要。

Struts 1 几年前就停产了,不应该用于任何新的东西。

这样做

import org.apache.struts.action.ActionForm;
public class AddExpenseForm extends ActionForm {

    private String date;
    private Double amount;
    public String getDate() {
        return date;
    }
    public void setDate(String date) {
        this.date = date;
    }
    public Double getAmount() {
        return amount;
    }
    public void setAmount(Double amount) {
        this.amount = amount;
    }
    public String getReason() {
        return reason;
    }
    public void setReason(String reason) {
        this.reason = reason;
    }
     private String reason;
   }

请看看这个 Inheritance and casting in Java