楼主还是把关键的spring配置文件以及struts2的配置文件和关键的注入部分的代码贴出来吧
没人会有心情去下载你的项目在仔细的看的

解决方案 »

  1.   

    你是Struts和spring结合使用的,一般Struts里面的Fileter作为控制器,spring的配置文件是通过listener在web。xml中配置的,之所以你注入是空 很可能是配置文件在服务器启动时根本没有去执行
      

  2.   

    <context:component-scan base-package="com.ssh"/>这句改成<context:component-scan base-package="com.ssh.*"/>
      

  3.   


    你下载我的ssh看了,谢谢。。等下我试试看。。
      

  4.   

    struts配置<?xml version="1.0" encoding="UTF-8" ?>
    <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
    <struts>
    <constant name="struts.devMode" value="true"></constant>
    <package name="web" extends="struts-default">
    <action name="userAction" class="com.ssh.action.UserAction" method="getALL">
    <result name="getALL" type="dispatcher">/index.jsp</result>
    <!-- <result name="save" type="redirectAction">addUser.action!getALL</result> 
    本身想重定向到action的但是上面的这行老是报错呀!!
     -->
    </action>
    <action name="addUser" class="com.ssh.action.UserAction" method="save">
    <result name="save">/index.jsp</result>
    </action>
    </package>
    </struts>   spring配置<?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:aop="http://www.springframework.org/schema/aop"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:context="http://www.springframework.org/schema/context"
    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-2.5.xsd
                        http://www.springframework.org/schema/tx
              http://www.springframework.org/schema/tx/spring-tx.xsd
                        http://www.springframework.org/schema/context
                         http://www.springframework.org/schema/context/spring-context-2.5.xsd"
                         default-autowire="byName">
    <!-- 开启组件扫描 -->
    <context:component-scan base-package="com.ssh.*"/>

    <!-- 开启注解处理器 -->
    <context:annotation-config/>
    <!-- 声明切面 -->
    <aop:aspectj-autoproxy></aop:aspectj-autoproxy>
    <!-- 数据源 -->
    <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/test?useUnicode=true&amp;characterEncoding=utf-8">
    </property>
    <property name="username" value="root"></property>
    <property name="password" value="root"></property>
    </bean>
    <!-- 注入hibernate的依赖sessionFactory -->
    <bean id="sessionFactory"
    class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
    <!-- 注入dataSource -->
    <property name="dataSource">
    <ref bean="dataSource" />
    </property>
    <!-- 扫描hibernate的映射文件 -->
    <property name="mappingLocations">
    <value>classpath:com/ssh/entity/*.hbm.xml</value>
    </property>
    <!-- hibernate的配置文件 -->
    <property name="hibernateProperties">
    <props>
         <prop key="hibernate.show_sql">true</prop>
         <prop key="hibernate.format_sql">true</prop>
         <prop key="hibernate.hbm2ddl.auto">create</prop>
    <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
    </props>
    </property>
    </bean>
    <!-- 配置事务管理器-->   
      <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">   
         <property name="dataSource" ref="dataSource"/>   
       </bean> 
       <tx:advice id="txAdvice" transaction-manager="txManager">
       <tx:attributes>
       <tx:method name="save*" propagation="REQUIRED"/>
       <tx:method name="get*" read-only="true"/>
       </tx:attributes>
       </tx:advice>
       <aop:config>
       <aop:pointcut expression="execution (* com.ssh.service..*.*(..))" id="pointcutId"/>
       <aop:advisor advice-ref="txAdvice" pointcut-ref="pointcutId"/>
       </aop:config>
       <!-- 注入hibernate依赖hibernateTemplate -->
       <bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
    <property name="sessionFactory" ref="sessionFactory" />
    </bean>
    </beans>dao层package com.ssh.dao;
    import java.util.List;
    import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
    import org.springframework.stereotype.Repository;
    import com.ssh.entity.User;
    @SuppressWarnings("unchecked")
    @Repository("userDao")
    public class UserDaoImpl extends HibernateDaoSupport implements UserDao { @Override
    public void saveUser(User u) {
    this.getHibernateTemplate().save(u);
    }
    @Override
    public List<User> getAllUser() {
    return this.getHibernateTemplate().find("form User");
    }}service层package com.ssh.service;
    import java.util.List;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Service;
    import com.ssh.dao.UserDao;
    import com.ssh.entity.User;
    @Service("userService")
    public class UserServiceImpl implements UserService {
    private UserDao userDao;
    @Autowired
    public void setUserDao(UserDao userDao) {
    this.userDao = userDao;
    } public UserDao getUserDao() {
    return userDao;
    } @Override
    public void saveUser(User u) {
    userDao.saveUser(u);
    } @Override
    public List<User> getAllUser() {
    return userDao.getAllUser();
    }}action代码package com.ssh.action;
    import java.util.List;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.context.annotation.Scope;
    import org.springframework.stereotype.Controller;
    import com.opensymphony.xwork2.ActionSupport;
    import com.ssh.entity.User;
    import com.ssh.service.UserService;
    @Controller("userAction")
    @Scope("prototype")
    public class UserAction extends ActionSupport{
    private static final long serialVersionUID = 1L;
    private User user;
    private UserService userService;
    private List<User> userList;

    @Autowired
    public void setUserService(UserService userService) {
    this.userService = userService;
    }

    public UserService getUserService() {
    return userService;
    } public List<User> getUserList() {
    return userList;
    }

    public void setUserList(List<User> userList) {
    this.userList = userList;
    } public User getUser() {
    return user;
    }
    public void setUser(User user) {
    this.user = user;
    } public String getAll(){
    userList=userService.getAllUser();
    return "getALL";
    }

    public String save() throws Exception {
    userService.saveUser(user);
    return "save";
    }
    }
    其中action中的userService,service中的userDao,dao中的this.getHibernateTemplate()为空;
    另外给我解释一下这几个标签作用是什么<context:annotation-config/>
    <aop:config>
    <aop:pointcut expression="execution (* com.ssh.service..*.*(..))" id="pointcutId"/>
    <aop:advisor advice-ref="txAdvice" pointcut-ref="pointcutId"/>
    <aop:aspect></aop:aspect>
    </aop:config>
    <aop:aspectj-autoproxy></aop:aspectj-autoproxy>
      

  5.   

    @Autowired一般都写在需要注入的资源上面,例如@Autowired
                                                 private UserService userService;关于最后几个标签,简单说下,是SpringAOP相关的配置文件,不懂的话可以百度下,pointcut就是声明的切入点,表明要拦截com.ssh.service包下的所有方法,说白了就是个拦截器
                                                 
      

  6.   

    <!-- <result name="save" type="redirectAction">addUser.action!getALL</result> 
                本身想重定向到action的但是上面的这行老是报错呀!!
                 -->
    动态方法调用应该这么写
    <result name="save" type="redirectAction">addUser!getALL.action</result>
    不过不建议用这种用,推荐使用以下这种
    <result name="save" type="redirectAction">
      <param name="actionName">addUser</param>
      <param name="method">getALL</param>
    </result>