简体   繁体   中英

No Getter Method Error when there is a getter

I am trying to add a checkbox to a jsp to accept terms and conditions before entering the site. I keep getting the same error about not having a getter method even though I do have a getter method and I don't see any typos. I don't understand what I'm missing.

login.jsp

<html:checkbox property="termsCheckbox" value="true"/>

LoginForm.java

public final class LoginForm extends ActionForm
{   
private boolean termsCheckbox = false;

public void setTermsCheckbox(boolean termsCheckbox)
{
    this.termsCheckbox = termsCheckbox;
}

public boolean isTermsCheckbox()
{
    return termsCheckbox;
}
}

This is the error I am getting when I go to the JSP:

org.apache.jasper.JasperException: An exception occurred processing JSP page /login.jsp 
at line 73

73: <html:checkbox property="termsCheckbox" title="terms" value="true"/>    

javax.servlet.ServletException: javax.servlet.jsp.JspException: No getter method 
for property termsCheckbox of bean org.apache.struts.taglib.html.BEAN

It should be isTermsCheckbox:

public boolean isTermsCheckbox(){
    return termsCheckbox;
}

And get your property value like:

<html:checkbox property="isTermsCheckbox" value="true"/>

See the following:

Java: how to name boolean properties

Write you property statement like this:

<html:checkbox property="${form.termsCheckbox}" title="terms" value="true"/>   

Where form is your form LoginForm instance.

I am unsure if JSP is this way but there are certain frameworks that require the explicit name get or set prefix for example, setTermsCheckbox and getTermsCheckbox. Try that and see if it fixes the problem.

First, you need to check the attribute action in your html:form tag in the file login.jsp .

<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html"%>
<html:form action="login">
    <html:checkbox property="termsCheckbox">Terms</html:checkbox>
    <html:submit />
</html:form>

Next, with the action identified, the associated form name ( LoginForm in my case) for this action mapping must be the same with the property termsCheckbox .

<form-beans>
    <form-bean name="LoginForm" type="test.form.LoginForm" />
</form-beans>
<action-mappings>
    <action path="/login" type="test.action.LoginAction" name="LoginForm"
        input="/login.jsp" scope="request"></action>
</action-mappings>

And the code for the action form does not require anything particular.

public final class LoginForm extends ActionForm {

    boolean termsCheckbox;

    public boolean isTermsCheckbox() {
        return termsCheckbox;
    }

    public void setTermsCheckbox(boolean termsCheckbox) {
        this.termsCheckbox = termsCheckbox;
    }

}

Its so much late for the answer now and you may already find the solution for this I would like to provide the solution which worked for me.

As you have mentioned that you have copied the form, please make ensure 2 things:

  1. Your <html:checkbox property = "" /> tag must be within <html:form></html:form> tag
  2. Your <html:form action=""> tag action property should match the <action path=""> property in struts config file.

During coping the existing code there is a possibility that we may forget to apply changes of 2nd point.

Try implement the following on your form:

public boolean getTermsCheckbox()
{
    return termsCheckbox;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM