我是Struts的新手,我试着理解它是如何工作的。我知道我必须用我的表格来访问行动类中的请求参数,我还参考了各种其他论坛,试图找出答案,但这是没有用的。不管我做什么,我都会犯同样的错误。提前谢谢。
我的动作课:
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-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>另外,我对struts-config文件还有另一个疑问,为什么我不能使用前向标记,如下面的代码所示。我在图像中附加了一个错误,即必须为元素类型“action”声明"Attribute“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>发布于 2020-05-07 20:31:14
您的AddExpenseForm类没有扩展ActionForm。
关于不相关的配置问题:
actions.AddExpenseAction不存在。那不是你申报的包裹。
无关:
除非您对学习Struts 1有非常明确的理由,否则不要。
Struts 1是几年前的EOL,不应该被用于任何新的东西。
发布于 2020-05-08 13:39:49
做这个
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;
}请看一下这个Java中的继承和转换
https://stackoverflow.com/questions/61656543
复制相似问题