问题描述
在tomact服务启动之后我在Action里对象set方法 打印出对象 证明是注入进去的 ,, 当我调用Action里面对象的时候 就提示我空指针异常 如果使用getBean 的话是可以得到对象的web.xml <context-param><param-name>contextConfigLocation</param-name><param-value>classpath:applicationContext.xml</param-value></context-param><listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener><!-- 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"?><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"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"> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"> <property name="driverClassName" value="${jdbc.driver}"></property><property name="url" value="${jdbc.url}"></property><property name="username" value="${jdbc.username}"></property><property name="password" value="${jdbc.pwd}"></property><property name="maxIdle" value="${jdbc.maxIdleTime}"></property><property name="maxActive" value="${jdbc.maxPoolSize}"></property> </bean> <bean id="sessionFactorydao" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <property name="dataSource" ref="dataSource" /> <property name="mappingDirectoryLocations"> <list> <value>classpath:/entity/</value></list> </property><property name="hibernateProperties"> <value> hibernate.dialect=org.hibernate.dialect.SQLServerDialect<!-- hibernate.dialect=org.hibernate.dialect.MySQL5InnoDBDialect -->hibernate.show_sql=true </value></property> </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";}} 问题补充:ethen 写道
解决方案
那就改成 userAction
解决方案二:
引用String name = request.getParameter("name"); String pwd = request.getParameter("pwd"); 这样能获取到值吗?如果是使用struts2的话,建议采用属性 getter setter的方式来获取参数值。多加点输出日志进行调试吧。这个不是什么大问题。
解决方案三:
没看见 struts.xmlstruts里头action里配置的是 class = "userAction" 还是 class="Action.UserAction"?struts2-spring-plugin.jar 加进去了没?
解决方案四:
把错误信息贴出来看看吧