gpt4 book ai didi

java - 无法从 ActionForm 转换为 AddExpenseForm

转载 作者:行者123 更新时间:2023-12-01 16:52:48 24 4
gpt4 key购买 nike

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

我的 Action 类:

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 文件还有另一个疑问,为什么我无法使用前向标记,如下面的代码所示。我在图像中附加了错误,指出“必须为元素类型“action”声明属性“type”。”

 <?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 类未扩展 ActionForm

关于不相关的配置问题:

actions.AddExpenseAction 不存在。那不是您在其中声明它的包。

<小时/>

不相关:

除非您有非常具体的理由学习 Struts 1,否则不要这样做。

Struts 1 几年前就已经停产,不应该用于任何新事物。

关于java - 无法从 ActionForm 转换为 AddExpenseForm,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61656543/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com