问题描述
我是Java,JSTL,CSS,JSP的新手... Java和Web应用程序世界中的任何一个.并且我正在同时学习和制作自己的Web程序(使用Spring MVC).
I am new to Java, JSTL, CSS, JSP... any of Java related and web application world.and I am learning and making my own web program at the same time (using Spring MVC).
现在,我将在.jsp文件中使用JSTL根据条件设置2种不同的背景颜色.我的条件是中位数.所以带有jstl的jsp文件中的逻辑应该是这样的:
Now I am going to set 2 different background color by condition using JSTL in .jsp file. My codition is median. so logic in jsp file with jstl should be like this:
if (value < median)
// set background: green
else
// set background: red
我已经完成了Controller中中值的所有计算.所以我的控制器提供中位数以及数据的整数类型和字符串类型
I have done all the calculations for median value in Controller. So my controller delivers median and both the integer type and the string type of data
((我注意到浏览器页面上无法显示整数类型的数据,对吗?[Q1]
(I noticed that integer type of data can't be presented on a browser page, correct? [Q1]
因此,带条件类型的整数类型用于条件运算,其中值用于字符串,字符串类型用于在浏览器上显示
So Integer type for conditional operation with median, string type for presentation on a browser)
model.addAttribute("dataNo", dataNo);
model.addAttribute("dataStr", dataStr);
model.addAttribute("dataInt", dataInt);
model.addAttribute("median", median);
我的.像这样的jsp
My. jsp like this
(我的桌子越来越多)
<table class="table table-bordered">
<tbody>
<tr>
<c:forEach var="dataNoValue" items="${dataNo}">
<th>${dataNoValue}</th>
</c:forEach>
</tr>
<tr>
<c:forEach var="dataStrValue" items="${dataStr}">
<th class="${dataInt < median ? 'background-color: green':'background-color: red'}">${dataStrValue}</th> -- [Q2]
</c:forEach>
</tr>
</tbody>
</table>
是的,[Q2]是错误的,有什么建议吗?感谢!
Yes, [Q2] is wrong, any suggestions? appreciated!
推荐答案
您应该用样式属性替换类属性,如下所示:
You should either replace class attribute by style attribute like this:
<th style="${dataInt < median ? 'background-color: green':'background-color: red'}">${dataStrValue}</th>
或创建绿色和红色两个类,并像这样使用它们:
or create two classes green and red and use them like this :
<style>
.green{background-color: green}
.red{background-color: red}
</style>
<th class="${dataInt < median ? 'green':'red'}">${dataStrValue}</th>
这篇关于JSTL-根据条件设置背景色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!