【问题标题】:No getter method for property... error没有属性的getter方法...错误
【发布时间】:2014-03-02 20:46:26
【问题描述】:

我无法找出我做错了什么。

我得到这个错误:

javax.servlet.jsp.JspException: No getter method for property: "firstname" of bean: "org.apache.struts.validator.DynaValidatorForm"
    at org.apache.struts.taglib.TagUtils.lookup(TagUtils.java:915)
    at org.apache.struts.taglib.html.BaseFieldTag.prepareValue(BaseFieldTag.java:126)
    at org.apache.struts.taglib.html.BaseFieldTag.renderInputElement(BaseFieldTag.java:102)
    at org.apache.struts.taglib.html.BaseFieldTag.doStartTag(BaseFieldTag.java:80)
    at org.apache.jsp.login_jsp._jspx_meth_html_005ftext_005f1(login_jsp.java:1095)
    at org.apache.jsp.login_jsp._jspx_meth_html_005fform_005f1(login_jsp.java:1040)
    at org.apache.jsp.login_jsp._jspService(login_jsp.java:759)
    at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)

在struts-config.xml文件中使用了tage:

<form-bean name="sendContactForm" type="org.apache.struts.validator.DynaValidatorForm">
            <form-property name="firstname" type="java.lang.String" initial="firstname"/>
            <form-property name="lastname" type="java.lang.String" initial="lastname"/>
            <form-property name="emailaddress" type="java.lang.String" initial="email"/>
            <form-property name="subject" type="java.lang.String" initial="subject"/>
            <form-property name="comments" type="java.lang.String" initial="comments"/>
        </form-bean>

还有:

<action path="/sendContactForm" attribute="sendContactForm" input="/login.jsp"
            name="sendContactForm" scope="request"  parameter="reqCode"
            type="org.springframework.web.struts.DelegatingActionProxy" validate="true">
            <forward name="sendcontacts" path="/login.jsp"/>
        </action>

在 Actionform 中我有:

public class ContactAction extends DynaValidatorActionForm {

    private static Logger log = Logger.getLogger(ContactAction.class);


public ActionForward sendContactForm(ActionMapping mapping, ActionForm form,
            HttpServletRequest req, HttpServletResponse resp) throws Exception {
            log.debug("ContactForm--start");


DynaValidatorActionForm sendContactForm = (DynaValidatorActionForm) form;
ActionMessages messages = new ActionMessages();

String firstName = ((String) sendContactForm.get("firstname"));
String lastName = ((String) sendContactForm.get("lastname"));
String emailAddress = ((String) sendContactForm.get("emailaddress"));
String subject = ((String) sendContactForm.get("subject"));
String comments = ((String) sendContactForm.get("comments"));
return mapping.findForward("sendcontacts");

在我的jsp文件中:

    <html:form action="/sendContactForm.do?ContactCd=sendContactForm" method="post" styleId="sendContactForm">
    <c:set var="sendContactForm" value="${sendContactForm}" />

    <html:errors/>           



        <label for="firstname">First Name  <span class="asterisk">*</span>
        </label>
            <html:text styleId="firstname" property="firstname" styleClass="form-control tip required"  name="sendContactForm" />



        <label for="lastname">Last Name  <span class="asterisk">*</span>
        </label>
        <html:text styleId="lastname" property="lastname" styleClass="form-control tip pplaceholder" name="sendContactForm"/>



        <label for="emailaddress">Email Address  <span class="asterisk">*</span>
        </label>
        <html:text styleId="emailaddress" property="emailaddress" styleClass="form-control tip pplaceholder"  name="sendContactForm" />


        <label for="subject">Subject  <span class="asterisk">*</span>
        </label>
        <html:text styleId="subject" property="subject" styleClass="form-control tip pplaceholder" name="sendContactForm"/>

             <label for="comments">Comments  <span class="asterisk">*</span>
        </label>
        <html:textarea styleId="comments" property="comments" styleClass="form-control tip pplaceholder" name="sendContactForm"></html:textarea>

</html:form>

我研究了很多,但还没有成功。

那么,怎么了?谢谢。

【问题讨论】:

    标签: java spring jsp struts


    【解决方案1】:

    我不太习惯DynaActionForms,一直用ActionForm

    出于好奇,如果您将表单定义中的firstname 更改为firstName 以及您所指的所有其他地方是否有效?

    【讨论】:

      【解决方案2】:

      主要更新:

      我用我在此处提到的更改尝试了您的代码,并且所有这五个字段的登录页面都显示为它们在 form-b​​ean 定义中给出的默认初始值。显示页面没有错误。

      这里要注意的另一件事是,您的 login.jsp 没有提交按钮,而且您并未全部提交表单,因此控件永远不会出现在您的操作类中。

      这是 login.jsp

      <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
          pageEncoding="ISO-8859-1"%>
      <%@ taglib uri="http://struts.apache.org/tags-html" prefix="html"%>
      <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
      <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
      <html>
      <head>
      <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
      <title>Login Form</title>
      </head>
      <body>
      <html:form action="/sendContactForm.do?ContactCd=sendContactForm"
              method="post" styleId="sendContactForm">
              <c:set var="sendContactForm" value="${sendContactForm}" />
      
              <html:errors />
      
      
      
              <label for="firstname">First Name <span class="asterisk">*</span>
              </label>
              <html:text styleId="firstname" property="firstname"
                  styleClass="form-control tip required" name="sendContactForm" />
      
      
      
              <label for="lastname">Last Name <span class="asterisk">*</span>
              </label>
              <html:text styleId="lastname" property="lastname"
                  styleClass="form-control tip pplaceholder" name="sendContactForm" />
      
      
      
              <label for="emailaddress">Email Address <span class="asterisk">*</span>
              </label>
              <html:text styleId="emailaddress" property="emailaddress"
                  styleClass="form-control tip pplaceholder" name="sendContactForm" />
      
      
              <label for="subject">Subject <span class="asterisk">*</span>
              </label>
              <html:text styleId="subject" property="subject"
                  styleClass="form-control tip pplaceholder" name="sendContactForm" />
      
              <label for="comments">Comments <span class="asterisk">*</span>
              </label>
              <html:textarea styleId="comments" property="comments"
                  styleClass="form-control tip pplaceholder" name="sendContactForm"></html:textarea>
      
          </html:form>
      
      
      </body>
      </html>
      

      使用的罐子是

      struts-taglib-1.3.10.jar,struts-core-1.3.10.jar,jstl-1.2.jar,commons-validator-1.3.1.jar, commons-logging-1.0.4.jar,commons-digester-1.8.jar,commons-chain-1.2.jar,commons-beanutils-1.8.0.jar,antlr-2.7.2.jar

      以下 tld 被放入我的 WEB-INF

      struts-bean.tld, struts-html.tld,struts-logic.tld

      如果您有任何验证,validation.xml 也可能出现在 WEB-INF 中

      ContactAction.java [在您当前的情况下,控制权不会出现在这里]

      public class ContactAction extends Action {
      
          @Override
          public ActionForward execute(ActionMapping mapping, ActionForm form,
                  HttpServletRequest req, HttpServletResponse resp) throws Exception {
      
              DynaValidatorForm sendContactForm = (DynaValidatorForm) form;
              ActionMessages messages = new ActionMessages();
      
              String firstName = ((String) sendContactForm.get("firstname"));
              String lastName = ((String) sendContactForm.get("lastname"));
              String emailAddress = ((String) sendContactForm.get("emailaddress"));
              String subject = ((String) sendContactForm.get("subject"));
              String comments = ((String) sendContactForm.get("comments"));
              return mapping.findForward("sendcontacts");
          }
      }
      

      以前的编辑 sendContactForm.get("firstname") 的用法是正确的,因为您使用的是 DynaValidatorForm

      错误是您的操作类扩展了错误的类。

      public class ContactAction extends DynaValidatorActionForm
      

      这是错误的。您需要扩展 Action 类 即:

      public class ContactAction extends Action
      

      另外,代替

      DynaValidatorActionForm sendContactForm = (DynaValidatorActionForm) form;
      

      使用

      DynaValidatorForm sendContactForm = (DynaValidatorForm) form;
      

      更新: 在你的动作课中,

      这个方法名称是什么 - sendContactForm ?这有什么原因吗?

      public ActionForward sendContactForm 
      

      你为什么不把它改成 public ActionForward execute

      【讨论】:

      • 我也试过这个。没有解决问题。在浏览器中,它停止在 emailaddress 之后显示字段,但在控制台中它说 No getter method for property: "firstname"!!
      • 默认会调用getFirstname()。当您将调试点放入 actin 类时,您是否看到预期的内容?
      • 我会尽力让你知道
      • 不需要将 tld 放在 WEB-INF 中,因为我们使用 struts-taglib-1.3.10.jar 作为依赖 jar
      【解决方案3】:

      好的。有你的问题。

      试试这个。

      而不是使用“String firstName = ((String) sendContactForm.get("firstname"));”

      使用这个

      String firstName = req.getParameter("firstName"));

      这将解决您的问题。试试看。

      【讨论】:

        【解决方案4】:

        我在这里在黑暗中拍摄(对struts不太了解)但似乎你正在从某个bean调用getter函数[((String)sendContactForm.get(“firstname”));]它是给出错误没有getter函数。 那么你是否为“名字”创建了 getter 函数?

        【讨论】:

        • 您是否保存了从 JSP 获得的数据?尝试使用“sendContactForm.getParameter()”代替“sendContactForm.Get()”。
        • 或者你可能缺少我说的 Bean 文件。像这样的东西。公共类 UploadForm 扩展 ActionForm { protected String myText;受保护的 FormFile myFile; public void setMyText(String text) { myText = text; } public String getMyText() { return myText; } public void setMyFile(FormFile file) { myFile = file; } public FormFile getMyFile() { return myFile; } }
        • 我不保存数据。我要测试那个类。
        • 我测试过,但没有成功。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-02-06
        • 1970-01-01
        • 2014-09-14
        • 2016-02-17
        相关资源
        最近更新 更多