当前位置:编程学习 > JAVA >>

我是新手,为什么程序里没有判断用户名和密码的语句呢

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";
}
我是新手,为什么程序里没有判断用户名和密码的语句呢?哪位能帮忙解释一下里面每一句的作用,多谢! --------------------编程问答--------------------
userAccount = webStore.getUserAccount(userName, password);

在这呢 --------------------编程问答--------------------

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";
// 判断对象是否为空,为空就返回error

Map session = (Map) ActionContext.getContext().get("session");
// 获取session对象
if (session == null) {
return "error";
}
// 如果session对象为空,则返回error

session.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";
}


//你这逻辑不是太清晰 --------------------编程问答--------------------
引用 1 楼 defonds 的回复:
userAccount = webStore.getUserAccount(userName, password);

在这呢

那里面为什么没有判断用户输入的用户名和密码正确与否的语句呢?比如如果正确显示什么,不正确显示什么 --------------------编程问答--------------------
引用 1 楼 defonds 的回复:
userAccount = webStore.getUserAccount(userName, password);

在这呢


引用 2 楼 huxiweng 的回复:

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";
// 判断对象是否为空,为空就返回error

Map session = (Map) ActionContext.getContext().get("session");
// 获取session对象
if (session == null) {
return "error";
}
// 如果session对象为空,则返回error

session.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";
}


//你这逻辑不是太清晰

那里面为什么没有判断用户输入的用户名和密码正确与否的语句呢?比如如果正确显示什么,不正确显示什么  --------------------编程问答--------------------
引用 4 楼 guanhui1999 的回复:
Quote: 引用 1 楼 defonds 的回复:

userAccount = webStore.getUserAccount(userName, password);

在这呢


引用 2 楼 huxiweng 的回复:

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";
// 判断对象是否为空,为空就返回error

Map session = (Map) ActionContext.getContext().get("session");
// 获取session对象
if (session == null) {
return "error";
}
// 如果session对象为空,则返回error

session.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 这个对象。 --------------------编程问答-------------------- userAccount = webStore.getUserAccount(userName, password);

if (userAccount == null)
return "error";

说的很清楚了
userAccount 就是个对象
getUserAccount(userName,password)这个方法对传入的参数进行数据库校验,即查找用户名和密码与参数匹配的 IUserAccount  ,

如果找到了就返回该对象,也就是存在,也就是你输入的数据正确
如果未找到返回null,也就是错误输入

之后提到了如果返回null 就提示错误 error  应该是返回错误或重新登录的页面  否则将该对象放入session,相应页面就可以显示该对象的信息了
  --------------------编程问答-------------------- 回复于: 2013-10-22 21:38:41 

userAccount = webStore.getUserAccount(userName, password);
 
if (userAccount == null)
 return "error";
 
说的很清楚了
 userAccount 就是个对象
 getUserAccount(userName,password)这个方法对传入的参数进行数据库校验,即查找用户名和密码与参数匹配的 IUserAccount  ,
 
如果找到了就返回该对象,也就是存在,也就是你输入的数据正确
 如果未找到返回null,也就是错误输入
 
之后提到了如果返回null 就提示错误 error  应该是返回错误或重新登录的页面  否则将该对象放入session,相应页面就可以显示该对象的信息了 --------------------编程问答--------------------
引用 5 楼 huxiweng 的回复:
Quote: 引用 4 楼 guanhui1999 的回复:

Quote: 引用 1 楼 defonds 的回复:

userAccount = webStore.getUserAccount(userName, password);

在这呢


引用 2 楼 huxiweng 的回复:

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";
// 判断对象是否为空,为空就返回error

Map session = (Map) ActionContext.getContext().get("session");
// 获取session对象
if (session == null) {
return "error";
}
// 如果session对象为空,则返回error

session.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);


引用 5 楼 huxiweng 的回复:
Quote: 引用 4 楼 guanhui1999 的回复:

Quote: 引用 1 楼 defonds 的回复:

userAccount = webStore.getUserAccount(userName, password);

在这呢


引用 2 楼 huxiweng 的回复:

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";
// 判断对象是否为空,为空就返回error

Map session = (Map) ActionContext.getContext().get("session");
// 获取session对象
if (session == null) {
return "error";
}
// 如果session对象为空,则返回error

session.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);

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对象返回给调用者
}
--------------------编程问答-------------------- getUserAccount(userName, password);
这个方法应该是用userName和password去数据库里查这个UserAccount,如果查到了就是验证通过,没查到就是有问题呗。 --------------------编程问答-------------------- 楼主还有待学习,加强自身逻辑和struts2框架的运行模式 --------------------编程问答-------------------- 路过路过 上面解释的很好了 哈哈 --------------------编程问答-------------------- webStore.getUserAccount(userName, password);看这个方法,这个方法里返回的是一个对象userAccount,
接着又对这个对象做了判断if (userAccount == null)
 return "error";, --------------------编程问答-------------------- 路过,LZ 有待磨练
补充:Java ,  Web 开发
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,