SSM实现登录,退出 您所在的位置:网站首页 退出登录代码 SSM实现登录,退出

SSM实现登录,退出

2024-06-21 19:10| 来源: 网络整理| 查看: 265

在拼命学完ssm一周后,对所学情况进行总结,以便于巩固:

前端使用form表单进行登录:起了id为frmLoginByAccount,使用ajax发送请求进行处理

用户名: 密;;;;码: 忘记密码? ; 关;;闭 登;;陆 ;; 短信快捷登录

ajax请求:

//根据用户名和账号登录 function loginByAccount(){ $.post( '${pageContext.request.contextPath}/front/customer/loginByAccount', //表单序列化获取数据 $('#frmLoginByAccount').serialize(), function(result){ if(result.status==1){ //刷新页面 location.href='${pageContext.request.contextPath}/front/product/search'; }else{ $('#loginInfo').html(resulssage); } } ) }

mapper层:

id, name, login_name, phone, address, is_valid, password, regist_date select from t_customer where login_name=#{loginName} and password=#{password} and is_valid=#{isValid}

dao层:

//在数据库这边登录不一定只是Service层中的参数,还得判断数据库中该用户是否有效 public Customer selectByLoginNameAndPassword(@Param("loginName") String loginName, @Param("password") String password, @Param("isValid") Integer isValid);

service层:

public interface CustomerService { public Customer login(String loginName,String password) throws LoginErrorException; }

serviceImpl层:

@Service @Transactional(propagation = Propagation.REQUIRED,rollbackFor = Exception.class) public class CustomerServiceImpl implements CustomerService { @Autowired private CustomerMapper customerMapper; @Override @Transactional(propagation = Propagation.SUPPORTS,readOnly = true) public Customer login(String loginName, String password) throws LoginErrorException { Customer customer = customerMapper.selectByLoginNameAndPassword(loginName, password, CustomerConstant.CUSTOMER_VALID); if(customer == null) { throw new LoginErrorException("登录失败,用户名或密码错误"); } return customer; } }

controller层:

@Autowired private CustomerService customerService; @RequestMapping("/loginByAccount") @ResponseBody public ResponseResult loginByAccount(String loginName, String password, HttpSession session) { ResponseResult result = new ResponseResult(); Customer customer = null; try { customer = customerService.login(loginName, password); session.setAttribute("customer",customer); result.setStatus(ResponseStatusConstant.Response_STATUS_SUCCESS); } catch (LoginErrorException e) { // e.printStackTrace(); result.setStatus(ResponseStatusConstant.Response_STATUS_FAILURE); result.setMessage(e.getMessage()); } return result; } 而退出操作,只需要清除session即可

ajax请求:

//退出 function logout(){ $.post( '${pageContext.request.contextPath}/front/customer/logout', function(result){ if(result.status==1){ location.href='${pageContext.request.contextPath}/front/product/search'; } } ) }

controller层:

@RequestMapping("/logout") @ResponseBody public ResponseResult logout(HttpSession session) { session.invalidate(); return ResponseResult.success(); }


【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有