我在整合环境 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>
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>
解决方案 »
- 跪求<<<<500分>>>全送,MAXIMO 开发中文文档,哪位大神,真正的大神有这些开发中文文档啊,我的全是英文的,看不懂啊!
- 求助JAVA高手:myeclipse7.1不编译FSO,怎么解决?
- 如何提高导入Excel的效率
- 使用jspsmartuploader下载出现问题
- 如何解决tapestry页面上的数据持久问题?
- 总算把CSDN的导航树给剽了
- 关于网页出错的处理
- java中调用自定义类?请指点天机
- JDBC的Preparestatement插入数据 字段总是空的
- applet如果不用servlet通信,能否直接从数据库里读资料?
- SSH框架的程序出现404错误
- [Microsoft][SQLServer 2000 Driver for JDBC]No ResultSet set was produced.谢谢
<bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/>换成
<bean class="org.springframework.beans.factory.annotation.CommonAnnotationBeanPostProcessor"/>或者
<bean class="org.springframework.context.annotation.CommonAnnotationBeanPostProcessor" />
试试~
改为
<context:component-scan base-package="com.ibm" />
<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" />
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 }
lib大家自己用mvn命令下载
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方法,这样才能和前台相互传递值啊,否则当然会空指针,仔细想想基础问题吧
以后一定要记得每个变量的get方法在Strust2中必须有
public String getMessage() {
return message;
}
配置的地方我没看,自己找个样例对照吧
具体参照 http://maven.apache.org/guides/getting-started/index.html
大致步骤
1、装mvn 2、设置settings.xml 3、到工程目录下用命令 mvn package 打包,lib就自动下载了
方法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>
跟标签完全没关系!和get set没关系,因为用annatation的话getset方法完全可以不要!和用@Resource还是用Autowired也没关系!因为2种方法都是对的可以的!和扫描包的位置也是没关系的,因为只要扫描到该类就可!真正的原因是没有引入struts2-spring-plugin
原本我action是用struts.xml来配置的,后来采用annation方式,引入struts2-convention-plugin包,在网上看到篇帖子说,大致意思说只要引入了struts2-spring-plugin包,它就会默认采用xml管理action,所以把该包删除了,于是问题产生了,多次试验后发现这个说法是有误的。所以说实践出真知啊