package controller;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import dao.UserDao;
import model.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import java.io.IOException;
@Controller
public class UserController {
@Autowired
private UserDao userDao;
@RequestMapping("/create")
public String create(@ModelAttribute("form")User users,Model model) throws IOException {
if((userDao.queryUser(users.getName()) == null)){
userDao.insertUser(users);
model.addAttribute("User",users);
return "login";
}else{
return "create";
}
}
@RequestMapping("/login")
public String login(HttpServletRequest request, HttpServletResponse response, @ModelAttribute("form") User User, Model model) throws IOException {
model.User tmpUser = userDao.queryUser(User.getName());
if(User.getPwd().equals(tmpUser.getPwd())){
model.addAttribute(tmpUser);
if (request.getParameter("save") != null) {
savePassword(response, tmpUser);
}
return "detail";
}else{
return "login";
}
}
public void savePassword(HttpServletResponse response, User users){
Cookie UserCookie = new Cookie("username", users.toString());
//设置Cookie的有效期为3天
UserCookie.setMaxAge(60 * 60 * 24 * 3);
response.addCookie(UserCookie);
}
}
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userController': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private dao.UserDao controller.UserController.userDao; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [dao.UserDao] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
xml version="1.0" ?>
mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
namespace="test">
select * from user where name=#{value}
id="insertUser" parameterType="model.User">
insert into user(name,pwd,age) values(#{name},#{pwd},#{age})
其中一个错误就是namespace的名字问题。mapper的namespace必须是与之映射的接口类全名,由于之前写的项目是自己new的接口实现类,然后再方法中调用sqlSession的方法。例如:
package dao;
import model.User;
public interface UserDao {
public User queryUser(String name);
public void insertUser(User User);
}
当配置成功后出现左侧的绿色箭头,在mapper文件中也有。当这个问题改好之后发现项目运行还是和原来一样的错误,那么现在唯一可能出错的地方就是spring配置文件的问题。
255
2
120000
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext-datasource.xml");
userDao = (UserDao) applicationContext.getBean("userDao");
Archetype Created Web Application
contextConfigLocation
classpath*:META-INF/applicationContext-datasource.xml
smart
org.springframework.web.servlet.DispatcherServlet
contextConfigLocation
classpath*:META-INF/applicationContext.xml
3
smart
/