今天在社区看到有人提问,恰巧我也遇到同类问题:
jsp 文件:
<%@ page contentType="text/html; charset=GBK" %>
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>
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 的话
如果用 标记,就没有这个问题
可惜在任何材料查不到标记库对路径的转换功能,只能够看源代码学习
刚好查到:
的action应用比较复杂:
表单标记
标记用来显示HTML标记,可以指定AcitonForm bean的名称和它的类名。如果没有设置这些属性,就需要有配置文件来指定ActionMapping以表明当前输入的是哪个JSP页,以及从映射中检索的bean名和类。如果在ActionMapping指定的作用域中没有找到指定的名称,就会创建并存储一个新的bean,否则将使用找到的bean。