1

I am trying to access to an interface of constants from my jsp but it shows the following error.

Caused by: tag 'select', field 'list', name 'title': 
The requested list key 'Constants.TITLE' 
could not be resolved as a collection/array/map/enumeration/iterator type. 
Example: people or people.{name} - [unknown location]

My interface

public interface Constants {
    public List<String> TITLE = Arrays.asList("Mr","Mrs","Ms","Miss"); 
    // public String[] TITLE = {"MR","MRs"}; << does not work as well
   //public static final String[] TITLE = {"MR","MRs"}; << does not work as well
}

My Jsp Code

...
<%@page import="com.myconstants.Constants" %>
<head>
</head>
<body>
     <s:form>
     <s:select label="title" name="title" list="Constants.TITLE" value=" "/>
     </s:form>
 ....
Daniel Morgan
  • 782
  • 5
  • 15
  • 43

1 Answers1

2

Make a class. Put the list in a class. Use normal OGNL static property access:

<s:select key="title" list="@some.package.Constants@TITLES" />

Would I do it like this? Probably not; harder to I18Nize, harder to find/refactor usages in JSP (depends on IDE). In general I'd recommend exposing data to the view layer via a service/layer in actions.

Dave Newton
  • 158,873
  • 26
  • 254
  • 302
  • 1
    By Service/Layer you mean send the data using an action/controller to the view ? does not it affect the performance of the application as it should send the request to server and receive the response? – Daniel Morgan Feb 12 '13 at 02:42
  • @DanielMorgan Sorry, I assumed the constant was actually a constant in the class (i.e., public static final) which is what the OGNL **static** property access syntax does. I still wouldn't *do* it like that, but that would be the most appropriate if you want it as a constant. But IMO values in interfaces has pretty much been relegated to anti-pattern status. – Dave Newton Feb 12 '13 at 02:46
  • whats the other method that you prefere do you have an example of it ? – Daniel Morgan Feb 12 '13 at 02:52
  • 1
    @DanielMorgan What I said; expose it as something from the action or create a layer that can handle I18N more easily. I suppose you could make a static method that took the locale or something. If I18N isn't a concern maybe it doesn't matter. – Dave Newton Feb 12 '13 at 02:55
  • I see, whats the advantage of retrieving them from action ? – Daniel Morgan Feb 12 '13 at 03:00
  • 1
    @DanielMorgan To avoid having to use clunky static method access, to avoid relying on the view layer to access data directly from its source, to normalize where data comes from, etc. Like I said, it may not matter. – Dave Newton Feb 12 '13 at 03:04