我在整合环境 Struts2.2.1 + Spring3.1.2 时,我的UserAction 的第16行user = userService.getUserByID("1");
userService始终为null,报NullPointerException ,不知如何回事,请大侠帮忙看看,谢谢!所用lib:
   aopalliance-1.0.jar
   aspectjrt-1.7.0.jar
   commons-fileupload-1.2.1.jar
   commons-io-1.3.2.jar
   commons-logging-1.1.1.jar
   freeer-2.3.16.jar
   javassist-3.11.0.GA.jar
   log4j-1.2.14.jar
   ognl-3.0.jar
   spring-aop-3.1.2.RELEASE.jar
   spring-asm-3.1.2.RELEASE.jar
   spring-beans-3.1.2.RELEASE.jar
   spring-context-3.1.2.RELEASE.jar
   spring-core-3.1.2.RELEASE.jar
   spring-expression-3.1.2.RELEASE.jar
   spring-jdbc-3.1.2.RELEASE.jar
   spring-orm-3.1.2.RELEASE.jar
   spring-tx-3.1.2.RELEASE.jar
   spring-web-3.1.2.RELEASE.jar
   struts2-convention-plugin-2.2.1.jar
   struts2-core-2.2.1.jar
   xwork-core-2.2.1.jar
---web.xml------------------------------------------------------------------------------------------------<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>myWebApp</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
    
 
         <!-- 配置spring的监听器 -->
 <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>

        <!-- 配置Struts2 -->
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
<init-param>
<param-name>struts.devMode</param-name>
<param-value>true</param-value>
</init-param>
<init-param>
<param-name>encoding</param-name>
            <param-value>UTF-8</param-value>
</init-param>
</filter> <filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping></web-app>---Action-------------------------------------------------------------------------------------------------1 package com.ibm.action;
2 import javax.annotation.Resource;
3 import org.apache.struts2.convention.annotation.Action;
4 import com.opensymphony.xwork2.ActionSupport;
5 import com.ibm.service.UserService;
6 import com.ibm.User;
7 public class UserAction extends ActionSupport {
8     
9     private User user;
10
11     @Resource(name="userService")
12     private UserService userService;
13
14     @Action("/user/detail")
15     public String detail() throws Exception {
16         user = userService.getUserByID("1");
17    return SUCCESS;
18     }
19 }
---Service------------------------------------------------------------------------------------------------package com.ibm.service;
import com.ibm.User;
public interface UserService {
User getUserByID(String id);
}
package com.ibm.service.impl;import org.springframework.stereotype.Service;
import com.ibm.User;
import com.ibm.service.UserService;@Service("userService") 
public class UserServiceImpl implements UserService {   public User getUserByID(String id) {
User user = new User();
user.setAge(20);
user.setUserName("mike");
return user;
}}
---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: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.1.xsd
    http://www.springframework.org/schema/tx
    http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
    http://www.springframework.org/schema/aop
    http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-3.1.xsd
    ">  <bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/> 
 <context:component-scan base-package="com.ibm.service" /><!-- 扫描包 -->
 <context:annotation-config/>  
           <!--问题应该出在这个xml,但不知如何解决-->
</beans>

解决方案 »

  1.   

    如果想使用@ Resource 、@ PostConstruct、@ PreDestroy等注解就必须声明CommonAnnotationBeanPostProcessor把你的applicationContext.xml里的
    <bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/>换成
    <bean class="org.springframework.beans.factory.annotation.CommonAnnotationBeanPostProcessor"/>或者
    <bean class="org.springframework.context.annotation.CommonAnnotationBeanPostProcessor" /> 
    试试~
      

  2.   

    <context:component-scan base-package="com.ibm.service" />
    改为
    <context:component-scan base-package="com.ibm" />
      

  3.   

    <?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:aop="http://www.springframework.org/schema/aop"
     xmlns:tx="http://www.springframework.org/schema/tx" 
     xmlns:context="http://www.springframework.org/schema/context"
     xmlns:mvc="http://www.springframework.org/schema/mvc" 
     xmlns:util="http://www.springframework.org/schema/util"
     xmlns:p="http://www.springframework.org/schema/p"

     xsi:schemaLocation="
      http://www.springframework.org/schema/beans
      http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
      http://www.springframework.org/schema/tx
      http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
      http://www.springframework.org/schema/aop
      http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
      http://www.springframework.org/schema/context
      http://www.springframework.org/schema/context/spring-context-3.1.xsd
      http://www.springframework.org/schema/mvc
      http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd

      ">
      
      <mvc:annotation-driven />
      <context:annotation-config/>
      <context:component-scan base-package="com.ibm" />

      
      
      

  4.   

    @Controller  //------ 这要加这个
    public class UserAction extends ActionSupport {
    8  
    9 private User user;
    10
    11 @Resource(name="userService")
    12 private UserService userService;
    13
    14 @Action("/user/detail")
    15 public String detail() throws Exception {
    16 user = userService.getUserByID("1");
    17 return SUCCESS;
    18 }
    19 }
      

  5.   

    为方便大家学习和查找问题,我已将工程上传至http://download.csdn.net/detail/thrive_li/4575935
    lib大家自己用mvn命令下载
      

  6.   

    网页打开404你懂的,如何用mvn命令下载,求指教~
      

  7.   

    你的Action类有问题!!
    7 public class UserAction extends ActionSupport {
    8   
    9 private User user;
    10
    11 @Resource(name="userService")
    12 private UserService userService;
    13
    14 @Action("/user/detail")
    15 public String detail() throws Exception {
    16 user = userService.getUserByID("1");
    17  return SUCCESS;
    18 } 这个你们也太粗心了,声明了变量,为什么不给他们付get 和set方法,这样才能和前台相互传递值啊,否则当然会空指针,仔细想想基础问题吧
      

  8.   

    如果还是不对的话再看看你的Action之下的Service什么的变量有没有给get, set方法!
    以后一定要记得每个变量的get方法在Strust2中必须有
    public String getMessage() {
             return message;
        } 
    配置的地方我没看,自己找个样例对照吧
      

  9.   

    还是不行啊,还需要配置什么呢?get set方法是我漏贴了,算你找到了个错误吧。
      

  10.   


    具体参照 http://maven.apache.org/guides/getting-started/index.html
    大致步骤 
    1、装mvn 2、设置settings.xml  3、到工程目录下用命令 mvn package 打包,lib就自动下载了
      

  11.   

    问题我自己解决了,还是感谢各位帮助。
    方法xml写成如下即可:why?!<?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:p="http://www.springframework.org/schema/p"
           xmlns:context="http://www.springframework.org/schema/context"
           xmlns:tx="http://www.springframework.org/schema/tx"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xmlns:aop="http://www.springframework.org/schema/aop"
           xmlns:jdbc="http://www.springframework.org/schema/jdbc"
           xsi:schemaLocation="
                http://www.springframework.org/schema/beans
                http://www.springframework.org/schema/beans/spring-beans.xsd
                http://www.springframework.org/schema/aop
                http://www.springframework.org/schema/aop/spring-aop.xsd
                http://www.springframework.org/schema/tx
                http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
                http://www.springframework.org/schema/jdbc
                http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd
                http://www.springframework.org/schema/context
                http://www.springframework.org/schema/context/spring-context-3.0.xsd">
        
        <context:component-scan base-package="com.ibm" />
        <tx:annotation-driven /> </beans>
      

  12.   


    跟标签完全没关系!和get set没关系,因为用annatation的话getset方法完全可以不要!和用@Resource还是用Autowired也没关系!因为2种方法都是对的可以的!和扫描包的位置也是没关系的,因为只要扫描到该类就可!真正的原因是没有引入struts2-spring-plugin
    原本我action是用struts.xml来配置的,后来采用annation方式,引入struts2-convention-plugin包,在网上看到篇帖子说,大致意思说只要引入了struts2-spring-plugin包,它就会默认采用xml管理action,所以把该包删除了,于是问题产生了,多次试验后发现这个说法是有误的。所以说实践出真知啊