package com.zt.controller;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
import com.zt.bean.Student;
import com.zt.service.StudentService;
@Controller
public class StudentController {
@Autowired
private StudentService studentService;
/**
* 根据用户名查询集合
* @return 用户集合
*/
@RequestMapping(value="/login",method=RequestMethod.GET)
public ModelAndView loginForm(String user)
{
ModelAndView mav = new ModelAndView("login");
List<Student> ls=studentService.findAll();
mav.getModelMap().put("list", ls);
return mav;
}
/**
* 根据用户名查询用户集合
* @param name 用户名
* @return 用户集合
*/
@RequestMapping(value="/getName" ,method=RequestMethod.POST)
public ModelAndView loginFormByName(String name){
ModelAndView mav = new ModelAndView("login");
System.out.println("==========================================");
List<Student> ls=studentService.searchContacts(name);
mav.getModelMap().put("list", ls);
return mav;
}
/**
* 根据传入的用户id跳转到用户修改页面
* @param id 用户id
* @return 讲选中的用户信息传到修改页面上
*/
@RequestMapping(value="/getStu",method=RequestMethod.GET)
public ModelAndView getStu(int id){
ModelAndView mav = new ModelAndView("update");
Student stu=studentService.getById(id);
mav.getModelMap().put("student", stu);
return mav;
}
/**
*修改用户信息
* @param id 要修改用户的id
* @param name 修改用户的用户名
* @param psw 修改后的密码
* @return
*/
@RequestMapping(value="/updateStu",method=RequestMethod.POST)
public String update(int id,String name,String psw){
Student stud=studentService.getById(id);
stud.setName(name);
stud.setPsw(psw);
studentService.update(stud);
return "redirect:login.do";
}
/**
* 根据传入的id删除用户
* @param id 用户id
* @return 删除成功后返回用户集合页面
*/
@RequestMapping(value="/delStu",method=RequestMethod.GET)
public String del(int id){
studentService.delete(id);
return "redirect:login.do";
}
/**
* 跳转到添加页面
* @return
*/
@RequestMapping("add")
public String addjsp(){
return "add";
}
/**
* 添加用户
* @param name 用户名
* @param psw 密码
* @return 添加成功后跳转到显示用户界面
*/
@RequestMapping(value="/addStu" ,method=RequestMethod.POST)
public String add(String name,String psw){
Student stu=new Student(name,psw);
studentService.save(stu);
return "redirect:login.do";
}
/**
* 注册用户
* @param name 用户名
* @param psw 密码
* @return 注册成功后 返回到登录页面
*/
@RequestMapping(value="/regaddStu" ,method=RequestMethod.POST)
public String regadd(String name,String psw){
Student stu=new Student(name,psw);
studentService.save(stu);
return "index";
}
/**
* 登录
* @param name 用户名
* @param psw 密码
* @param session 用session保存用户
* @return 登录成功后 返回到显示用户界面
*/
@RequestMapping(value="/gologin",method=RequestMethod.POST)
public String gologin(String name,String psw,HttpSession session){
if(studentService.loginStudent(name, psw)){
session.setAttribute("user", name);
System.out.println(name+"------===controller");
return "redirect:login.do";
}else {
return "redirect:mes.do";
}
}
/**
* 用户登录失败 返回错误信息
* @return
*/
@RequestMapping(value="/mes")
public ModelAndView mes(){
ModelAndView mav=new ModelAndView("index");
mav.getModelMap().put("message", "用户名或密码错误!");
return mav;
}
/**
* 退出
* @param request
* @return
*/
@RequestMapping("exit")
public ModelAndView exit(HttpServletRequest request){
ModelAndView mav = new ModelAndView("index");
request.getSession().removeAttribute("user");
return mav;
}
/**
* 验证用户名是否存在
* @param response
* @param request
* @return
* @throws IOException
*/
@RequestMapping("isExistName")
public String isName(HttpServletResponse response,HttpServletRequest request) throws IOException{
PrintWriter out = response.getWriter();
if(request.getParameter("username")==""){
request.getSession().setAttribute("flag", "请输入查询的名字!");
} else if(studentService.isExistName(request.getParameter("username"))){
request.getSession().setAttribute("flag", "该用户名可以使用!");
out.print(true);
}else{
request.getSession().setAttribute("flag", "该用户名已存在!");
out.print(false);
}
return "redirect:login.do";
}
}
- 1
- 2
前往页