报错信息
[code="java"]
java.lang.NullPointerException
at com.dareway.apps.demo.test.TestController.save(TestController.java:52)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:585)
at com.dareway.apps.common.webcontroller.MultiActionController.invokeNamedMethod(MultiActionController.java:456)
at com.dareway.apps.common.webcontroller.MultiActionController.handleRequestInternal(MultiActionController.java:375)
at org.springframework.web.servlet.mvc.AbstractController.handleRequest(AbstractController.java:153)
at org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter.handle(SimpleControllerHandlerAdapter.java:48)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:857)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:792)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:475)
at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:440)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:709)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:237)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:157)
at com.dareway.apps.common.webcontroller.SafetyFilter.doFilter(SafetyFilter.java:114)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:186)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:157)
at com.dareway.apps.common.webcontroller.EncodingFilter.doFilter(EncodingFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:186)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:157)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:214)
at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:104)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:520)
at org.apache.catalina.core.StandardContextValve.invokeInternal(StandardContextValve.java:198)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:152)
at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:104)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:520)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:137)
at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:104)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:117)
at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:102)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:520)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:104)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:520)
at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:929)
at org.apache.coyote.tomcat5.CoyoteAdapter.service(CoyoteAdapter.java:160)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:799)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.processConnection(Http11Protocol.java:705)
at org.apache.tomcat.util.net.TcpWorkerThread.runIt(PoolTcpEndpoint.java:577)
at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:683)
at java.lang.Thread.run(Thread.java:595)
[/code]
web.xml中的配置
[code="java"]
....
org.springframework.web.context.request.RequestContextListener
org.springframework.web.context.ContextLoaderListener
....
[/code]
applicationContext.xml中的配置:
[code="java"]
....
<bean id="testDemo"
class="com.dareway.apps.demo.test.TestDemo">
</bean>
<bean id="TestController"
class="com.dareway.apps.demo.test.TestController">
<property name="testDemo">
<ref bean="testDemo" />
</property>
</bean>
....
[/code]
TestDemo.java的内容:
[code="java"]
public class TestDemo implements TestInterface {
public void printDemo() {
System.out.print("测试成功");
}
}
[/code]
TestInterface 的内容:
[code="java"]
public interface TestInterface {
public void printDemo();
}
[/code]
testController中的内容:
[code="java"]
public class TestController extends BizDispatchControler {
private TestInterface testDemo;
public ModelAndView save(HttpServletRequest request,
HttpServletResponse response, TestDTO dto) throws Exception {
...
// WebApplicationContext applicationContext = WebApplicationContextUtils
// .getWebApplicationContext(request.getSession().getServletContext());
// TestDemo testDemo=(TestDemo) applicationContext.getBean("testDemo");//这样我就可以
// 得到非空的testdemo。但是这不是setter注入吧?
testDemo.printDemo();//报空指针的地方
...
return null;
}
public void setTestDemo(TestInterface testDemo) {
this.testDemo = testDemo;
}
}
[/code]
不知道这样配置到底是那个地方出了问题?
[b]问题补充:[/b]
[code="java"]
package com.dareway.apps.demo.test;
import java.util.Collection;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
import org.springframework.web.servlet.ModelAndView;
import com.dareway.apps.common.ClassHelper;
import com.dareway.apps.common.exception.ApplicationException;
import com.dareway.apps.common.log.BizLogHandler;
import com.dareway.apps.common.pub.util.DataObject;
import com.dareway.apps.common.pub.util.DataStore;
import com.dareway.apps.common.webcontroller.BizDispatchControler;
import com.dareway.apps.common.webcontroller.Delegator;
public class TestController extends BizDispatchControler {
private TestInterface testDemo;
public ModelAndView enterTest(HttpServletRequest request,
HttpServletResponse response, TestDTO dto) throws Exception {
return new ModelAndView("test");
}
public ModelAndView query(HttpServletRequest request,
HttpServletResponse response, TestDTO dto) throws Exception {
DataObject pdo = new DataObject();
DataStore vds = null;
try {
pdo.put("dto", dto);
Delegator dele = new Delegator();
DataObject result = dele.execute(
"com.dareway.apps.demo.test.TestBPO", "query", pdo,
getUser(request));// 用自己的bpo以及方法。
vds = (DataStore) result.get("vds");
} catch (ApplicationException e) {
BizLogHandler.saveBizLog(request, e.getMessage());
return this.showErrorMessage(e.getMessage());
}
return this.flashDataWindow(request, vds);
}
public ModelAndView save(HttpServletRequest request,
HttpServletResponse response, TestDTO dto) throws Exception {
DataObject pdo = new DataObject();
// WebApplicationContext applicationContext = WebApplicationContextUtils
// .getWebApplicationContext(request.getSession().getServletContext());
//TestDemo testDemo=(TestDemo) applicationContext.getBean("testDemo");
testDemo.printDemo();
// try {
Collection entities = ClassHelper.getEntities(request,TestDTO.class,"testdatawindow3");
pdo.put("dto", dto);
pdo.put("entities", entities);
Delegator dele = new Delegator();
return alertSuccessMessage("保存补贴成功。");
}
// public TestInterface getTestDemo() {
// return testDemo;
// }
//
public void setTestDemo(TestInterface testDemo) {
this.testDemo = testDemo;
}
}
[/code]
这是测试程序的源程序,我做了删改,所以行数可能不对。
我的问题是为什么注入不了。
报的问题就是testDemo=null
[b]问题补充:[/b]
[code="java"]
<bean id="testDemo"
class="com.dareway.apps.demo.test.TestDemo">
</bean>
<bean id="TestController"
class="com.dareway.apps.demo.test.TestController">
<property name="testDemo">
<ref bean="testDemo" />
</property>
</bean>
[/code]
这个是正确的,如果不正确的 在tomcat启动的应该就报错吧?
楼下的让我改成
class="com.dareway.apps.demo.test.TestInterface">
TestInterface是个接口 这样不对吧?
[b]问题补充:[/b]
你的controller不是由spring创建的。。。
它继承的是MultiActionController
这样不行吗?