spring3+hibernate4+struts2整合有关问题

spring3+hibernate4+struts2整合问题
本帖最后由 leichangfeng 于 2014-05-28 11:09:00 编辑
一个注册用户的小例子;运行后UserAction.java文件中
try {
if(userManager.exists(u)){
System.out.print("此用户存在");
return "fail";
}
此段中userManager总是为null。如果需要具体联系。我的QQ号:382692563   加我时备注:解决你java问题
出错效果图为:
spring3+hibernate4+struts2整合有关问题


1、运行程序后首个页面图如下:spring3+hibernate4+struts2整合有关问题
2、项目结构图如下:spring3+hibernate4+struts2整合有关问题
3、UserAction.java文件主要部分代码:
public class UserAction extends ActionSupport {
private String username;
private String password;
private String password2;

@Autowired
    private UserManager userManager;
    
@Override   
public String execute(){
User u=new User();
u.setUsername(username);
u.setPassword(password);

try {
if(userManager.exists(u)){     //注意出错地方userManager为null
System.out.print("此用户存在");
return "fail";
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
userManager.add(u);     //注意出错地方userManager为null
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return "success";
}

public String getUsername() {
return username;
}

public void setUsername(String username) {
this.username = username;
}

public String getPassword() {
return password;
}

public void setPassword(String password) {
this.password = password;
}

public String getPassword2() {
return password2;
}

public void setPassword2(String password2) {
this.password2 = password2;
}


}
4、UserDaoImpl.java主要代码如下:
@Component("userDao")
public class UserDaoImpl implements UserDao  {

@Autowired
@Qualifier("mySessionFactory")
private SessionFactory mySessionFactory;

@Override
public boolean checkUserExistsWithName(String username) {
Session s=mySessionFactory.getCurrentSession();
s.beginTransaction();
    long count=(long)s.createQuery("select count(*) from User u where u.username = :username")
      .setString("username", username)
      .uniqueResult();
s.getTransaction().commit();

if(count > 0){
return true;
}
return false;
}

@Override
public void sava(User u) {
Session s=mySessionFactory.getCurrentSession();
s.beginTransaction();
    s.save(u);
    s.getTransaction().commit();
}
}
5、UserManagerImpl.java部分代码如下:

@Service("userManager")  //把普通pojo实例化到spring容器中,相当于配置文件中的<bean id="" class=""/>
public class UserManagerImpl implements UserManager {

@Autowired
private UserDao userDao;

@Override
public boolean exists(User u) throws Exception{
return userDao.checkUserExistsWithName(u.getUsername());
}


@Override
public void add(User u) throws Exception {
userDao.sava(u);
}

}
6、beans.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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" 
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:dwr="http://www.directwebremoting.org/schema/spring-dwr"
xsi:schemaLocation="http://www.springframework.org/schema/beans 
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context 
http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.2.xsd">

   <context:annotation-config />   
   <context:component-scan base-package="com.registration.*" annotation-config="true"/>  <!--spring通过扫描找@Component注解部分, 把userDAO初始化成一个对象 -->
  
  <!-- 连接数据库部分开始 -->
   <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="locations">
        <value>classpath:jdbc.properties</value>  <!-- 具体的数据库连接地址文件:jdbc.properties-->