resources里所有配置文件和LoginInterceptor登录拦截器的实现,博主之前的文章里有一篇统一的放过代码,这里就不放了。另外StudentDao,Student,StudentExample都是通过mybatis逆向工程生成的,具体实现方法博主之前文章也有
StudentService
package com.etc.service;
import com.etc.dao.StudentDao;
import com.etc.entity.Student;
import com.etc.entity.StudentExample;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class StudentService {
@Autowired
private StudentDao studentDao;
public boolean login(Student student){
StudentExample example = new StudentExample();
example.createCriteria().andUsernameEqualTo(student.getUsername()).
andPasswordEqualTo(student.getPassword());
List result = studentDao.selectByExample(example);
return result.size()>0;
}
}
StudentController
package com.etc.controller;
import com.etc.entity.Student;
import com.etc.service.StudentService;
import org.omg.CORBA.Request;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
@Controller
public class StudentController {
@Autowired
private StudentService service;
@RequestMapping("/tologin")
public String tologin(){
System.out.println("tologin");
return "login";
}
/*
ModelAndView方法实现
@RequestMapping("/login")
public ModelAndView login(HttpSession session, ModelAndView modelAndView, Student student){
System.out.println("login:"+student.getUsername());
session.setAttribute("username",student.getUsername());
if (service.login(student)){
modelAndView.setViewName("list");
return modelAndView;
}else {
modelAndView.addObject("msg","用户名或密码错误");
modelAndView.setViewName("login");
return modelAndView;
}
}*/
@RequestMapping("/login")
public String login(HttpSession session, Model model, Student student){
System.out.println("login:"+student.getUsername());
session.setAttribute("username",student.getUsername());
if (service.login(student)){
return "list";
}else {
model.addAttribute("msg","用户名或密码错误");
return "login";
}
}
@RequestMapping("/logout")
public String logout(HttpSession session){
System.out.println("logout");
session.invalidate();
return "login";
}
}
index
<body>
<h2>Hello World!</h2>
<jsp:forward page="/WEB-INF/jsp/login.jsp"/>
</body>
login
${msg}
<%-- 提交后的位置:/WEB-INF/jsp/login.jsp--%>
<form action="${pageContext.request.contextPath }/login" method="post" >
姓名:<input id="username" type="text" name="username" />
<br />
密码:<input id="password" type="password" name="password" />
<br />
<input type="submit" value="登录" />
</form>
list
<form action="${pageContext.request.contextPath }/logout">
<input type="submit" value="注销" />
</form>