问题描述
UserInfoDAOImpl.java@Servicepublic class UserInfoDAOImpl extends HibernateDaoSupport implements UserInfoDAO {public boolean login(Userinfo userinfo) {boolean isExist = false;Userinfo temp = (Userinfo)this.getHibernateTemplate().find("from Userinfo u where u.username=?", userinfo.getUsername()).get(0);try {if(userinfo.getPassword().equals(temp.getPassword())) isExist = true;} catch(Exception e) {e.printStackTrace();}System.out.println("id:" + temp.getId());System.out.println("username:" + temp.getUsername());System.out.println("password:" + temp.getPassword());System.out.println("isExist:" + isExist);return isExist;}}LoginAction.java@Component@Results(value={@Result(name="success", location="/login_success.jsp"),@Result(name="fail", location="/Login.jsp")})public class LoginAction extends ActionSupport {private Userinfo userinfo;@Autowiredprivate UserInfoDAO userInfoDAOImpl;public Userinfo getUserinfo() {return userinfo;}public void setUserinfo(Userinfo userinfo) {this.userinfo = userinfo;}public UserInfoDAO getUserInfoDAOImpl() {return userInfoDAOImpl;}public void setUserInfoDAOImpl(UserInfoDAO userInfoDAOImpl) {System.out.println("注入");this.userInfoDAOImpl = userInfoDAOImpl;}@Action("login")public String login() {System.out.println("id:" + userinfo.getId());System.out.println("username:" + userinfo.getUsername());System.out.println("password:" + userinfo.getPassword());if(userInfoDAOImpl == null)System.out.println("error");if(userinfo == null)return "fail";else return (userInfoDAOImpl.login(userinfo))?"success":"fail";}}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:jee="http://www.springframework.org/schema/jee"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/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"default-lazy-init="true"><beanclass="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"><property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE" /><property name="ignoreResourceNotFound" value="true" /><property name="locations"><list><value>classpath:application.properties</value></list></property></bean><context:component-scan base-package="org.sales.dao.Impl" /><!-- 数据源配置,使用应用内的DBCP数据库连接池 --><bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"destroy-method="close"><!-- Connection Info --><property name="driverClassName" value="${jdbc.driverClassName}" /><property name="url" value="${jdbc.url}" /><property name="username" value="${jdbc.username}" /><property name="password" value="${jdbc.password}" /></bean><bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"><property name="dataSource" ref="dataSource" /><property name="namingStrategy"><bean class="org.hibernate.cfg.ImprovedNamingStrategy" /></property><property name="hibernateProperties"><props><prop key="hibernate.dialect">${hibernate.dialect}</prop><prop key="hibernate.show_sql">${hibernate.show_sql}</prop><prop key="hibernate.format_sql">${hibernate.format_sql}</prop><prop key="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</prop><!--<prop key="hibernate.cache.provider_configuration_file_resource_path">${hibernate.ehcache_config_file}</prop>--></props></property><property name="packagesToScan" value="org.sales.entity" /><property name="annotatedClasses"><list><value>org.sales.entity.Iteminfo</value><value>org.sales.entity.Cardinfo</value><value>org.sales.entity.Positioninfo</value><value>org.sales.entity.Sellinfo</value><value>org.sales.entity.Userinfo</value><value>org.sales.entity.Voteinfo</value></list></property></bean><bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate" > <property name="sessionFactory" ref="sessionFactory"/></bean><bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"><property name="sessionFactory" ref="sessionFactory" /></bean><tx:annotation-driven transaction-manager="transactionManager" /><bean id="CardinfoDAO" class="org.sales.entity.CardinfoDAO"><property name="sessionFactory"><ref bean="sessionFactory" /></property></bean><bean id="IteminfoDAO" class="org.sales.entity.IteminfoDAO"><property name="sessionFactory"><ref bean="sessionFactory" /></property></bean><bean id="PositioninfoDAO"class="org.sales.entity.PositioninfoDAO"><property name="sessionFactory"><ref bean="sessionFactory" /></property></bean><bean id="SellinfoDAO" class="org.sales.entity.SellinfoDAO"><property name="sessionFactory"><ref bean="sessionFactory" /></property></bean><bean id="UserinfoDAO" class="org.sales.entity.UserinfoDAO"><property name="sessionFactory"><ref bean="sessionFactory" /></property></bean><bean id="VoteinfoDAO" class="org.sales.entity.VoteinfoDAO"><property name="sessionFactory"><ref bean="sessionFactory" /></property></bean></beans>web.xml<?xml version="1.0" encoding="UTF-8"?><web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"><context-param><param-name>contextConfigLocation</param-name><param-value>classpath*:applicationContext.xml</param-value></context-param><filter><filter-name>OpenSessionInViewFilter</filter-name><filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class><init-param> <param-name>singleSession</param-name> <param-value>true</param-value> </init-param></filter><listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener><filter><filter-name>struts2Filter</filter-name><filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class></filter><filter-mapping><filter-name>struts2Filter</filter-name><url-pattern>/*</url-pattern></filter-mapping><filter-mapping><filter-name>OpenSessionInViewFilter</filter-name><url-pattern>/*</url-pattern></filter-mapping><welcome-file-list><welcome-file>index.jsp</welcome-file></welcome-file-list></web-app>结果在LoginAction.java里面输出:id:nullusername:usernamepassword:USERNAMEerror说明: error是当被注入的属性为空的时候输出. 求解~ 异常(网页上的):HTTP Status 500 -type Exception reportmessagedescription The server encountered an internal error () that prevented it from fulfilling this request.exceptionjava.lang.NullPointerExceptionorg.sales.actions.LoginAction.login(LoginAction.java:53)sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)java.lang.reflect.Method.invoke(Method.java:597)com.opensymphony.xwork2.DefaultActionInvocation.invokeAction(DefaultActionInvocation.java:441)com.opensymphony.xwork2.DefaultActionInvocation.invokeActionOnly(DefaultActionInvocation.java:280)com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:243)问题补充xixix2004 写道
解决方案
会不会是struts2-spring插件包没有加进来?
解决方案二:
你的 @Autowired是不是要写在set方法上面啊。。
解决方案三:
bbinglongg 写道dwbin 写道loginAction没有进行spring托管。这个类spring不会自己进行注入的。你的ssh整合有问题在 @Autowired 这个标签里面已经是注入了你这个不是进行spring托管,该类的实例还是由struts2进行维护的,所以spring没法注入。我没有做过struts2和spring进行整合,但是我觉得应该有一个由spring提供的进行整合的类,这个类需要被配置在web.xml里面而不是struts的类。或者有一个struts提供的自动为spring提供注入的接口和实现。你这个注解是spring的,struts是无法使用的
解决方案四:
loginAction没有进行spring托管。这个类spring不会自己进行注入的。你的ssh整合有问题
解决方案五:
<ref bean="UserInfoDAO" />这里的REF的BEAN, 写你之前定义的BEAN的ID。你可明白?
解决方案六:
bbinglongg 写道已经试过在 applicationContext.xml 文件里面添加<bean id="userInfoDAOImpl" class="org.sales.dao.Impl.UserInfoDAOImpl"></bean><bean id="loginAction" class="org.sales.actions.LoginAction"><property name="userInfoDAOImpl"><ref bean="userInfoDAOImpl" /></property></bean>异常信息一样.<bean id="userInfoDAOImpl" class="org.sales.dao.Impl.UserInfoDAOImpl"> </bean> <bean id="loginAction" class="org.sales.actions.LoginAction"> <property name="userInfoDAOImpl"> <ref bean="UserInfoDAO" /> </property> </bean>
解决方案七:
bbinglongg 写道xixix2004 写道好吧,应该不需要异常,请把ACTION里定义的DAO的变量名,变得和xml里定义的BEAN ID属性一致。xml里面没有对应的bean,因为使用注释你完全没看懂我在说什么。private UserInfoDAO userinfoDAO; <bean id="userinfoDAO" class="org.sales.entity.UserinfoDAO"> <property name="sessionFactory"> <ref bean="sessionFactory" /> </property> </bean>
解决方案八:
可能和楼主的应用写法不太相同,请问你其他的service是否能注入成功呢?,不过是否能试试在applicationContext配置文件中的声明方式换一种呢?如:<bean id="UserInfoDAO"class="UserInfoDAOImp"></bean>这种形式注入,实例化由spring托管,而在action中只对UserInfoDAO的service进行注入呢而如果出了该service无法注入外,其他其余的service都能正常注入使用,那就另想办法了
解决方案九:
好吧,应该不需要异常,请把ACTION里定义的DAO的变量名,变得和xml里定义的BEAN ID属性一致。
解决方案十:
有异常吗。 敢不敢把异常发出来看一下。