java - 如何根据参数值在jsp文件中使用动态CSS

标签 java javascript css jsp

我有一个 jsp 文件,像这样:

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

我的问题是:如何根据类型值动态更改 css? 例如,如果 type 等于 2,则 td 标签应使用 td-type2 的 css 类。 我应该使用 .properties 文件来保存所有配置或多 css 文件还是...?

最佳答案

您可以将请求属性的值附加到 JSP 中的 class 属性:

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

作为旁注,强烈建议不要使用 scriptlet(JSP 中的 java 代码)。请改用 JSTL 和 EL。在这个问题中你会发现 Why and how to avoid Java Code in JSP files .

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

或者,如果你想实现一个类似 if-else 的结构,例如:

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

关于java - 如何根据参数值在jsp文件中使用动态CSS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14775958/

相关文章:

php - 如何移动wordpress插件的输出

javascript - wow.js 仅在向上滚动时有效

java - 如何让webdriver等待,它的作用是什么?

java - JBoss 部署错误 : Caused by: java. lang.RuntimeException : . application.xml 中列出的 war 模块在 .ear 中不存在

javascript - 如何仅在未与元素交互时为其设置动画?

javascript - 为什么 SP.js 没有加载?

javascript - 需要有关 AJAX 表单处理小部件的帮助

java - Trove4j TObjectIntHashMap 与 EnumMap<Enum, Integer>

java - 简单的 Android 应用程序即将 OOM

javascript - 从本地文件系统提供的 Webworker 不工作