public class LoginAction { private ICart cart; private String password; private IUserAccount userAccount; private String userName; private IWebStoreFacade webStore = new WebStorePOJO(); public String execute() { userAccount = webStore.getUserAccount(userName, password); if (userAccount == null)
return "error"; Map session = (Map) ActionContext.getContext().get("session"); if (session == null) {
return "error";
} session.put("userAccount", userAccount); if (getCartFromSession()) {
return (!getOrderFromSession()) ? "success" : "showBillingDetails";
} else if (createCart()) {
return "success";
} else
return "error";
}
我是新手,为什么程序里没有判断用户名和密码的语句呢?哪位能帮忙解释一下里面每一句的作用,多谢!
return "error"; Map session = (Map) ActionContext.getContext().get("session"); if (session == null) {
return "error";
} session.put("userAccount", userAccount); if (getCartFromSession()) {
return (!getOrderFromSession()) ? "success" : "showBillingDetails";
} else if (createCart()) {
return "success";
} else
return "error";
}
我是新手,为什么程序里没有判断用户名和密码的语句呢?哪位能帮忙解释一下里面每一句的作用,多谢!
在这呢
public class LoginAction {private ICart cart;private String password;private IUserAccount userAccount;private String userName;private IWebStoreFacade webStore = new WebStorePOJO();public String execute() {userAccount = webStore.getUserAccount(userName, password);
// 获取一个UserAccunt对象if (userAccount == null)
return "error";
// 判断对象是否为空,为空就返回errorMap session = (Map) ActionContext.getContext().get("session");
// 获取session对象
if (session == null) {
return "error";
}
// 如果session对象为空,则返回errorsession.put("userAccount", userAccount);
// session不为空把获取到的userAccount放入到session中。前台可以从session中获取
if (getCartFromSession()) {
return (!getOrderFromSession()) ? "success" : "showBillingDetails";
// 根据getOrderFromSession的结果,返回true,就跳转到success的对应页面,否则跳转到showBillingDetails对应的页面
} else if (createCart()) {
return "success";
} else
return "error";
}
//你这逻辑不是太清晰
那里面为什么没有判断用户输入的用户名和密码正确与否的语句呢?比如如果正确显示什么,不正确显示什么 应该是在这个方法里面,你看看这方法里的代码吧:
webStore.getUserAccount(userName, password);
userAccount = webStore.getUserAccount(userName, password);这个方法里面应该是service层调用dao层查询用户,如果密码不对的话就返回null
对的话就返回用户userAccount 这个对象。
return "error";说的很清楚了
userAccount 就是个对象
getUserAccount(userName,password)这个方法对传入的参数进行数据库校验,即查找用户名和密码与参数匹配的 IUserAccount ,如果找到了就返回该对象,也就是存在,也就是你输入的数据正确
如果未找到返回null,也就是错误输入之后提到了如果返回null 就提示错误 error 应该是返回错误或重新登录的页面 否则将该对象放入session,相应页面就可以显示该对象的信息了
if (userAccount == null)
return "error";
说的很清楚了
userAccount 就是个对象
getUserAccount(userName,password)这个方法对传入的参数进行数据库校验,即查找用户名和密码与参数匹配的 IUserAccount ,
如果找到了就返回该对象,也就是存在,也就是你输入的数据正确
如果未找到返回null,也就是错误输入
之后提到了如果返回null 就提示错误 error 应该是返回错误或重新登录的页面 否则将该对象放入session,相应页面就可以显示该对象的信息了
那里面为什么没有判断用户输入的用户名和密码正确与否的语句呢?比如如果正确显示什么,不正确显示什么 应该是在这个方法里面,你看看这方法里的代码吧:
webStore.getUserAccount(userName, password);
那里面为什么没有判断用户输入的用户名和密码正确与否的语句呢?比如如果正确显示什么,不正确显示什么 应该是在这个方法里面,你看看这方法里的代码吧:
webStore.getUserAccount(userName, password);
public IUserAccount getUserAccount(String userName, String pwd) { Session session = HibernateUtil.getSessionFactory().getCurrentSession();
IUserAccount account = null; try {
session.beginTransaction(); Query query = session.createQuery("from UserAccount as account "
+ "where account.userName = ? and account.password = ?");
query.setString(0, userName);
query.setString(1, pwd); account = (IUserAccount) query.uniqueResult();
} catch (Exception e) {
e.printStackTrace();
} finally {
session.getTransaction().commit();
}
return account;
}
谢谢你的回答,这方法里面的哪部分是实现用户名和密码验证的,能帮我讲一下吗,刚接触struct好多不懂,多谢
public IUserAccount getUserAccount(String userName, String pwd) {Session session = HibernateUtil.getSessionFactory().getCurrentSession();
IUserAccount account = null;try {
session.beginTransaction();Query query = session.createQuery("from UserAccount as account "
+ "where account.userName = ? and account.password = ?");
// 生成查询数据库对象
query.setString(0, userName);
query.setString(1, pwd);
// 设置查询条件account = (IUserAccount) query.uniqueResult();
// 根据查询条件进行查询并都出结果account对象
} catch (Exception e) {
e.printStackTrace();
} finally {
session.getTransaction().commit();
}
return account;
//把account对象返回给调用者
}
这个方法应该是用userName和password去数据库里查这个UserAccount,如果查到了就是验证通过,没查到就是有问题呗。
接着又对这个对象做了判断if (userAccount == null)
return "error";,