javax.el.PropertyNotFoundException: using JSTL in JSP [duplicate]
Asked Answered
A

3

10

I have a JSP where I'm trying to use JSTL tags to display data from an in-memory instance of a class. The data consists of a series of Strings where each String is the address of an RSS feed.

In the JSP, I have the following code:

<table border = "1">
    <tr>
        <c:forEach var = "rssFeedURL" items = "${rssfom.rssFeedURLs}">
            <td align = "left">${rssFeedURL}</td>
        </c:forEach>
    </tr>
</table>

Basically, rssfom is an instance of the following class:

public class RSSFeedOccurrenceMiner extends RSSFeedMiner {

   private HashMap<String, Counter> keywordFrequencies;

   public RSS_Feed_OccurrenceMiner() {
      super();
      this.keywordFrequencies = new HashMap();
   }
   ...
}

This inherits from class RSSFeedMiner which contains the following variable and methods:

private ArrayList<String> rssFeedURLs;

public ArrayList<String> getRSSFeedURLs() {
    return rssFeedURLs;
}

public void setRSSFeedURLs(ArrayList<String> rssFeedURLs) {
    this.rssFeedURLs = rssFeedURLs;
}

So in the JSP, I thought I would be able to use the code above but when the page is run, I simply receive an empty table. And in the server logs, I tend to find message:

javax.el.PropertyNotFoundException: Property 'rssFeedURLs' not found on type RSSFeedOccurrenceMiner

Which is correct given my use of inheritance. So can anyone tell me if JSTL allows inheritance or is there something missing in my code?

I really don't want to use a scriptlet in the JSP.

Alcaraz answered 7/11, 2010 at 16:36 Comment(0)
D
23

Your getter method doesn't follow the JavaBeans naming convention. It should be named getRssFeedURLs (even if you have an acronym, it should be capitalized like a regular word). In EL, when you specify a property name, it actually ends up calling the getter for that property. To figure out the name of the getter, it capitalizes the first letter in the property name that you have provided (so rssFeedURLs gets converted to RssFeedURLs) and tacks on get to the front of it. So you end up with getRssFeedURLs. However, you have named your method as getRSSFeedURLs. Java can't find the method and so you get a PropertyNotFoundException exception.

If you don't name your getters right, you cannot access them with EL.

Dixiedixieland answered 7/11, 2010 at 16:40 Comment(6)
True, I renamed the method slightly but do not see this as being the fault in this case.Alcaraz
@Mr Morgan, did you rename the method to getRssFeedURLs and try it?Dixiedixieland
@Vivin Palaith: I am in your debt. Renaming the method to the Java convention worked perfectly. I have had issues with JSTL before but never like this. Many thanks.Alcaraz
Many thanks again. This would have taken me hours to resolve.Alcaraz
Oh nice. Another drive-by-downvoting.Dixiedixieland
The JavaBeans naming convention supports capitalized names, like getRSSFeedURLs(). His method is named correctly, but he used the wrong EL. It should have been RSSFeedURLs instead of rssFeedURLs.Jaquith
G
11

If the property name starts with two or more subsequent capitals, then it should be accessed like that in EL as well. So, to access the getter getRSSFeedURLs() you need ${rssfom.RSSFeedURLs}.

That's specified in JavaBeans Spec as well.

8.8 Capitalization of inferred names.

When we use design patterns to infer a property or event name, we need to decide what rules to follow for capitalizing the inferred name. If we extract the name from the middle of a normal mixedCase style Java name then the name will, by default, begin with a capital letter. Java programmers are accustomed to having normal identifiers start with lower case letters. Vigorous reviewer input has convinced us that we should follow this same conventional rule for property and event names.

Thus when we extract a property or event name from the middle of an existing Java name, we normally convert the first character to lower case. However to support the occasional use of all upper-case names, we check if the first two characters of the name are both upper case and if so leave it alone. So for example,

“FooBah” becomes “fooBah”
“Z” becomes “z”
“URL” becomes “URL”

We provide a method Introspector.decapitalize which implements this conversion rule.

The JSP EL (Expression Language, those ${} things) adheres the JavaBeans Spec. This is thus not specifically related to JSTL (those <c:xxx> tags).

Goodly answered 7/11, 2010 at 17:19 Comment(3)
The property in java xZone has getter getXZone()... but access in jsp el expression is foo.XZone it's why its better avoiding properties with only one letter decapitalized at the first position.Goa
@Jesus: EL doesn't look at name of field but infers name from getter. The field is totally ignored by EL. See also a.o. https://mcmap.net/q/17708/-javax-el-propertynotfoundexception-property-39-foo-39-not-found-on-type-com-example-beanGoodly
Yes, what I tried to be noted is that el infers name from getter so infers "XZone" as the property name, then you have the property named xZone in java althought XZone in jsp when actualy are the same property, so it may induce posible errorsGoa
P
-1

My VO has a following code

 public class DocumentPolicyVO {
          @JsonProperty("Id")
            private String Id;
            @JsonProperty("Id")
            public String getId() {
                return Id;
            }
     @JsonProperty("Id")
            public void setId(String Id) {
                this.Id = Id;
            }
    }

When I am trying to access it in a jsp page as below, it is giving following error javax.el.PropertyNotFoundException: Property 'Id' not found on type DocumentPolicyVO

<select name="settingsListExcludingEnvironmentList" class="selectComboboxCheck">
                                                  <c:forEach var="settingsType" items="${settingsListExcludingEnvironmentList}">
                                                      <option value="${settingsType.Id}">${settingsType.Name}</option>
                                                  </c:forEach>
                                        </select>

Please anybody can explain the reason.

Panjabi answered 9/6, 2016 at 6:11 Comment(1)
If you have another question, please ask it by clicking the Ask Question button.Lorraine

© 2022 - 2024 — McMap. All rights reserved.