作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我是 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>
此外,我对 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/
我是 Struts 新手,我试图了解它是如何工作的。我知道我必须转换我的表单才能访问操作类中的请求参数,并且我还引用了各种其他论坛试图找出答案,但它没有用。无论我尝试什么,我都会遇到同样的错误。提前致
我是一名优秀的程序员,十分优秀!