使用<s:set> - Struts2为偶数行设置颜色

时间:2018-02-19 10:10:40

标签: struts2

我想为表格的偶数行提供动态颜色/样式。下面是我的代码,但它没有给我所需的输出。

<table>
   <s:iterator value="allMonths" status="incr">
   <style>
    .evenRow{background-color: yellow;}
    .oddRow{background-color: orange;}
   </style>
   <tr>

   <s:if test="#incr.count%2 ==0">
    <s:set var="clr" value="evenRow"></s:set>
   </s:if>

   <s:else>
    <s:set var="clr" value="oddRow"></s:set>
   </s:else>

        <td class="%{#clr}">Month:</font> <s:property/></td>
        <%-- <td class="%{clr}">Month:</font> <s:property/></td> --%>
        <%-- <td class="clr">Month:</font> <s:property/></td> --%>
    </tr>
   </s:iterator>
</table>

的index.jsp

    <%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<body>
<s:form action="clickAction">

    <s:textfield name="beanobject.uname" label="Enter Username" /><br>
    <s:textfield name="beanobject.age" label="Enter Age" /><br>
    <s:textfield name="beanobject.sex" label="sex" /><br>
    <s:submit value="Submit" align="center" /> 
    <s:submit value="Clear" align="center" action="clearAction"/>
</s:form>
</body>
</html>

MyAction.java

    ---imports---

public class MyAction extends ActionSupport{
    private MyBean beanobject;
    private List<String> allMonths;

    //Getters & Setters

    public String execute(){    
        System.out.println("execute");
        return SUCCESS;
    }

    public String click()
    {   
        allMonths = new ArrayList<String>();
        allMonths.add("Jan");
        allMonths.add("Feb");
        allMonths.add("Mar");
        return SUCCESS;
    }

    public String clear(){
        return SUCCESS;
    }
}

我也尝试使用“bgcolor”属性,但没有成功。我错过了什么?

提供所需的代码。我只是在ActionClass

列表中硬编码值

1 个答案:

答案 0 :(得分:0)

你假设JSP理解OGNL;它不是。 JSP了解JSP EL。

OGNL严格遵循S2自定义标记,而不是通用JSP。