错误是:
Servlet [StaffSystem]的Servlet.service()在路径[/ StaffSystem]的上下文中引发了异常[请求处理失败;具有根本原因的嵌套异常是java.lang.NullPointerException] 显示java.lang.NullPointerException。
这是我的工具类: 包com.etc.web.imp;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import com.etc.web.model.User;
import com.etc.web.service.UserService;
public class UserServiceImp implements UserService{
private JdbcTemplate jdbcTemplate;
@Override
public void save(User user) {
// TODO Auto-generated method stub
jdbcTemplate.update("insert into user(username,pwd) values (?,?)",new Object[]{user.getUsername(),user.getPassword()});
}
@Override
public void del(User user) {
// TODO Auto-generated method stub
}
@Override
public void update(User user) {
// TODO Auto-generated method stub
}
@Override
public void searchAll() {
// TODO Auto-generated method stub
}
public JdbcTemplate getJdbcTemplate() {
return jdbcTemplate;
}
public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
}
@Override
public List<User> searchLogin(String username){
// TODO Auto-generated method stub
List<User> users = null;
Object[] params = new Object[] { username };
String sql = "select * from user where username = ?";
users = (List<User>) jdbcTemplate.queryForObject(sql, params, new UserRowMapper());
return (users != null && users.size() > 0) ? users : null;
}
}
和服务类: 包com.etc.web.service;
import java.util.List;
import com.etc.web.model.User;
public interface UserService {
void save(User user);
void del(User user);
void update(User user);
void searchAll();
List<User> searchLogin(String username);
}
接下来是控制器类: 包com.etc.web.controller;
import javax.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
import com.etc.web.imp.UserServiceImp;
import com.etc.web.model.User;
import com.etc.web.service.UserService;
@Controller
@RequestMapping("/user")
public class UserController {
UserService userService;
@RequestMapping(value="/toIndex",method=RequestMethod.POST)
public ModelAndView toIndex (HttpServletRequest request){
String username = request.getParameter("username");
userService.searchLogin(username);
return new ModelAndView("/index");
}
}
RowMapper: 包com.etc.web.imp;
import java.sql.ResultSet;
import java.sql.SQLException;
import org.springframework.jdbc.core.RowMapper;
import com.etc.web.model.User;
public class UserRowMapper implements RowMapper<User> {
@Override
public User mapRow(ResultSet rs, int value) throws SQLException {
// TODO Auto-generated method stub
User user = new User();
user.setUsername(rs.getString("username"));
user.setPassword(rs.getString("password"));
return user;
}
}
最后这是我的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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
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/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
<!-- 静态资源访问 -->
<mvc:default-servlet-handler/>
<!-- 扫描包 -->
<context:component-scan base-package="com.etc.web.controller" />
<!-- 添加注解驱动 -->
<mvc:annotation-driven />
<bean id="ViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
<!-- apache.dbcp连接池的配置 -->
<bean id="dataSource"
class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName"
value="com.mysql.jdbc.Driver">
</property>
<property name="url" value="jdbc:mysql://localhost:3306/test"></property>
<property name="username" value="root"></property>
<property name="password" value="123456"></property>
<property name="maxActive" value="100"></property>
<property name="maxIdle" value="30"></property>
<property name="maxWait" value="500"></property>
<property name="defaultAutoCommit" value="true"></property>
</bean>
<!-- jdbc注入 -->
<bean id="jdbcTemplate"
class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource">
<ref bean="dataSource" />
</property>
</bean>
<!-- dao注入 -->
<bean id="userService" class="com.etc.web.imp.UserServiceImp">
<property name="jdbcTemplate" ref="jdbcTemplate"></property>
</bean>
</beans>
我正在学习spring-mvc只有两天。这个问题困扰了我一整天。