登录功能开发
页面导入
拷贝静态资源
创建登录页面
相对路径与绝对路径
绝对路径:固定不变的路径。
c:<u>temp</u>1.<u>jsp</u>
http://19.168.137.3:8080/xxx/yyy.jsp
<jsp:forward page="/index"><u></jsp:forward></u>
相对路径:与当前请求所访问的路径相关,是可变的路径。
./1.<u>jsp</u>
../1.<u>jsp</u>
../../1.<u>jsp</u>
<a <u>href</u>="./yyy.jsp"><u>xxx</u></a>
<a <u>href</u>="../yyy.jsp"><u>xxx</u></a>
前台路径:浏览器端发起的请求路径。
不以斜杠开头表示相对路径。
<link <u>rel</u>="<u>stylesheet</u>"
<u>href</u>="static/bootstrap/<u>css</u>/bootstrap.min.css">
以斜杠开头,表示从服务器的根(ROOT)进行资源查找。
<link <u>rel</u>="<u>stylesheet</u>"
<u>href</u>="/static/bootstrap/<u>css</u>/bootstrap.min.css">
以上下文路径开头,表示从当前应用程序的根(<u>atcrowdfunding</u>-main)进行资源查找。
<link <u>rel</u>="<u>stylesheet</u>"
<u>href</u>="${pageContext.request.contextPath}/static/bootstrap/<u>css</u>/bootstrap.min.css">
后台路径:服务器端发起资源请求路径。
<jsp:forward page="/index"><u></jsp:forward></u>
项目启动监听器
自定义监听器
package com.atguigu.atcrowdfunding.listener;
import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.atguigu.atcrowdfunding.util.Const;
/**
* 监听application对象创建和销毁
*
* @author Administrator
*
*/
public class SystemUpInitListener implements ServletContextListener {
Logger log = LoggerFactory.getLogger(SystemUpInitListener.class);
// 当application销毁时执行销毁方法。
@Override
public void contextDestroyed(ServletContextEvent sce) {
log.debug("当前应用application对象被销毁");
}
// 当application创建时执行初始化方法
@Override
public void contextInitialized(ServletContextEvent sce) {
ServletContext application = sce.getServletContext();
String contextPath = application.getContextPath();
log.debug("当前应用上下文路径:{}", contextPath);
application.setAttribute(Const.PATH, contextPath);
}
}
注册到xml文件中
前端流程业务
登录页面
管理员登录后页面
会员登录后页面
流程图
登录页面--》DispatcherController--》登录页面--》DispatcherController--》service--》DispatcherController--》主页面
展示错误信息
抽取
菜单查询并组装
查询
package com.atguigu.atcrowdfunding.service.impl;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.atguigu.atcrowdfunding.bean.TMenu;
import com.atguigu.atcrowdfunding.mapper.TMenuMapper;
import com.atguigu.atcrowdfunding.service.TMenuService;
@Service
public class TMenuServiceImpl implements TMenuService {
Logger log = LoggerFactory.getLogger(TMenuServiceImpl.class);
@Autowired
TMenuMapper menuMapper ;
@Override
public List<TMenu> listMenuAll() {
List<TMenu> menuList = new ArrayList<TMenu>(); //只存放父菜单,但是将children属性赋值
Map<Integer,TMenu> cache = new HashMap<Integer,TMenu>();
List<TMenu> allList = menuMapper.selectByExample(null);//查询所有菜单
for (TMenu tMenu : allList) {//在所有菜单中的对应号为0的作为一级菜单
if(tMenu.getPid() == 0) {
menuList.add(tMenu);
cache.put(tMenu.getId(), tMenu);
}
}
for (TMenu tMenu : allList) {//在所有菜单中的对应号不为0的作为二级菜单
if(tMenu.getPid() != 0) {
Integer pid = tMenu.getPid();//得到子菜单的id
TMenu parent = cache.get(pid);//查询对应的父菜单
parent.getChildren().add(tMenu); //根据子菜单pid查找父菜单id,然后根据父菜单children属性进行父子关系组合。
}
}
log.debug("菜单={}",menuList);
return menuList;
}
@Override
public List<TMenu> listMenuAllTree() {
return menuMapper.selectByExample(null);
}
@Override
public void saveTMenu(TMenu menu) {
menuMapper.insertSelective(menu);
}
@Override
public TMenu getMenuById(Integer id) {
return menuMapper.selectByPrimaryKey(id);
}
@Override
public void updateTMenu(TMenu menu) {
menuMapper.updateByPrimaryKeySelective(menu);
}
@Override
public void deleteTMenu(Integer id) {
menuMapper.deleteByPrimaryKey(id);
}
}
组装
<%@ page language="java" contentType="text/html;
charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="c"
uri="http://java.sun.com/jsp/jstl/core" %>
<div class="col-sm-3 col-md-2 sidebar">
<div class="tree">
<ul style="padding-left:0px;"
class="list-group">
<c:forEach items="${menuList}"
var="parent">
<c:if test="${empty parent.children}">
<li class="list-group-item
tree-closed" >
<a
href="${PATH}/${parent.url}"><i class="${parent.icon
}"></i> ${parent.name }</a>
</li>
</c:if>
<c:if test="${not empty
parent.children}">
<li class="list-group-item
tree-closed">
<span><i
class="${parent.icon }"></i> ${parent.name }<span
class="badge"
style="float:right">${parent.children.size()}</span></span>
<ul
style="margin-top:10px;display:none;">
<c:forEach
items="${parent.children}" var="child">
<li
style="height:30px;">
<a
href="${PATH}/${child.url}"><i class="${child.icon
}"></i> ${child.name }</a>
</li>
</c:forEach>
</ul>
</li>
</c:if>
</c:forEach>
</ul>
</div>
</div>