第一种:写通用的函数:
public static boolean isNull(Object object) {
if(object == null || "".equals(object) || "null".equals(object)) {
return true;
} else {
return false;
}
}
public static boolean isNull(String str) {
if(str == null || "".equals(str) || "null".equals(str)) {
return true;
} else {
return false;
}
}
String add_weirflow_totalflows2=request.getParameter("add_weirflow_totalflow");
Float add_weirflow_totalflow=null;
if(!isNull(stcd)){
sql.append("select a.* from dt_temper a where not exists(select 1 from dt_temper where stcd = a.stcd and tm > a.tm) and a.stcd='"+stcd+"' order by a.tm desc");
}else{}
else{}不一定要,设置默认值可以用else{}
第二种:在if条件里直接判断是不是空字符串
String add_weirflow_totalflows2=request.getParameter("add_weirflow_totalflow");
Float add_weirflow_totalflow=null;
if(null == add_weirflow_weirflows2 | add_weirflow_weirflows2== ""){
add_weirflow_weirflow = Float.valueOf(0);}else{
add_weirflow_weirflow = Float.valueOf(add_weirflow_weirflows2);
}
eg.
jsp:
<td style="width:45px;height: 10%;">
<label>工程项目编号:</label>
</td><td style="width: 160px;height: 10%;">
<select id="pstcd" name="pstcd">
<option value=" " selected="selected">请选择</option>//选默认值得时候如果value=" ",则先用trim()去掉空格,再判断isNull()
<c:forEach items="${sessionScope.gysgcxm}" var="item2">
<option value="${item2.pstcd}">${item2.pstcd}</option>
</c:forEach>
</td>
controller:
public ModelAndView projectbudget(@RequestParam(value = "pstcd",required = false,defaultValue = "") String pstcd,
//spring mvc里String pstcd=request.getParameter("pstcd");这种取值还无效对value=" "来说,所以最后还是用了 @RequestParam(value = "pstcd",required = false,defaultValue = "")
String pstcd,
String pstcd2="";
if(!isNull(pstcd.trim())){
queryParams1.put("pstcd",pstcd);
}
else
{
queryParams1.put("pstcd",pstcd2);
}
以上两种根据实际情况选择就可以了