网上商城项目SSM(五)——后台管理员登录

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_40788630/article/details/85479676

后台管理员登录实现是按照——持久层→dao层→业务层(service层)→控制层(controller层)→表面层(jsp层)

的顺序实现的。由于相关的持久层在第三篇博客的时候已经全部建立,所以直接从dao层开始

一、实现dao文件AdminDao接口

创建com.dao包,在包中创建AdminDao接口,其代码如下:

package com.dao;
import java.util.List;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.stereotype.Repository;
import com.po.Auser;
@Repository("adminDao")
@Mapper
public interface AdminDao {
    public List<Auser> login(Auser auser);
}

在com.dao包中创建AdminDao接口对应的mybatis映射文件,其名称应和接口同名

AdminDao.xml代码如下:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.dao.AdminDao">
    <!-- 查询用户信息 -->
    <select id="login" resultType="Auser" parameterType="Auser">
        select * from ausertable where aname=#{aname} AND apwd=#{apwd}
    </select>
</mapper>

二、实现service层

此层共有两个文件,一个接口文件,一个接口实现类文件

在src目录下创建com.service.admin包,并在包中创建AdminService接口,其代码如下所示:

package com.service.admin;
import javax.servlet.http.HttpSession;
import org.springframework.ui.Model;
import com.po.Auser;

public interface AdminService {
    public String login(Auser auser,Model model,HttpSession session);
}

其接口实现类如下:

package com.service.admin;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;

import com.dao.AdminDao;
import com.po.Auser;

public class AdminServiceImpl implements AdminService {
    @Autowired
    private AdminDao adminDao;
    @Autowired
    private AdminTypeDao adminTypeDao;
	@Override
	public String login(Auser auser, Model model, HttpSession session) {
		if(adminDao.login(auser) != null && adminDao.login(auser).size()>0) {
			session.setAttribute("auser", auser);
			//添加商品与修改商品页面使用
			session.setAttribute("goodsType", adminTypeDao.selectGoodsType());
			return "admin.main";
		}
		model.addAttribute("msg","用户名或密码错误!");
		return "admin/login";
	}

}

其中AdminTypeDao是后台操作商品类型的dao层类,这个稍后进行实现

三、实现控制层(controller层)

创建com.controller.admin包,在包中创建AdminController类,其代码如下所示:

package com.controller.admin;
import javax.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import com.po.Auser;
import com.service.admin.AdminService;

@Controller
public class AdminController {
    @Autowired
    private AdminService adminService;
    @RequestMapping("/admin")
    public String toLogin(@ModelAttribute Auser auser) {
    	return "admin/login";
    }
    @RequestMapping("/admin/login")
    public String login(@ModelAttribute Auser auser,Model model,HttpSession session) {
    	return adminService.login(auser, model, session);
    }
    @RequestMapping("/exit")
    public String exit(@ModelAttribute Auser auser,HttpSession session) {
    	session.invalidate();
    	return "admin/login";
    }
}

四、实现jsp层

在jsp.admin层创建login.jsp页面,具体代码如下:

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
  <head>  
  <base href="<%=basePath%>">
  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>后台登录</title>
	<style type="text/css">
	table{
		text-align: center;
	}
	.textSize{
		width: 120px;
		height: 25px;
	}
	* {
		margin: 0px;
		padding: 0px;
	}
	body {
		font-family: Arial, Helvetica, sans-serif;
		font-size: 12px;
		margin: 10px 10px auto;
		background-image: url(images/admin/bb.jpg);
	}
	</style>
	<script type="text/javascript">
	//确定按钮
	function gogo(){
		document.forms[0].submit();
	}
	//取消按钮
	function cancel(){
		document.forms[0].action = "";
	}
	</script>
  </head>
  <body>
  	<form:form action="admin/login" modelAttribute="auser" method="post">
	<table>
		<tr>
			<td colspan="2"><img src="images/admin/login.gif"></td>
		</tr>
		<tr>
			<td>姓名:</td>
			<td>
				<form:input path="aname" cssClass="textSize"/>
			</td>
		</tr>
		<tr>
			<td>密码:</td>
			<td>
				<form:password path="apwd" cssClass="textSize" maxlength="20"/>
			</td>
		</tr>
		<tr>
			<td colspan="2">
				<input type="image" src="images/admin/ok.gif" onclick="gogo()" >
				<input type="image" src="images/admin/cancel.gif" onclick="cancel()" >
			</td>
		</tr>
	</table>
	</form:form>
	${msg }
  </body>
</html>

到这,管理员登录的相关操作就算全部完成了。

实现的页面如图所示:

 登录进入之后如下所示:

猜你喜欢

转载自blog.csdn.net/qq_40788630/article/details/85479676