使用spring时,无法注入(提示:java.lang.NullPointerException)

异常信息如下:
org.springframework.web.util.NestedServletException: Request processing failed; nested exception is java.lang.NullPointerException
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:408)
org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:360)
javax.servlet.http.HttpServlet.service(HttpServlet.java:710)
javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:78)
org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:77)
root cause
java.lang.NullPointerException
com.test.dao.jdbc.UserDAO.insert(UserDAO.java:11)
com.test.controller.LoginController.onSubmit(LoginController.java:21)
org.springframework.web.servlet.mvc.SimpleFormController.onSubmit(SimpleFormController.java:408)
org.springframework.web.servlet.mvc.SimpleFormController.onSubmit(SimpleFormController.java:380)
org.springframework.web.servlet.mvc.SimpleFormController.processFormSubmission(SimpleFormController.java:267)
org.springframework.web.servlet.mvc.AbstractFormController.handleRequestInternal(AbstractFormController.java:250)
org.springframework.web.servlet.mvc.AbstractController.handleRequest(AbstractController.java:153)
org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter.handle(SimpleControllerHandlerAdapter.java:45)
org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:820)
org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:755)
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:396)
org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:360)
javax.servlet.http.HttpServlet.service(HttpServlet.java:710)
javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:78)
org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:77)

web.xml

<display-name>TestDemo</display-name>

&lt;context-param&gt;
    &lt;param-name&gt;contextConfigLocation&lt;/param-name&gt;
    &lt;param-value&gt;/WEB-INF/applicationContext.xml&lt;/param-value&gt;
&lt;/context-param&gt;

&lt;servlet&gt;
    &lt;servlet-name&gt;context&lt;/servlet-name&gt;
    &lt;servlet-class&gt;org.springframework.web.context.ContextLoaderServlet&lt;/servlet-class&gt;
    &lt;load-on-startup&gt;1&lt;/load-on-startup&gt;
&lt;/servlet&gt;

&lt;filter&gt;
    &lt;filter-name&gt;encodingFilter&lt;/filter-name&gt;
    &lt;filter-class&gt;
        org.springframework.web.filter.CharacterEncodingFilter
    &lt;/filter-class&gt;
    &lt;init-param&gt;
        &lt;param-name&gt;encoding&lt;/param-name&gt;
        &lt;param-value&gt;UTF-8&lt;/param-value&gt;
    &lt;/init-param&gt;
&lt;/filter&gt;

&lt;filter-mapping&gt;
    &lt;filter-name&gt;encodingFilter&lt;/filter-name&gt;
    &lt;url-pattern&gt;/*&lt;/url-pattern&gt;
&lt;/filter-mapping&gt;

&lt;servlet&gt;
    &lt;servlet-name&gt;dispatcherServlet&lt;/servlet-name&gt;
    &lt;servlet-class&gt;org.springframework.web.servlet.DispatcherServlet&lt;/servlet-class&gt;
    &lt;load-on-startup&gt;1&lt;/load-on-startup&gt;
&lt;/servlet&gt;

&lt;servlet-mapping&gt;
    &lt;servlet-name&gt;dispatcherServlet&lt;/servlet-name&gt;
    &lt;url-pattern&gt;*.do&lt;/url-pattern&gt;
&lt;/servlet-mapping&gt;

applicationContext.xml:

<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.microsoft.jdbc.sqlserver.SQLServerDriver">
<property name="url" value="jdbc:sqlserver://localhost:1433;databaseName=test">
<property name="username" value="sa">
<property name="password" value="">
</bean>

&lt;bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"&gt;
    &lt;property name="dataSource" ref="dataSource"&gt;&lt;/property&gt;
    &lt;property name="mappingResources"&gt;
        &lt;list&gt;
            &lt;value&gt;com/test/domain/User.hbm.xml&lt;/value&gt;
        &lt;/list&gt;
    &lt;/property&gt;
    &lt;property name="hibernateProperties"&gt;
        &lt;props&gt;
            &lt;prop key="hibernate.dialect"&gt;org.hibernate.dialect.SQLServerDialect&lt;/prop&gt;
        &lt;/props&gt;
    &lt;/property&gt;
&lt;/bean&gt;

&lt;bean id="userDAO" class="com.test.dao.jdbc.UserDAO"&gt;
    &lt;property name="sessionFactory" ref="sessionFactory"&gt;
&lt;/bean&gt;

dispatcherServlet-servlet.xml:
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/"></property>
<property name="suffix" value=".jsp"></property>
</bean>

&lt;bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping"&gt;
    &lt;property name="mappings"&gt;
        &lt;props&gt;
            &lt;prop key="/login.do"&gt;loginController&lt;/prop&gt;
        &lt;/props&gt;
    &lt;/property&gt;
&lt;/bean&gt;

&lt;bean id="loginController" class="com.test.controller.LoginController"&gt;
    &lt;property name="successView" value="/admin/success"&gt;&lt;/property&gt;
    &lt;property name="formView" value="/admin/login"&gt;&lt;/property&gt;
&lt;/bean&gt;

UserDAO.java:

public class UserDAO extends BaseDao implements IUserDAO {

public void insert(User user) {
    System.out.println("hibernateTemplate: " + this.getHibernateTemplate());
    this.getHibernateTemplate().update("INSERT INTO USERPASS (USERNAME, PASSWORDS) VALUES ('" + user.getUserName() +"', '" + user.getPassword() + "')");
}

public User find(Integer id) {
    List rows = this.getHibernateTemplate().find("SELECT * FROM USERPASS WHERE ID=" + id.intValue());
    Iterator it = rows.iterator();
    if(it.hasNext()) {
        Map userMap = (Map)it.next();
        User user = new User();
        user.setUserName(userMap.get("USERNAME").toString());
        user.setPassword(userMap.get("PASSWORDS").toString());
        return user;
    }
    return null;
}

}

查看全部
xdqcx
xdqcx
2008/11/05 09:36
  • spring
  • 点赞
  • 收藏
  • 回答
    私信
满意答案
查看全部

0个回复