I want to check if a specific item is selected in a drop-down menu. Suppose I have something like this in my JSP:
<sf:select path="xItem" >
<sf:option value="val1" />
<sf:option value="val2" />
</sf:select>
and in my corresponding java bean, I want to check it as
if (xItem.equals("val1")) xItem = doSomething();
but, if I define “val1” as a constant, i.e., final static String mytext="val1", and change my jsp file as <sf:option value="${MyClass.mytext}" />, Spring will complain that mytext is not a property of MyClass.
Is there anyway to define this text as a constant and refer to it in JSP and the corresponding Java class?
The
${MyClass.mytext}expression applies to beans and properties, specifically it means that the server is expecting to find a bean saved in the current context asMyClassand on that bean to call a getter calledgetMyText(it’s ajspContext.FindAttribute) which isn’t the case here since you have a static member on a class.Here are some workarounds off the top of my head:
I would personally go with defining an EL function if you refer to constants often in your JSPs or set it under some model key in the controller if it’s a one time only use.