3

这有效:

String address1 = HtmlUtil.escape(rs.getString("address1"));

...但我不想显示“null”。所以我尝试了这个,它不起作用:

String address1 = HtmlUtil.escape(rs.getString("address1") || "");

使用这个是最好的选择:?

String address1 = HtmlUtil.escape(rs.getString("address1"));
if (address1 == null) address1 = "";
4

7 回答 7

6

使用三元运算符,您可以使用单行代码分配一个值:

String address1 = (rs.getString("address1") != null) ?
                  HtmlUtil.escape(rs.getString("address1")) : "";
于 2012-12-18T09:51:24.230 回答
4
String address1 = HtmlUtil.escape(rs.getString("address1"));
if (address1 == null || address1.equals("null")) {
    address1 = "";
}
于 2012-12-18T09:52:23.697 回答
1
String address1 = HtmlUtil.escape(rs.getString("address1")) == null ? "" : HtmlUtil.escape(rs.getString("address1")); 

如果你想要丑陋的单线:-)

于 2012-12-18T09:51:43.167 回答
1

使用临时变量只调用HtmlUtil.escape一次。然后检查null并分配给address1

String tempAddress =  HtmlUtil.escape(rs.getString("address1"));
String address1 = tempAddress != null ? tempAddress : "";
于 2012-12-18T09:55:23.090 回答
1

解决方案:

 String address1 = HtmlUtil.escape(rs.getString("address1"));
 if (address1 == null) address1 = "";
于 2012-12-18T09:58:39.000 回答
0

尝试这样的事情

<c:when test="${empty rs.getString("address1")}">
  ...
</c:when>
<c:otherwise>
   .....
</c:otherwise>
于 2012-12-18T09:55:17.610 回答
0

你可以试试这个:

String address1 = "";
if(HtmlUtil.escape(rs.getString("address1") != null && !(HtmlUtil.escape(rs.getString("address1").equalsIgnoreCase("null"))){
address1 = HtmlUtil.escape(rs.getString("address1"));
}
于 2012-12-18T12:48:45.417 回答