简体   繁体   English

有没有更好的方法来检查 HttpServletRequest.getParamter() 中的 null

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

Can there a better/optimized way to extract value from request ?有没有更好/优化的方法来从request提取价值?

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

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

If you could use Java-8, you can use Optional.ofNullable as :如果你可以使用 Java-8,你可以使用Optional.ofNullable作为:

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

or alternatively the suggestion from ernest_k in comments :或者ernest_k 在评论中建议

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM