土豆你个番茄 2015-08-18 03:54 采纳率: 0%
浏览 1957
已结题

Spring mvc 空指针错误

LoginController

 @Controller
public class LoginController {

    @RequestMapping("test")
    public void login() {
        LoginServiceImp loginService = new LoginServiceImp();

        loginService.loadUserInfo();
    }
}

LoginServiceImp

 public class LoginServiceImp implements ILoginService {

    LoginDaoImp loginDao = new LoginDaoImp();

    /*
     * (non-Javadoc)
     * 
     * @see
     * com.sani.service.ILoginService#loadUserInfo(com.sani.bean.UserInfoBean)
     */
    public List loadUserInfo() {
        return loginDao.loadUserInfo();
    }
}

LoginDaoImp

 public class LoginDaoImp extends BaseDao implements ILoginDao {

    /*
     * (non-Javadoc)
     * 
     * @see com.sani.dao.ILoginDao#loadUserInfo()
     */
    public List<UserInfoBean> loadUserInfo() {
        String sql = "select NOW();";
        List tmpList = this.getJdbcTemplate().queryForList(sql);
        System.out.println(tmpList.size());
        return tmpList;
    }

}

BaseDao

 public class BaseDao extends JdbcDaoSupport {

    Logger log = Logger.getLogger(this.getClass().getName());

}

web.xml

 <?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
    http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

    <!-- 字符编码过滤器 -->
    <filter>
        <filter-name>encodingFilter</filter-name>
        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
        <init-param>
            <param-name>encoding</param-name>
            <param-value>UTF-8</param-value>
        </init-param>
        <init-param>
            <param-name>forceEncoding</param-name>
            <param-value>true</param-value>
        </init-param>
    </filter>

    <context-param>
        <param-name>log4jConfigLocation</param-name>
        <param-value>classpath:log4j.properties</param-value>
    </context-param>
    <context-param>
        <param-name>log4jRefreshInterval</param-name>
        <param-value>60000</param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
    </listener>

    <!--             应用上下文配置文件 -->
    <!-- Spring 容器加载 -->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:spring-db.xml</param-value>
    </context-param>

    <!-- 配置spring核心servlet -->
    <servlet>
        <servlet-name>spring</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:spring-servlet.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <!--
        url-pattern配置为/,不带文件后缀,会造成其它静态文件(js,css等)不能访问。如配为*.do,则不影响静态文件的访问
    -->
    <servlet-mapping>
        <servlet-name>spring</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

    <session-config>
        <session-timeout>30</session-timeout>
    </session-config>

    <!-- 欢迎页面 -->
    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>
</web-app>

spring-servlet.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:mvc="http://www.springframework.org/schema/mvc"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="
    http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-4.1.xsd
    http://www.springframework.org/schema/mvc
    http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd">

    <!-- 启动注解驱动的Spring MVC功能,注册请求url和注解POJO类方法的映射-->
    <mvc:annotation-driven />

    <!-- 允许对静态资源文件的访问 -->
    <mvc:default-servlet-handler />

    <!--
        启动包扫描功能,以便注册带有@Controller、@Service、@repository、@Component等注解的类成为spring的bean
    -->
    <context:component-scan base-package="com.sani.controller" />
    <context:component-scan base-package="com.sani.service" />
    <context:component-scan base-package="com.sani.dao" />

    <!-- 对模型视图名称的解析,在请求时模型视图名称添加前后缀 -->
    <!-- jsp视图解析器 -->
    <bean id="jspViewResolver"
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="viewClass"
            value="org.springframework.web.servlet.view.JstlView" />
        <property name="prefix" value="/WEB-INF/jsp/" />
        <property name="suffix" value=".jsp" />
        <property name="order" value="1" />
    </bean>
</beans>

spring-db.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:jaxws="http://cxf.apache.org/jaxws" xmlns:task="http://www.springframework.org/schema/task"
    xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
         http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
         http://www.springframework.org/schema/context
         http://www.springframework.org/schema/context/spring-context-3.0.xsd
         http://www.springframework.org/schema/aop
         http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
         http://www.springframework.org/schema/task 
         http://www.springframework.org/schema/task/spring-task-3.0.xsd  
         http://www.springframework.org/schema/tx 
         http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
          http://cxf.apache.org/jaxws 
          http://cxf.apache.org/schemas/jaxws.xsd"
    default-autowire="byName">

    <!--
        <context:property-placeholder location="classpath:jdbc.properties" />
    -->

    <!-- 配置数据源 -->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
        destroy-method="close" lazy-init="false">
        <property name="driverClass" value="com.mysql.jdbc.Driver" />
        <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/wms" />
        <property name="user" value="root" />
        <property name="password" value="123" />

        <!--连接池中保留的最小连接数。 -->
        <property name="minPoolSize">
            <value>5</value>
        </property>
        <!--连接池中保留的最大连接数。Default: 15 -->
        <property name="maxPoolSize">
            <value>30</value>
        </property>
        <!--初始化时获取的连接数,取值应在minPoolSize与maxPoolSize之间。Default: 3 -->
        <property name="initialPoolSize">
            <value>10</value>
        </property>
        <!--最大空闲时间,60秒内未使用则连接被丢弃。若为0则永不丢弃。Default: 0 -->
        <property name="maxIdleTime">
            <value>60</value>
        </property>
        <!--当连接池中的连接耗尽的时候c3p0一次同时获取的连接数。Default: 3 -->
        <property name="acquireIncrement">
            <value>5</value>
        </property>
        <!--
            JDBC的标准参数,用以控制数据源内加载的PreparedStatements数量。但由于预缓存的statements
            属于单个connection而不是整个连接池。所以设置这个参数需要考虑到多方面的因素。
            如果maxStatements与maxStatementsPerConnection均为0,则缓存被关闭。Default: 0
        -->
        <property name="maxStatements">
            <value>0</value>
        </property>
        <!--每60秒检查所有连接池中的空闲连接。Default: 0 -->
        <property name="idleConnectionTestPeriod">
            <value>60</value>
        </property>
        <!--定义在从数据库获取新连接失败后重复尝试的次数。Default: 30 -->
        <property name="acquireRetryAttempts">
            <value>30</value>
        </property>
        <!--
            获取连接失败将会引起所有等待连接池来获取连接的线程抛出异常。但是数据源仍有效
            保留,并在下次调用getConnection()的时候继续尝试获取连接。如果设为true,那么在尝试
            获取连接失败后该数据源将申明已断开并永久关闭。Default: false
        -->
        <property name="breakAfterAcquireFailure">
            <value>true</value>
        </property>
        <!--
            因性能消耗大请只在需要的时候使用它。如果设为true那么在每个connection提交的
            时候都将校验其有效性。建议使用idleConnectionTestPeriod或automaticTestTable
            等方法来提升连接测试的性能。Default: false
        -->
        <property name="testConnectionOnCheckout">
            <value>false</value>
        </property>
    </bean>

    <!-- 配置Jdbc模板 -->
    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"
        lazy-init="false">
        <property name="dataSource" ref="dataSource" />
    </bean>


    <bean id="baseDAO" class="com.sani.dao.imp.BaseDao" lazy-init="false">
        <property name="jdbcTemplate" ref="jdbcTemplate" />
    </bean>

</beans>

现在只要调用action—test就会空指针 是什么原因?应该这么改?

  • 写回答

2条回答 默认 最新

  • lxduu87 2015-08-18 05:03
    关注

    关键在于你哪里报空指针,this.getJdbcTemplate()这个?还是哪个地方报空指针,看你spring配置是没有问题 的

    评论

报告相同问题?

悬赏问题

  • ¥15 fluent的在模拟压强时使用希望得到一些建议
  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services
  • ¥15 关于#c语言#的问题:我现在在做一个墨水屏设计,2.9英寸的小屏怎么换4.2英寸大屏
  • ¥15 模糊pid与pid仿真结果几乎一样
  • ¥15 java的GUI的运用
  • ¥15 Web.config连不上数据库
  • ¥15 我想付费需要AKM公司DSP开发资料及相关开发。
  • ¥15 怎么配置广告联盟瀑布流
  • ¥15 Rstudio 保存代码闪退