【问题标题】:how to use dynamic css in jsp file base on param value如何根据参数值在jsp文件中使用动态css
【发布时间】:2013-01-24 10:03:14
【问题描述】:

我有一个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-type2 的 css 类应该用于 td 标签。 我应该使用 .properties 文件来保存所有配置或多 css 文件还是...?

【问题讨论】:

    标签: java javascript css jsp


    【解决方案1】:

    您可以将请求属性的值附加到 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}">
    

    【讨论】:

    • 我使用struts获取类型变量,对你的回答有影响吗?
    • @Mina 我上次使用它已经有很长时间了,但如果我没记错的话,你会有某种形式的 bean 可用作请求属性。应该是一样的,在 EL 表达式中访问 bean 的属性。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-15
    • 2017-05-12
    相关资源
    最近更新 更多