好久没写SSH了,最近写了一个登录发现好多问题,请大侠赐教!
action部分:public String login() throws Exception{
System.out.print(users.getUname());
System.out.print(users.getUpwd());
try {
Users loginUser=loginservice.login(users.getUname(),users.getUpwd());
if(loginUser!=null){
ActionContext context=ActionContext.getContext();
context.getSession().put("LOGINUSER", loginUser);
return SUCCESS;
}
} catch (Exception e) {
log.error("登录查询失败", e);
return ERROR;
}

return INPUT;
}service部分:public Users login(String uname,String upass){
List list=usersDAO.findByUname(uname);

if(list==null ||list.size()==0){
return null;
}
Users users=(Users)usersDAO.findByUname(uname).get(0);

if(users==null){
return null;
}

if(uname.equals(users.getUname())&&upass.equals(users.getUpwd())){
return users;
}else{
return null;
}
}
dao部分:public List findByUname(Object uname) {
return findByProperty(UNAME, uname);
}public List findByProperty(String propertyName, Object value) {
log.debug("finding Users instance with property: " + propertyName
+ ", value: " + value);
try {
String queryString = "from Users as model where model."
+ propertyName + "= ?";
return getHibernateTemplate().find(queryString, value);
} catch (RuntimeException re) {
log.error("find by property name failed", re);
throw re;
}
}问题是,加断点调试。走完这些方法就回到action里的return ERROR

解决方案 »

  1.   

    public List findByProperty(String propertyName, Object value) {
            log.debug("finding Users instance with property: " + propertyName
                    + ", value: " + value);
            try {
                String queryString = "from Users as model where model."
                        + propertyName + "= ?";
                return getHibernateTemplate().find(queryString, value);        } catch (RuntimeException re) {
                log.error("find by property name failed", re);
                throw re;
            }
        }
    提示是空指针。请大侠赐教,谢谢,在线等..........
      

  2.   

       这个可能是你的daoimpl里出了问题,你换种方式试试.
      

  3.   

    daoimpl是myeclipse8.6自己生成的。
      

  4.   

    把你的queryString打印出来看看。
      

  5.   

    from Users as model where model.uname= ?
      

  6.   

    先检查你的参数传到daoimpl没有,数据库是否有对应的记录存在,检查查询结果是否为空. 这是我的理解,希望对你有帮助,不过一般daoimpl都是自己写的吧.
      

  7.   

    value也有我从前台输入的用户名!
      

  8.   

    String hql = "from User u where u.uname=:uname";
    Query query = getSession().createQuery(hql);
    query.setParameter("uname", uname);
    user = (User) query.uniqueResult();
    return user;
      

  9.   

    您的意思是让我把整个自动生成的dao全部删除了自己写?
      

  10.   

    http://www.iteye.com/problems/38304
    你的问题很奇怪,应该是配置问题?
      

  11.   

    我已经把所有的DAO都改成Dao了,配置文件也改了。
    目测,配置文件没有报错。
      

  12.   

    applicationContext.xml  如下:<?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="
    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
    <bean id="dataSource"
    class="org.apache.commons.dbcp.BasicDataSource">
    <property name="driverClassName"
    value="com.mysql.jdbc.Driver">
    </property>
    <property name="url"
    value="jdbc:mysql://localhost:3306/redwooddb">
    </property>
    <property name="username" value="root"></property>
    <property name="password" value="123654"></property>
    </bean>
    <bean id="sessionFactory"
    class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
    <property name="dataSource">
    <ref bean="dataSource" />
    </property>
    <property name="hibernateProperties">
    <props>
    <prop key="hibernate.dialect">
    org.hibernate.dialect.MySQLDialect
    </prop>
    </props>
    </property>
    <property name="mappingResources">
    <list>
    <value>com/foraise/entity/Furnituretype.hbm.xml</value>
    <value>com/foraise/entity/Redwoodparameter.hbm.xml</value>
    <value>com/foraise/entity/Images.hbm.xml</value>
    <value>com/foraise/entity/News.hbm.xml</value>
    <value>com/foraise/entity/Users.hbm.xml</value></list>
    </property></bean>
    <bean id="FurnituretypeDao"
    class="com.foraise.dao.impl.FurnituretypeDao">
    <property name="sessionFactory">
    <ref bean="sessionFactory" />
    </property>
    </bean>
    <bean id="RedwoodparameterDao"
    class="com.foraise.dao.impl.RedwoodparameterDao">
    <property name="sessionFactory">
    <ref bean="sessionFactory" />
    </property>
    </bean>
    <bean id="ImagesDao" class="com.foraise.dao.impl.ImagesDao">
    <property name="sessionFactory">
    <ref bean="sessionFactory" />
    </property>
    </bean>
    <bean id="NewsDao" class="com.foraise.dao.impl.NewsDao">
    <property name="sessionFactory">
    <ref bean="sessionFactory" />
    </property>
    </bean>
    <bean id="UsersDao" class="com.foraise.dao.impl.UsersDao">
    <property name="sessionFactory">
    <ref bean="sessionFactory" />
    </property>
    </bean>
    <bean id="LoginService" class="com.foraise.service.impl.LoginService">
    <property name="usersDao" ref="UsersDao"/>
    </bean>
    <bean id="LoginAction" class="com.foraise.action.LoginAction">
    <property name="loginservice" ref="LoginService"/>
    </bean>

    <!-- 配置事务管理器 -->
    <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
        <property name="sessionFactory">
    <ref bean="sessionFactory" />
    </property>    
    </bean>

    <!--配置事务的传播特性-->
        <tx:advice id="txAdvice" transaction-manager="transactionManager">
      <tx:attributes>
    <tx:method name="save*" propagation="REQUIRED"/>
    <tx:method name="update*" propagation="REQUIRED"/>
    <tx:method name="delete*" propagation="REQUIRED"/>
    <tx:method name="add*" propagation="REQUIRED"/>
    <tx:method name="del*" propagation="REQUIRED"/>
    <tx:method name="*" read-only="true"/>
      </tx:attributes>
        </tx:advice>

    <!-- 哪些类的哪些方法参与事务 -->
        <aop:config>
    <aop:pointcut id="allServiceMethod" expression="execution(* com.foraise.service.impl.*.*(..))"/>
    <aop:advisor pointcut-ref="allServiceMethod" advice-ref="txAdvice"/>
        </aop:config>

    </beans>
      

  13.   

    楼主把spring的配置文件粘出来,可能是bean没有配置对的问题。
      

  14.   


    <!-- 配置DAO组件的父模板 -->
    <bean id="daoTemplate" abstract="true">
    <!-- 注入sessionFactory引用 -->
    <property name="sessionFactory" ref="sessionFactory"/>
    </bean>
    <!-- 流水号 -->
    <bean id="sequenceDao" parent="daoTemplate" class="com.cms.dao.impl.SequenceDaoImpl" />
    下班了,你试下?
      

  15.   

    <bean id="UsersDao" class="com.foraise.dao.impl.UsersDao">
            <property name="sessionFactory">
                <ref bean="sessionFactory" />
            </property>
        </bean>id="UsersDao"   改成小写  id="usersDao" 再试试。