Mina
Mina

Reputation: 139

how to use dynamic css in jsp file base on param value

I have a jsp file ,like this :

<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 to change css dynamically base on type value? For example, if type equal 2, then the css class of td-type2 should be used for td tag. should I use .properties file to save all config or multi css files or ...?

Upvotes: 5

Views: 20561

Answers (1)

Xavi L&#243;pez
Xavi L&#243;pez

Reputation: 27880

You can append the request attribute's value to the class attribute in the JSP :

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

As a side note, the use of scriptlets (java code in JSP's) is strongly discouraged. Use JSTL and EL instead. In this question you'll find out Why and how to avoid Java Code in JSP files.

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

Or, if you wanted to implement an if-else like construct, for instance :

<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}">

Upvotes: 9

Related Questions