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个回答

那就改成 userAction

把错误信息贴出来看看吧

没看见 struts.xml

struts里头action里配置的是 class = "userAction" 还是 class="Action.UserAction"?

struts2-spring-plugin.jar 加进去了没?

[quote]
String name = request.getParameter("name");
String pwd = request.getParameter("pwd");
[/quote]

这样能获取到值吗?如果是使用struts2的话,建议采用属性 getter setter的方式来获取参数值。

多加点输出日志进行调试吧。这个不是什么大问题。 :idea:

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!