1. 程式人生 > >SSM (IDEA) —— 實現登入登出

SSM (IDEA) —— 實現登入登出

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>