quanzjun 2010-11-25 15:27
浏览 256
已采纳

ssh2整合 spring 注入对象为空

在tomact服务启动之后我在Action里对象set方法 打印出对象 证明是注入进去的 ,, 当我调用Action里面对象的时候 就提示我空指针异常
如果使用getBean 的话是可以得到对象的

web.xml
[code="java"][/code]

contextConfigLocation
classpath:applicationContext.xml


org.springframework.web.context.ContextLoaderListener

<!-- OpenSessionInView的处理 -->
<filter>
    <filter-name>openSessionInView</filter-name>
    <filter-class>
        org.springframework.orm.hibernate3.support.OpenSessionInViewFilter
    </filter-class>
    <init-param>  
        <param-name>sessionFactoryBeanName</param-name>  
        <param-value>sessionFactorydao</param-value>  
    </init-param> 
</filter>
<filter-mapping>
    <filter-name>openSessionInView</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

    <filter>
        <filter-name>struts2</filter-name>
        <filter-class>org.apache.struts2.dispatcher.FilterDispatcher
        </filter-class>
    </filter>

    <filter-mapping>
        <filter-name>struts2</filter-name>
        <url-pattern>*.action</url-pattern>
    </filter-mapping>

    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>
    </web-app>

applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
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-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.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-2.5.xsd">












classpath:/entity/




hibernate.dialect=org.hibernate.dialect.SQLServerDialect
<!-- hibernate.dialect=org.hibernate.dialect.MySQL5InnoDBDialect -->
hibernate.show_sql=true

           </bean>
           <bean id="p" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
            <property name="locations">
                <list><value>classpath:/jdbc.properties</value></list>
            </property>
        </bean>
        <bean id="txManage" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
            <property name="sessionFactory" ref="sessionFactorydao" />
        </bean>
        <tx:advice id="TxtransactionManage" transaction-manager="txManage">
            <tx:attributes>
                <tx:method name="save*" propagation="REQUIRED"  />
                <tx:method name="delete*" propagation="REQUIRED" />
                <tx:method name="update*" propagation="REQUIRED" />
            </tx:attributes>
        </tx:advice>
        <aop:config>
            <aop:pointcut id="userDaosearch" expression="execution( * ouyang.UserService.*.*(..))" />
            <aop:advisor advice-ref="TxtransactionManage"  pointcut-ref="userDaosearch" />
        </aop:config>
        <bean id="spring" class="util.SpringUtil" />
            <bean id="UserDao" class="testDAO.UserDao">
                <property name="sessionFactory" ref="sessionFactorydao" />
            </bean>
            <bean id="UserService" class="UserService.UserService">
                <property name="userDao" ref="UserDao" />
            </bean>
            <bean id="userAction" class="Action.UserAction">
                <property name="userService" ref="UserService" />

            </bean>

       </beans>

UserAction

package Action;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;
import org.apache.xbean.spring.context.ClassPathXmlApplicationContext;
import org.springframework.context.ApplicationContext;
import org.springframework.web.struts.ActionSupport;

import testDAO.IUserDao;

import UserService.IUserService;
import UserService.UserService;

public class UserAction extends ActionSupport{

IUserService userService;
public void setUserService(IUserService userService) {
    System.out.println("注入进来了");
    this.userService = userService;
}
public IUserService getUserService() {
    return userService;
}
public String login(){
    HttpServletRequest request = ServletActionContext.getRequest();
    String name = request.getParameter("name");
    String pwd = request.getParameter("pwd");
    //ApplicationContext app= new ClassPathXmlApplicationContext("applicationContext.xml");
    //userService =(UserService)app.getBean("UserService");
    System.out.println(userService==null);
    if(userService.Login(name, pwd)){
        request.setAttribute("success", "登陆成功");
    }else{
        request.setAttribute("success", "登陆失败");
    }
    return "index";
}

}

  • 写回答

4条回答 默认 最新

  • 智百盛软件 2010-11-25 16:39
    关注

    那就改成 userAction

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 jmeter脚本回放有的是对的有的是错的
  • ¥15 r语言蛋白组学相关问题
  • ¥15 Python时间序列如何拟合疏系数模型