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代码



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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值