动态JSP背景颜色

时间:2014-03-24 16:54:33

标签: css jsp

我有一个提交给JSP的html表单。表单有一个输入,指定背景颜色首选项。在JSP页面上,我尝试使用用户指定的背景颜色,或者,如果他们没有输入任何内容,则使用CSS文件中指定的颜色。这可能吗?

我的jsp中有一个指定CSS文件的位置,所以我知道我可以使用css背景颜色,但如果用户指定颜色,我想忽略'那,并使用他们选择的颜色。

这可能吗?

1 个答案:

答案 0 :(得分:0)

您可以通过<link href...>加载默认的css,然后在页面的<style>块中添加替换。由于style块上定义的样式优先于其他样式,因此它将成功覆盖默认值。你可以看到更多关于CSS&#34; cascades&#34; W3C specs上的样式。

<head>

中尝试这样的事情
<link href="default.css" media="all" rel="stylesheet" />
<style>
    /* Define custom CSS by the user here */
    body{
        background-color: <% ... %>;
    }
</style>

我想有很多方法可以在JSP中生成正确的输出,但使用JSTL Core IF Tag可以执行以下操作:

<style>
    /* Define custom CSS by the user here */
    body{
        <core:if test="${param.backgroundColor!= null}">
            background-color: <%=request.getParameter("backgroundColor")%>
        </core:if>
    }
</style>

这样,如果用户为background-color元素选择了自定义h1,则会覆盖默认值。

相关问题