用的ssh框架完全沒有問題,主要問題是execute()方法中if語句問題。直接上代碼奔主題去啦。
package cn.web.action;
import com.opensymphony.xwork2.ActionSupport;
import cn.web.daoimp.UserDaoImp;
import cn.web.service.Users;
public class UserAction extends ActionSupport {
private String username;
private String password;
public boolean isa;
public boolean isa(){
isa="a".equals(username);
return isa;
}
public boolean isIsa() {
return isa;
}
public void setIsa(boolean isa) {
this.isa = isa;
}
public UserAction(){}
public UserAction(String username,String password){
this.username=username;
this.password=password;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String execute() {
if (("a".equals(username))){
return "success";
}
else {
return "error";
}
}
這是action的主要代碼,就是想實現登陸功能什麼的。但是上面代碼的execute()方法中的if判斷語句有問題。直接提取出來。
public String execute() {
if (("a".equals(username))){
return "success";
}
else {
return "error";
}
}
無論我在前端輸入a還是其他的都會返回“error”。但是當我把if()語句改成這樣
public boolean isa;
public boolean isa(){
isa="a".equals(username);
return isa;
}
public String execute() {
if (isa()){
return "success";
}
else {
return "error";
}
}
改寫之後我就能得到正確的我想要的結果。我不明白這是為什麼,求大神指點。
package Action;
import com.opensymphony.xwork2.ActionSupport;
/**
Created by Administrator on 2016/11/29.
*/
public class netTestAction extends ActionSupport{
private String username;
private String password;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@Override
public String execute() throws Exception {
System.out.println(username + "::::" +password);
if (("a".equals(username))){
return "success";
}
else {
return "error";
}
}
}