今天在社区看到有人提问,恰巧我也遇到同类问题:
jsp 文件:
exAction.java :
package web;
import org.apache.struts.action.*;
import javax.servlet.http.*;
public class exAction extends Action {
public ActionForward execute(ActionMapping actionMapping, ActionForm actionForm, HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) {
exActionForm exActionForm_1 = (exActionForm) actionForm;
if(exActionForm_1!=null)
{
String test = exActionForm_1.getTest();
httpServletRequest.setAttribute("test",test);
}
else
httpServletRequest.setAttribute("test","fail");
return (actionMapping.findForward("success"));
}
}
exActionForm.java:
package web;
import org.apache.struts.action.*;
import javax.servlet.http.*;
public class exActionForm extends ActionForm {
public ActionErrors validate(ActionMapping actionMapping, HttpServletRequest httpServletRequest) {
return null;
}
private String test;
public String getTest()
{
return this.test;
}
public void setTest(String test)
{
this.test=test;
}
public void reset(ActionMapping actionMapping, HttpServletRequest httpServletRequest) {
}
}
struts-config.xml:
http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd">
----------------------------------------------------------
为什么 我没的到 exActionForm 对象??,exActionForm_1 总是为 null 啊???
答复如下:
很简单,跟我的错误一样的
大量的struts书误人,很容易导致初学者出现这个问题
注意 jsp 中的 form action="exAction.do"
应为 ="/exaction/exAction.do" ,如果你的应用目录是exaction 的话
如果用
可惜在任何材料查不到标记库对路径的转换功能,只能够看源代码学习
刚好查到:
表单标记
与表单相关的操作路径是validateEmployee,而表单数据是通过POST传递的。对于这个表单来说,ActionForm bean的其他信息,如bean名称类型,作用域,都是从表单指定操作的ActionMapping中检索得到的:
21
name=”empForm”
scope=”request”
input=”/employeeInput.jsp”>
如果配置文件中包含上述信息,并且请求URI的*.do被映射到ActionServlet,与表单相关的ActionForm bean的名称,类型和作用域分别是empForm,com.example.EmployeeForm和request.这些属性也可以使用