Spring MVC控制层判断登录 ,登录失败则返回提示信息

给自己做个笔记,该文主要是利用session做传值的作用,都是些基本的就不多说了。

request.getSession().setAttribute("login", su);



java代码


package com.arvato.controller;


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.arvato.model.SaicUser;
import com.arvato.service.SaicUserService;


@Controller
public class SaicUserController {

@Autowired
private SaicUserService saicUserService;


@RequestMapping("saicUser/loginIn")
public ModelAndView loginIn(HttpServletRequest request,HttpServletResponse response) {

String name = request.getParameter("userName");
String pwd = request.getParameter("pw");
pwd = pwd.trim().toLowerCase();

//给密码加密
//String passMD5 = StringUtiles.getMD5(pwd);

//调用查询方法
SaicUser user = saicUserService.findSaicUser(name,pwd);

if(user == null){
ModelAndView mav = new ModelAndView("redirect:/login.jsp"); 
SaicUser su = new SaicUser();
su.setLoginInfo("用户名或密码不正确 !");
request.getSession().setAttribute("login", su);
    return mav; 
}

System.out.println("------》用户: " + name + " 登录成功 !");

ModelAndView mav = new ModelAndView("jsp/instrument/list"); 
    return mav; 
}


}





login.jsp代码



 


                  ${login.loginInfo}    //获取seesionsetAttribute()的值
                        <%session.removeAttribute("login");%>
                  
                        ${SPRING_SECURITY_LAST_EXCEPTION.message}
                        <%session.removeAttribute("SPRING_SECURITY_LAST_EXCEPTION");%>
 
 

你可能感兴趣的:(java,登录)