问题描述
config.xml<?xmlversion="1.0"encoding="UTF-8"?><beansxmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:p="http://www.springframework.org/schema/p"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-3.0.xsd"><beanid="viewResolver"class="org.springframework.web.servlet.view.InternalResourceViewResolver"><propertyname="viewClass"><value>org.springframework.web.servlet.view.JstlView</value></property><propertyname="prefix"><value>/view/</value></property><propertyname="suffix"><value>.jsp</value></property></bean><beanid="urlMapping"class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping"><propertyname="mappings"><props><propkey="/TestAction.do">TestAction</prop></props></property></bean><beanid="TestAction"class="test.TestAction"><propertyname="commandClass"><value>test.TestForm</value></property><propertyname="fail_view"><value>fail</value></property><propertyname="success_view"><value>main</value></property></bean><beanid="dataSource"class="org.apache.commons.dbcp.BasicDataSource"destroy-method="close"><propertyname="driverClassName"value="com.mysql.jdbc.Driver"></property><propertyname="url"value="jdbc:mysql://localhost:3306/test"></property><propertyname="username"value="root"></property><propertyname="password"value="aa5524929"></property><!--最大活动连接数--><propertyname="maxActive"value="100"></property><!--最大可空闲连接数--><propertyname="maxIdle"value="30"></property><!--最大可等待连接数--><propertyname="maxWait"value="500"></property><!--默认的提交方式(如果不需要事务可以设置成true,在实际应用中一般设置为false,默认为false)--><propertyname="defaultAutoCommit"value="true"></property></bean><!--jdbc注入--><beanid="jdbcTemplate"class="org.springframework.jdbc.core.JdbcTemplate"><propertyname="dataSource"><refbean="dataSource"/></property></bean><!--dao注入--><beanid="TestFormDAO"class="daoimp.TestFormDAOIMP"><propertyname="jdbcTemplate"ref="jdbcTemplate"></property></bean></beans>daopackagedaoimp;importorg.springframework.jdbc.core.JdbcTemplate;importdao.TestFormDAO;importtest.TestForm;publicclassTestFormDAOIMPimplementsTestFormDAO{privateJdbcTemplatejdbcTemplate;publicvoiddel(TestFormform){//TODOAuto-generatedmethodstub}publicvoidsave(TestFormform){//TODOAuto-generatedmethodstubSystem.out.println(form.getName());this.getJdbcTemplate().update("insertintouservalues(?,?)",newObject[]{form.getName(),form.getEmail()});}publicvoidsearchAll(){//TODOAuto-generatedmethodstub}publicvoidupdate(TestFormform){//TODOAuto-generatedmethodstub}publicJdbcTemplategetJdbcTemplate(){returnjdbcTemplate;}publicvoidsetJdbcTemplate(JdbcTemplatejdbcTemplate){this.jdbcTemplate=jdbcTemplate;}}
解决方案
解决方案二:
使用jdbctempletedao继承JdbcDaoSupport就可以使用了不用这样
解决方案三:
另外这样写,你把异常贴出来看看。
解决方案四:
201410:08:12下午org.apache.catalina.core.StandardWrapperValveinvoke严重:Servlet.service()forservlet[Dispatcher]incontextwithpath[/2014-7-14-3]threwexception[Requestprocessingfailed;nestedexceptionisjava.lang.NullPointerException]withrootcausejava.lang.NullPointerExceptionatdaoimp.TestFormDAOIMP.save(TestFormDAOIMP.java:18)attest.TestAction.onSubmit(TestAction.java:27)atorg.springframework.web.servlet.mvc.SimpleFormController.onSubmit(SimpleFormController.java:387)atorg.springframework.web.servlet.mvc.SimpleFormController.processFormSubmission(SimpleFormController.java:272)atorg.springframework.web.servlet.mvc.AbstractFormController.handleRequestInternal(AbstractFormController.java:268)atorg.springframework.web.servlet.mvc.AbstractController.handleRequest(AbstractController.java:153)atorg.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter.handle(SimpleControllerHandlerAdapter.java:48)atorg.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:771)atorg.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:716)atorg.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:644)atorg.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:560)atjavax.servlet.http.HttpServlet.service(HttpServlet.java:647)atjavax.servlet.http.HttpServlet.service(HttpServlet.java:728)atorg.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)atorg.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)atorg.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)atorg.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)atorg.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)atorg.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)atorg.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)atorg.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:947)atorg.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)atorg.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)atorg.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1009)atorg.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589)atorg.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:312)atjava.util.concurrent.ThreadPoolExecutor.runWorker(UnknownSource)atjava.util.concurrent.ThreadPoolExecutor$Worker.run(UnknownSource)atjava.lang.Thread.run(UnknownSource)