java中的逻辑运算符(使用Struts 1验证)
我对在java中使用逻辑运算符感到困惑。我使用Struts1进行字段验证。这是我的密码:java中的逻辑运算符(使用Struts 1验证),java,operators,struts-1,logical-operators,Java,Operators,Struts 1,Logical Operators,我对在java中使用逻辑运算符感到困惑。我使用Struts1进行字段验证。这是我的密码: ActionErrors errors = new ActionErrors(); if(StringUtils.isBlank(getCode()) || StringUtils.isBlank(getNewCode())){ errors.add("code", new ActionMessage("error.codeMode.required"));
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”也是如此。我想知道是否有避免运算符或的组合的解决方案。(真| |假)
这就是我要搜索的:(0=false 1=true)
不熟悉此技术,但来自
从逻辑角度看,这对我来说更有意义
if(StringUtils.isBlank(getCode())){
errors.add("code", new ActionMessage("error.codeMode.required"));
}
if(StringUtils.isBlank(getNewCode())){
errors.add("codeNew", new ActionMessage("error.codeMode.required"));
}
不确定这是否是您所需要的,只是猜测。我找到了解决方案,即使它不完全符合逻辑,但它仍然有效:(只需添加AND) 第二个问题是peter.petrov建议我的:
if(StringUtils.isBlank(getCode())){
errors.add("code", new ActionMessage("error.codeMode.required"));
}
if(StringUtils.isBlank(getNewCode())){
errors.add("codeNew", new ActionMessage("error.codeMode.required"));
}
如果有人有另一个对我更有帮助的解决方案。没有“其他解决方案”。如果将|
放入,则如果只有一个字段为空,则会添加两个错误。如果你把&&
放进去,那么当两个字段都是emtpy时,两个错误都会被添加,但是如果只有一个字段是空的,那么就不会添加任何内容(这也不是你想要的)
唯一的解决方案是peter.petrov建议的好吧,您的代码说,如果条件为真(整个情况),那么添加两条消息。计算机是相当愚蠢的。他们不读心。所以它不知道您的意思是“仅在适当的情况下添加第一条消息”和“仅在适当的情况下添加第二条消息”。你让它两者都做,它两者都做。是的,我完全同意你。我会更清楚地编辑我的帖子是的,谢谢你,但我知道我能做到,我只是更喜欢找到另一个解决方案(因为我有其他的IF)是的,我知道,但它是有效的,因为“code”和“codeNew”,这些字段中的每一个都在不同的页面中(因此,即使添加了两条错误消息,页面中也只显示一条)但我更喜欢使用peter.petrov的解决方案,即使这不是一个更好的解决方案
if(StringUtils.isBlank(getCode()) && StringUtils.isBlank(getNewCode())){ errors.add("code", new ActionMessage("error.codeMode.required")); errors.add("codeNew", new ActionMessage("error.codeMode.required")); }
if(StringUtils.isBlank(getCode())){
errors.add("code", new ActionMessage("error.codeMode.required"));
}
if(StringUtils.isBlank(getNewCode())){
errors.add("codeNew", new ActionMessage("error.codeMode.required"));
}