问题是在:
Dao类是不能直接new出来的必须是通过 ApplicationContext applicationContext= new
ClassPathXMLApplicationContext(“springmvc.xml”)erDao dao = (UserDao) ac.getBean("UserDaoId
得到类UserDao的实例化
从而JdbcTemplate 的值才能获得
否则JdbcTemplate的值为null
package springmvc.Servlet;
import java.io.UnsupportedEncodingException;
import javax.servlet.http.HttpServletRequest;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestMapping;import springmvc.Dao.UserDao;
import springmvc.bean.User;@Service
@Controller
public class Servlet {@RequestMapping("/insert")
public String insert(User user,HttpServletRequest request) {
ApplicationContext ac=new ClassPathXmlApplicationContext("springmvc.xml");
UserDao dao = (UserDao) ac.getBean("UserDaoId");
try {
request.setCharacterEncoding("utf-8");
String id = request.getParameter("userid");
String name = request.getParameter("username");
String password = request.getParameter("userpassword");
user = new User(id, name, password);
System.out.println(user);
boolean result =dao.insert(user);
if (result) {
return "success";
}else {
return "defult" ;
}
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null ;
}
}
}