Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 ...?

like image 267
Mina Avatar asked Jan 15 '23 06:01

Mina


1 Answers

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}">
like image 80
Xavi López Avatar answered Jan 16 '23 22:01

Xavi López