簡體   English   中英

有沒有更好的方法來檢查 HttpServletRequest.getParamter() 中的 null

[英]Is there a better way to check for null in HttpServletRequest.getParamter()

有沒有更好/優化的方法來從request提取價值?

void m() {
    String tFlag = request.getParameter("tFlag");

    tFlag = (tFlag == null) ? "" : tFlag;
}

如果你可以使用 Java-8,你可以使用Optional.ofNullable作為:

String tFlag = Optional.ofNullable(request.getParameter("tFlag")).orElse("");

或者ernest_k 在評論中建議

String tFlag = request.getParameterMap().getOrDefault("tFlag", "")

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM