SSM实现登录,退出

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

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

<form class="form-horizontal" method="post" id="frmLoginByAccount">
                <div class="modal-body">
                    <div class="form-group">
                        <label class="col-sm-3 control-label">用户名:</label>
                        <div class="col-sm-6">
                            <input class="form-control" type="text" placeholder="请输入用户名" name="loginName">
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-sm-3 control-label">密&nbsp;&nbsp;&nbsp;&nbsp;码:</label>
                        <div class="col-sm-6">
                            <input class="form-control" type="password" placeholder="请输入密码" name="password">
                        </div>
                    </div>
                </div>
                <div class="modal-footer" style="text-align: center">
                    <a class="btn-link">忘记密码?</a> &nbsp;
                    <button type="button" class="btn btn-warning" data-dismiss="modal" aria-label="Close">关&nbsp;&nbsp;闭</button>
                    <button type="button" class="btn btn-warning" onclick="loginByAccount()">登&nbsp;&nbsp;陆</button> &nbsp;&nbsp;
                    <a class="btn-link" id="btn-sms-back">短信快捷登录</a>
                </div>
            </form>

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(result.message);
                }
            }
        )
    }

mapper层:

 <!--列名跟属性名不太一样,定义resultMap-->
    <resultMap id="customerMapper" type="org.burningfire.imeetingroom.entity.Customer">
        <id property="id" column="id"/>
        <result property="name" column="name"/>
        <result property="loginName" column="login_name"/>
        <result property="phone" column="phone"/>
        <result property="address" column="address"/>
        <result property="isValid" column="is_valid"/>
        <result property="password" column="password"/>
        <result property="registDate" column="regist_date"/>
    </resultMap>

    <sql id="customerColumn">
        id,
        name,
        login_name,
        phone,
        address,
        is_valid,
        password,
        regist_date
    </sql>

    <select id="selectByLoginNameAndPassword" resultMap="customerMapper">
        select <include refid="customerColumn"/>
        from t_customer
        where login_name=#{loginName} and password=#{password} and is_valid=#{isValid}
    </select>

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();
    }
  • 3
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值