applicationContext.xml<bean id="userDao" class="com.quxiuna.dao.impl.UserDaoImpl">
<property name="sessionFactory">
<ref local="sessionFactory" />
</property>
</bean>
<bean id="userService" class="com.quxiuna.service.impl.UserServiceImpl">
<property name="userDao">
<ref local="userDao" />
</property>
</bean>
<bean id="loginAction" class="com.quxiuna.struts.action.login.LoginAction" scope="prototype">
<property name="userService">
<ref local="userService" />
</property>
</bean>struts.xml<struts>
    <constant name="struts.objectFactory" value="spring" />
<constant name="struts.devMode" value="true" />
    <constant name="struts.objectFactory.spring.autoWire" value="type" />
    <include file="struts-default.xml"/>
<include file="com/quxiuna/struts/action/login/loginAction.xml" />
</struts> loginAction.xml<struts>    
     <package name="login" extends="struts-default" namespace="/login">    
         <action name="loginAction" class="com.quxiuna.struts.action.login.LoginAction">    
             <result name="success">/sendSuccss.jsp</result>
         </action> 
     </package>
</struts> web.xml  <context-param>
       <param-name>contextConfigLocation</param-name>
       <param-value>
       /WEB-INF/applicationContext*.xml
       </param-value>
</context-param>
<listener>      
       <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>UserServiceImpl.javapublic class UserServiceImpl implements UserService {
public UserDAO userDao; public UserDAO getUserDao() {
return userDao;
} public void setUserDao(UserDAO userDao) {
this.userDao = userDao;
}package com.quxiuna.struts.action.login;import java.util.Map;import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.quxiuna.hibernate.po.RbacUser;
import com.quxiuna.service.UserService;
import com.quxiuna.util.Constants;public class LoginAction extends ActionSupport {

private static final long serialVersionUID = -1640288447822607013L;

private String name;
private String password;
private String message;
public UserService userService; public String getName() {
return name;
}
    
public void setName(String name) {
this.name = name;
} public String getPassword() {
return password;
} public void setPassword(String password) {
this.password = password;
}
    
@SuppressWarnings("unchecked")
public String execute() {
RbacUser user = userService.login(name, password);
if (user != null) {
ActionContext ctx = ActionContext.getContext(); 
    Map session = ctx.getSession(); 
session.put(Constants.SESSSION_NAME, name);
return SUCCESS;
} else {
message = "用户名或密码错误!"; 
return ERROR;
}
} public String getMessage() {
return message;
} public void setMessage(String message) {
this.message = message;
} public UserService getUserService() {
return userService;
} public void setUserService(UserService userService) {
this.userService = userService;
}
}
报错
java.lang.NullPointerException
com.quxiuna.struts.action.login.LoginAction.execute(LoginAction.java:38)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
java.lang.reflect.Method.invoke(Unknown Source)
com.opensymphony.xwork2.DefaultActionInvocation.invokeAction(DefaultActionInvocation.java:404)
com.opensymphony.xwork2.DefaultActionInvocation.invokeActionOnly(DefaultActionInvocation.java:267)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:229)
         …………………………………………
         …………………………………………

解决方案 »

  1.   

    我怀疑是spring的配置文件没有加载工程结构图如下
      

  2.   

    <action name="loginAction" class="com.quxiuna.struts.action.login.LoginAction">改下:<action name="loginAction" class="loginAction">
      

  3.   


    这样会报错严重: Error filterStart
    2010-3-23 17:04:38 org.apache.catalina.core.StandardContext start
    严重: Context [/email] startup failed due to previous errors
    2010-3-23 17:04:38 org.apache.coyote.http11.Http11BaseProtocol start
      

  4.   


    spring加载了,loginAction对象也生成并且注入成功了,但是和struts2的使用的loginAction对象不是一个,所以报空指针错误。把完整的web.xml发上来看看吧
      

  5.   

    <?xml version="1.0" encoding="UTF-8"?>   
    <web-app 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"   
               version="2.5"> 
        <context-param>
           <param-name>contextConfigLocation</param-name>
           <param-value>
           /WEB-INF/applicationContext*.xml
           </param-value>
    </context-param>
    <listener>      
           <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <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>/*</url-pattern>
    </filter-mapping>
    <!-- OpenSessionInViewFilter过滤器 -->
    <filter>
    <filter-name>lazyLoadingFilter</filter-name>
    <filter-class>
    org.springframework.orm.hibernate3.support.OpenSessionInViewFilter
    </filter-class>
    </filter>
    <filter-mapping>
    <filter-name>lazyLoadingFilter</filter-name>
    <url-pattern>*.action</url-pattern>
    </filter-mapping>
    <welcome-file-list>   
            <welcome-file>index.jsp</welcome-file>   
        </welcome-file-list>   
    </web-app>  
      

  6.   

    如果把Action给spring管理 
    web.xml里的Action的class=“spring里配置的Action的name”
      

  7.   

    applicationContext.xml把这个spring的配置文件与web.xml同一目录下试试
      

  8.   


    <bean id="loginAction" class="com.quxiuna.struts.action.login.LoginAction" scope="prototype">
            <property name="userService">
                <ref local="userService" />
            </property>
        </bean>
    删掉试试
      

  9.   

    java.lang.NullPointerException
    com.quxiuna.struts.action.login.LoginAction.execute(LoginAction.java:38)错误多明确。。
    那就看看 38行呀。RbacUser user = userService.login(name, password);
    其中  userService 你初始化了吗?
      

  10.   


    sorry...习惯@Autowired了
    不过 肯定还是38行那个有问题 
      

  11.   

    <constant name="struts.objectFactory" value="spring" />
    这就话说明你把action交给spring管理了,
     <action name="loginAction" class="com.quxiuna.struts.action.login.LoginAction">     
    这句话的意思是让struts去管理action,这样就矛盾了,
    <bean id="loginAction" class="com.quxiuna.struts.action.login.LoginAction" scope="prototype">
    这句话又是让spring管理action,又矛盾了
    应该改成:
    <action name="loginAction" class="LoginAction"> 
    其他不变~~~
      

  12.   


    <bean id="loginAction" class="com.quxiuna.struts.action.login.LoginAction" scope="prototype">
            <property name="userService">
                <ref local="userService" />
            </property>
        </bean>
    上面的id 建议换成name="/loginAction" 和下面的struts中的action的path对应
    <struts>            
    <package name="login" extends="struts-default" namespace="/login">                <action name="loginAction" class="com.quxiuna.struts.action.login.LoginAction"> 
    这里的class不要。通过spring注入不需要class,已经和上面的bean对应了。
                       <result name="success">/sendSuccss.jsp </result>            </action>        
    </package>
    </struts>  
      

  13.   

    这是struts2不是struts1.x 你看不出来吗???明显是userService没有注入...建议楼主写个JUNIT测试一下,还有一点也有可能导致出现问题:
    对于注入的属性,你能不提供get方法不? 这也可能导致问题的.以前我就碰到过.虽然不是注入的问题,为什么要多提个一个get方法的? 偷下懒???
      

  14.   

    写一个junit先把DAO测通,再测service层,这个别人帮不了你,因为我们工具上没有你的项目环境.还得靠你自己.
      

  15.   

    service 没有注入成功或是没有获得。struts2注入的时候必须要提供set、get,否则null是很正常的好好检查下,其实debug一下,一步步的走,就看到哪里出问题啦这个问题完全可以自己搞定的要先自己 debug完后在找别人解决。直接定位bug,这样大家也好给你调试
      

  16.   

    在  LoginAction  中 添加  
    public UserService userService;
    的 get/set 方法
      

  17.   

    2楼说的就对啊,不然你就没把action交给spring处理
    如果还报错,那是别的错误,贴出来
      

  18.   

    <action name="loginAction" class="com.quxiuna.struts.action.login.LoginAction">改下:<action name="/loginAction" class="loginAction">
      

  19.   

    userService 对象没有注入,
    问题在struts.xml 中
    <constant name="struts.objectFactory" value="spring" />
    <constant name="struts.devMode" value="true" />不是矛盾了吗, 对象到底由谁创建
      

  20.   

    谢谢大家帮忙  最后我 重新换了lib中所有的jar包 好用了 
      

  21.   

    请问你是把jar包换成哪些啊 ?我也出现了同样的问题。谢谢了!