Freemarker template value missing while first or second letter is capitalized

Viewed 160

I am trying to learn how to integrate Spring Boot(2.6.3) with FreeMarker(2.3.31). Everything works fine until I met an FreeMarker error while trying to get the value like ${myObject.pAram!} while the first or second letter of the param is capitalized.

So I did so test work and get the following conclusion:

While param name of an object is first letter or second letter capitalized, FreeMarker couldn`t get the param value. Some code here in case of my poor English:

@Data
public class TestBean {
    String param; //no-capitalized param
    String aParam; //second letter capitalized param
    String Bparam; //first letter capitalized param
    String cpRam;  //third letter capitalized param
}

Here what I do some setter in controller:

@RequestMapping("/test")
public String insure(@PathVariable String module,HttpServletRequest request) {
     TestBean testBean = new TestBean();
     testBean.setParam("param-no capitalize");
     testBean.setAParam("aParam-capitalize the second letter");
     testBean.setBparam("Bparam-capitalize the first letter");
     testBean.setCpAram("cpAram-capitalize the third letter");
     request.setAttribute("testBean",testBean);
     return "test";
    }

And what the test.ftl looks like:

<body>
<body>
<div>${testBean.param!"missing param"}</div>
------------------------------
<div>${testBean.aParam!"missing aParam"}</div>
------------------------------
<div>${testBean.Bparam!"missing Bparam"}</div>
------------------------------
<div>${testBean.cpAram!"missing cpAram"}</div>

However, the final html result is:

param-no capitalize
------------------------------
missing aParam
------------------------------
missing Bparam
------------------------------
cpAram-capitalize the third letter

Since I`m new to FreeMarker and Spring Boot, I failed to figure out why.

Could someone tell me the exact reason? Am I missing some point?

Thanks a lot!

1 Answers

The Java Bean property names are deduced from the getter method names, not from the field names.

For Bparam, Lombok will generate getBparam(), which is the same that you will get for a filed named bparam. So the two cases are indistinguishable, and both will give the property name bparam.

For aParam, Lombok will generate getAParam(), which is the same that you will get for a filed named AParam. So the two cases are indistinguishable, and both will give the property name AParam. Now this last is not intuitive (I would rather expect aParam), but these are the rules of Java Beans (not of FreeMarker). Which, in turn is a consequence of the broken camel case conventions of Java (i.e., that you must keep letters that were originally upper case as upper case, instead of only using upper case for the first letter of words, so Java camel case is not reversible in general).

Related