我对在 java 中使用逻辑运算符感到困惑。我使用 struts 1 进行字段验证。这是我的代码:
ActionErrors errors = new ActionErrors();
if(StringUtils.isBlank(getCode()) || StringUtils.isBlank(getNewCode())){
errors.add("code", new ActionMessage("error.codeMode.required"));
errors.add("codeNew", new ActionMessage("error.codeMode.required"));
}
如果 code 字段不为空且 newCode 字段为空,则会出现 code 字段的错误消息(即使不为空),与 newCode 字段相同。我想知道是否有一些解决方案可以避免运算符 OR 的组合。(true || false)
这就是我要搜索的内容:(0=false 1=true)
0 || 0 => 0
1 || 0 => 0
0 || 1 => 0
1 || 1 => 1
最佳答案
不熟悉这项技术,但来自
从逻辑的角度来看,这对我来说更有意义。
if(StringUtils.isBlank(getCode())){
errors.add("code", new ActionMessage("error.codeMode.required"));
}
if(StringUtils.isBlank(getNewCode())){
errors.add("codeNew", new ActionMessage("error.codeMode.required"));
}
不确定这是否是您需要的,只是猜测。
关于java - java 中的逻辑运算符(使用 Struts 1 的验证),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22675496/