Search code examples
javastruts

Cannot cast from ActionForm to AddExpenseForm


I am new to Struts, i am trying to understand how it all works. I understand that i have to cast my form to access the request parameters in action class and i have also referred various other forums trying to figure out the answer but it is of no use. i keep getting the same error no matter what i try. Thanks in advance.

My Action 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;


        }
        }
      }

My Form:

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

My Struts-config:

    <?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

Also I have another doubt with my struts-config file why am i not able to use forward tag as shown in the below code. i have attached the error in the image saying "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>

Solution

  • Your AddExpenseForm class doesn't extend ActionForm.

    Regarding the unrelated config question:

    actions.AddExpenseAction doesn't exist. That's not the package you declared it in.


    Unrelated:

    Unless you have a very specific reason for learning Struts 1, don't.

    Struts 1 was EOL'd years ago and should not be used for anything new.