var ...">

How to use dynamic css in jsp file base for parameter value

I have a jsp file for example:

<html>
<head>
     <script type="text/javascript">
       var type=<bean:write name="class" property="type" />  
     </script> 

     <style type="text/css">
        .td-type1
        {
            width: 10mm;
        }
        .td-type2
        {
            width: 20mm;
        }
    </style>
</head>
<body>
    <table>
        <tr>
            <td class="td-type1">
            </td>
        </tr>
    </table>
</body>
</html>

      

My question is, how do I change the css dynamically based on the value of the type? For example, if the type is 2, then the css class td-type2 should be used for the td tag. should i use a .properties file to save all config or multi css files or ...?

+3


source to share


1 answer


You can add request attribute value to attribute class

in JSP:

<td class="td-type<%=type%>">

      

As a side note, the use of scriptlet (java code in JSP) is strongly discouraged. Use JSTL and EL instead. In this question, you will learn Why and how to avoid Java code in JSP files .



<td class="td-type${type}">

      

Or, if you want to implement an if-else construct, for example:

<c:choose>
    <c:when test="${type eq "2"}">
        <c:set var="varclass" value="td-type2"/>
    </c:when>
    <c:otherwise>
        <c:set var="varclass" value="td-type1"/>
    </c:otherwise>
</c:choose>
<td class="${varClass}">

      

+7


source







All Articles