这是一个基于SSH框架的网站系统其中的登录模块,结果在登录的时候userserviceimpl始终是null
这个是DAO实现
package dao;
import entity.User;
import org.springframework.orm.hibernate5.support.HibernateDaoSupport;
import org.springframework.stereotype.Repository;
import java.util.List;
@Repository
public class UserDAOImpl extends HibernateDaoSupport implements UserDAO {
//增加用户
@Override
public void addUser(User user){
getHibernateTemplate().save(user);
}
//查询验证用户是否存在
@Override
public User findUser(User user){
User user1 = new User();
String hql = "from User user where user.username='"
+ user.getUsername() + "' and user.password= '"
+ user.getPassword() + "'";
//将查询出的结果放到List
List<User> userlist = getHibernateTemplate().findByExample(user);
//判断是否有查询结果,换句话说就是判断用户是否存在
if(userlist.size()>0){
//取出查询结果的第一个值
user1 = userlist.get(0);
}
return user1;
}
}
userService
package service;
import dao.UserDAOImpl;
import entity.User;
import org.springframework.stereotype.Service;
@Service
public class UserServiceImpl implements UserService {
//注入对象UserDAO
private UserDAOImpl userdaoimpl;
public void setUserDAOImpl(UserDAOImpl userdaoimpl) {
this.userdaoimpl = userdaoimpl;
}
//保存用户信息
@Override
public void addUser(User user){
this.userdaoimpl.addUser(user);
}
//查找验证用户信息
@Override
public boolean findUser(User user){
User user1 = this.userdaoimpl.findUser(user);
//DAO查询中已经判断当用户名密码都存在时才返回user1,这里只用判断用户名是否存在就行
if(user1.getUsername()!=null){
return true;
}else{
return false;
}
}
}
LoginAction
package action;
import com.opensymphony.xwork2.ActionSupport;
import entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import service.UserService;
import service.UserServiceImpl;
public class LoginAction extends ActionSupport{
private User user;
public void setUser(){
this.user=user;
}
private UserServiceImpl userserviceimpl;
public void setUserServiceImpl(UserServiceImpl userserviceimpl) {
this.userserviceimpl = userserviceimpl;
}
@Override
public String execute() throws Exception{
boolean flag = userserviceimpl.findUser(user);
if (flag) {
return SUCCESS;
} else {
return INPUT;
}
}
}
配置文件
<?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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd ">
<!-- dbcp连接池 -->
<bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource" >
<property name="driverClassName" value="com.mysql.jdbc.Driver">
</property>
<property name="url" value="jdbc:mysql://localhost:3306/user_db">
</property>
<property name="username" value="root">
</property>
<property name="password" value="1234">
</property>
</bean>
<!--hibernate-->
<bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource">
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQL55Dialect</prop>
<prop key="hbm2ddl.auto">update</prop>
<prop key="hibernate.connection.autocommit">true</prop>
</props>
</property>
<property name="mappingResources">
<list>
<value>entity/User.hbm.xml</value>
</list>
</property>
</bean>
<!--使用属性注入-->
<bean id="User" class="entity.User"></bean>
<bean id="UserDAO" class="dao.UserDAOImpl" scope="singleton">
<property name="sessionFactory">
<ref bean="sessionFactory"/>
</property>
</bean>
<bean id="UserService" class="service.UserServiceImpl" scope="prototype">
<property name="userDAOImpl" ref="UserDAO"></property>
</bean>
<bean id="action.RegistAction" class="action.RegistAction" scope="prototype">
<property name="UserServiceImpl" ref="UserService"></property>
</bean>
<bean id="action.LoginAction" class="action.LoginAction" scope="prototype">
<property name="UserServiceImpl" ref="UserService"></property>
</bean>
</beans>
在LoginAction中 userserviceimpl总是null