日期:2014-05-17  浏览次数:20930 次

一个简单的登陆问题?
IUserDao
Java code

public interface IUserDao {
    
    public User queryByUserNameAndPassword(String userName, String password) throws Exception;

}


UserDao
Java code

public User queryByUserNameAndPassword(String userName, String password)
            throws Exception {
        String hql = "FROM User as u WHERE u.userName=? and u.password=?";

           String[] str = new String[] {userName, password};

           List<User> users = this.getHibernateTemplate().find(hql, str);

           if (users != null && users.size() >= 1) {

               return users.get(1);

           } else {

               return null;

           }
        
    }


IUserService
Java code

 public String isLogin(String userName, String password) throws  Exception;


userService
Java code

public String isLogin(String userName, String password) throws Exception {
         User user = userDao.queryByUserNameAndPassword(userName, password);

           if(user != null) {

               return user.getUserName();

           } else {

               return null;

           }

    }


UserAction
Java code


        public String execute() throws Exception {
          if (isInvalid(user.getUserName())) {

               this.addFieldError("uid", "登录ID不能为空");

               return INPUT;
           }

           if (isInvalid(user.getPassword())) {

               this.addFieldError("password", "密码项不能为空");

               return INPUT;

           }
           String uname = userService.isLogin(user.getUserName(), user.getPassword());

           if (uname != null) {

               Map session = ActionContext.getContext().getSession();

               session.put("uname", uname);

               session.put("uid", user.getUid());

               return SUCCESS;

           } else {

               this.addFieldError("idorpassword", "用户名或密码错误");

               return INPUT;

           }

        }
    private boolean isInvalid(String userName) {
        // TODO Auto-generated method stub
        return false;
    }


不够写了 在下面


------解决方案--------------------
初始化action有错误,也就是说你的action对应累的函数处理有问题,你好好查查
------解决方案--------------------
<action name="login" class="userAction">


<bean id="userAction" class="com.action.UserAction">
<property name="userService" ref="userService"></property>
</bean>

不能这样注入action的吧
------解决方案--------------------
<constant name="struts.objectFactory.spring.autoWire" value="name" />

定义一下这个常量, 让spring自动按名字注入service
------解决方案--------------------
美女,sessionFactory配置错误。
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"&