今天做一个Spring和Struts的融合,通过bean注入后,程序一跑起来,就报这个错误:

java.lang.NullPointerException
com.cb.action.LoginAction.execute(LoginAction.java:48)
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:452)
com.opensymphony.xwork2.DefaultActionInvocation.invokeActionOnly(DefaultActionInvocation.java:291)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:254)
com.opensymphony.xwork2.interceptor.DefaultWorkflowInterceptor.doIntercept(DefaultWorkflowInterceptor.java:176)
com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)

下面先交待下各配置文件的情况:

struts.xml如下:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
<package name="login2" extends="struts-default" >
<action name="login" class="loginAction">
<result name="error">/err.jsp</result>
<result name="success">/welcome.jsp</result>
</action>
<!-- 让用户直接访问该应用时列出所有视图页面 -->
<action name="*">
<result>/WEB-INF/content/{1}.jsp</result>
</action>
</package>
</struts>

Spring的applicationContext.xml如下:

<?xml version="1.0" encoding="GBK"?>
<!-- Spring配置文件的根元素,使用spring-beans-3.0.xsd语义约束 -->
<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="myService" class="com.cb.serviceImpl.MyServiceImpl">
</bean> <!-- controller -->
<bean id="loginAction" class="com.cb.action.LoginAction" scope="prototype">
<property name="ms" ref="myService"></property>
</bean>
<!-- <bean id="ms" class="com.cb.serviceImpl.MyServiceImpl"/> -->
</beans>

唯一的action如下,LoginAction.java

package com.cb.action;

import com.cb.serviceInter.MyService;
import com.opensymphony.xwork2.Action; public class LoginAction implements Action{ private String username;
private String password;
private String tip;
//系统所需业务逻辑组件
private MyService ms; public String getUsername() {
return username;
} public void setUsername(String username) {
this.username = username;
} public String getPassword() {
return password;
} public void setPassword(String password) {
this.password = password;
} public String getTip() {
return tip;
} public void setTip(String tip) {
this.tip = tip;
} public MyService getMs() {
return ms;
} public void setMs(MyService ms) {
this.ms = ms;
} public String execute() throws Exception {
System.out.println("************test1****"+ms+"名字是:"+getUsername());
if(ms.valid(getUsername(), getPassword())){
setTip("恭喜,整合成功!");
return SUCCESS;
}else {
setTip("对不起,登录失败!");
return ERROR;
}
} }

【分析】通过报的错误,可以明确的知道,private MyService ms;没有正常注入,就是这个实例是null。显然是spring的注入出现问题,但是我检查了所有的情况,发现ms的setMs也有,配置的也没有任何问题,这就奇怪了,怎么回一直提示注入失败呢?

【解决】最后发现一切问题出在struts.xml中,有两种解决办法:1,在<package name="login2" extends="struts-default" >中加上namespace=“/”。2,把action的<action name="login" class="loginAction">中name=“login”改成别的任何名字,估计有可能是login为struts2的保留字,所以以后的命名,尽量不要使用login,换个别的名字。但是第一种方法,加上namespace=“/”我始终没想明白是什么原因,加上命名空间了以后就没有错误,因为命名空间是一个逻辑名,访问action的时候,我不加逻辑名,应该不会有问题的。还是没想明白,希望各位看出问题的,能交流下,万分感谢!

java.lang.NullPointerException&com.cb.action.LoginAction.execute(LoginAction.java:48)的更多相关文章

  1. java.lang.NullPointerException org.apache.jsp.index_jsp._jspInit(index_jsp.java:22)

    java.lang.NullPointerException org.apache.jsp.index_jsp._jspInit(index_jsp.java:22) org.apache.jsp.i ...

  2. 整合ssh的时候出现空指针java.lang.NullPointerException

    转自:https://blog.csdn.net/koudailidexiaolong/article/details/9468857 HTTP Status 500 - type Exception ...

  3. CrashHandler: java.lang.NullPointerException

    08-29 20:33:47.305 20636-20636/com.tongyan.subway.inspect D/AndroidRuntime: Shutting down VM 08-29 2 ...

  4. Caused by: java.lang.NullPointerException, java.lang.reflect.InvocationTargetExc

    java.lang.reflect.InvocationTargetException at sun.reflect.NativeMethodAccessorImpl.invoke0(Native M ...

  5. Spring异常解决 java.lang.NullPointerException,配置spring管理hibernate时出错

    @Repository public class SysUerCDAO { @Autowired private Hibernate_Credit hibernate_credit; /** * 根据 ...

  6. JAVA错误:Exception in thread "main" java.lang.NullPointerException

    JAVA错误:Exception in thread "main" java.lang.NullPointerException例如: Exception in thread &q ...

  7. 报错!!!Servlet.service() for servlet [action] in context with path [/myssh] threw exception [java.lang.NullPointerException] with root cause java.lang.NullPointerException

    这个为什么报错啊~~ at com.hsp.basic.BasicService.executeQuery(BasicService.java:33) 这个对应的语句是   Query query = ...

  8. 报错:空指针java.lang.NullPointerException 原因 Action层 private UserService userservice 上未加@Autowire注解

    java.lang.NullPointerException at com.itheima.test.Test2.fun1(Test2.java:18) at sun.reflect.NativeMe ...

  9. 关于java.lang.NullPointerException: Module 'null' not found.的问题

    在用eclipse做struts1项目时,配置都ok了.可是一运行就报java.lang.NullPointerException: Module 'null' not found.错. 代码如下: ...

随机推荐

  1. 【OSG细节实现】节点围绕位于axisPos平行于axis的轴进行旋转

    //绕着与axis平行的任意轴旋转 void rotate(const std::string& name, float angle, osg::Vec3 axisPos, osg::Vec3 ...

  2. javascript里面技巧整理

    web develop tools secrets: http://jinlong.github.io/blog/2013/08/29/devtoolsecrets/ 1.Date new Date( ...

  3. JavaScript移除数组元素减少长度的方法

    JavaScript移除数组元素减少长度的方法,代码如下: //数组移除长度方法 var array=[];  array[0]="张三";  array[1]="李四& ...

  4. mysql手工注入

    以下是mynona本人原创的,奉献给大家,不要小看数据库注入 参考: http://www.daydaydata.com/help/sql/advance/limit.html http://www. ...

  5. 基于PinnedSectionListView实现联系人通讯录并且点击打电话

    PinnedSectionListView具体下载地址.使用方法和注意事项:http://www.cnblogs.com/zzw1994/p/4997601.html 怎么根据联系人姓名首字符顺序读取 ...

  6. CLR via C# 内存管理读书记

    1. CLR 垃圾回收采用基于代的机制, 在一次垃圾回收中存活下来的对象被提升到另一代 2. 在确认对象是否垃圾时,从一组根开始,根包括静态字段,方法参数,局部变量等 3. 使用CriticalFin ...

  7. How to achieve dialog with lookup control

    How to create a dialog with the lookup as a control, the other control SalesId ItemId lookup is the ...

  8. Dataguard之redo传输服务

    一.Data Guard架构 整个Data Guard体系就是围绕三个关键点展开: 日志发送(Redo Send) 日志接收(Redo Receive) 日志应用(Redo Apply) 二.日志发送 ...

  9. ENVI 5.1操作心得

    1.ENVI中计算的NDVI导出ARCGIS中识别的float数据类型,选择File——save as——erdas img.在ARCGIS中就能统计出NDVI信息 2.如何去掉Nan值从影像中心位置 ...

  10. OpenWRT 路由配置技巧

    随着最近 Google 在国内已经完全无法访问,使得通过 VPN 访问网络的需求更加强烈,本文介绍的方法可以使一个普通的路由具备稳定连接 VPN 的能力,并能够根据目标访问网站选择国内外线路,从而得到 ...