if (use.getNewsh() == "政治类新闻") {
// 把当前登录用户存放到session中
getSession().setAttribute("curr_use", use);
result = "SHsucc";
}else{
put2Request("use", "类型异常");
}
这句判断用户喜欢的新闻类型是否为政治类新闻,断点测试use中已经有用户的newsh属性为政治类新闻,但最终结过都是类型异常,newsh是String类型,测试其他String类型也无法判断,求助该如何解决
@Override
public String execute() throws Exception {
String result = "fail";
String real = (String) getSession().getAttribute("real");
if (cc != null && cc.equalsIgnoreCase(real)) { // 验证码正确
User use = userService.findone(lname);
if (use != null) { // 用户名正确
if (use.getPwd().equals(pwd)) { // 密码正确
if (use.getStatus()==1) { // 状态正常
if (use.getNewsh() == "政治类新闻") {
// 把当前登录用户存放到session中
getSession().setAttribute("curr_use", use);
result = "SHsucc";
}else{
put2Request("use", "类型异常");
}
} else {
put2Request("use", "状态异常,请联系系统管理员!");
}
} else {
put2Request("use", "密码有误,请检查!");
}
} else {
put2Request("use", "用户有误,请检查!");
}
} else {
put2Request("use", "验证码有误,请检查!");
}
return result;
}
String中,比较字符串内容是否相等用的是equals,而==主要用来判断引用是否指向堆内存的同一块地址。
当String作为一个对象来使用(如:String str1 = new String("zhangsan"))时:1) 对象不同,内容相同,"=="返回false,equals返回true; 2)同一对象,"=="和equals结果相同;
当String作为一个直接量来使用时,如果值不相同,对象就不相同,所以"==" 和equals结果一样。比如:
String str1 = "zhangsan";
String str2 = "zhangsan";
System.out.println(str1==str2); //true
System.out.println(str1.equals(str2)); //true
希望对你有帮助
用 equals ;equals是比较内容 == 是比较内容,同时比较存储地址
用equals,也可以用StringUtils.equals(str,str)
use.getNewsh().equals("政治类新闻")
if (use.getPwd().equals(pwd)) { // 密码正确 你这下面的代码不是有正确的比较字符串的代码吗?上面的怎么写的是错的?