Search code examples
jspstruts2iteratorognl

Using OGNL inside a JSP scripting delimiters


How can I use the OGNL inside a scripting delimiters?

<s:iterator var="arr" value="%{carNames}" status="incr">        
    <option value="<%=car.getType()["#incr.index"]%>" >
        <s:property value="arr"/>
    </option>           
</s:iterator>

This is my Car structure:

private String[] carNames = {"A", "B", "C"};

public static Integer[] getType() {
    return new Integer[]{
        new Integer(Global.DISEL),
        new Integer(Global.TESLA),
        new Integer(Global.HYBRID)
    };
 }

 //getter and setter

Solution

  • In scripting delimiters you can't use OGNL. You should remove scriptlets an replace it with <s:property> tag or use ${}.

    <option value="<s:property value='%{car.type[#incr.index]}'/>">
    

    It shows how to use OGNL in HTML tags.

    If you are working with select tag, I recommend you to read Struts2 select tag - Dynamically add options.