gpt4 book ai didi

java - 没有属性的 getter 方法...错误

转载 作者:太空宇宙 更新时间:2023-11-04 15:08:55 26 4
gpt4 key购买 nike

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

我收到此错误:

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 文件中使用标签:

<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>

我进行了很多研究,但尚未成功。

所以,出了什么问题?谢谢。

最佳答案

主要更新:

我用我在这里提到的更改尝试了您的代码,并且包含所有这五个字段的登录页面都显示了 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>

使用的 jar 是

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

以下顶级域名放入我的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;

更新:在你的 Action 类中,

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

public ActionForward sendContactForm 

你为什么不把它改成公共(public) ActionForward 执行

关于java - 没有属性的 getter 方法...错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21593878/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com