网站页面的导航栏是从数据库中生成出来的,所以在界面上展示导航栏时,要从数据库中读取出来,但不能每次显示一个页面都从数据库中读,这样就很浪费性能,应该考虑把导航栏放到一个缓存中。如:session、application、memcache等。
对于访问量小的网站,也可以使用如下方法:
@Component public class NavigationUtil { public static List<BeautifulClassEntity> beautifulClassList; public static List<ActivityClassEntity> activityClassList; public static List<BeautifulClassEntity> getBeautifulClassList(){ if(beautifulClassList == null){ beautifulClassList = beautifulClassService.getAllBeautifulClass(); } return beautifulClassList; } public static List<ActivityClassEntity> getActivityClassList(){ if(activityClassList == null){ activityClassList = activityService.getActivityList(); } return activityClassList; } private static BeautifulClassService beautifulClassService; private static ActivityService activityService; @Autowired public void setBeautifulClassService(BeautifulClassService beautifulClassService) { NavigationUtil.beautifulClassService = beautifulClassService; } @Autowired public void setActivityService(ActivityService activityService) { NavigationUtil.activityService = activityService; } public String getTest(){ return "aa"; } }
<% List<BeautifulClassEntity> beautifulList = NavigationUtil.getBeautifulClassList(); for(BeautifulClassEntity beautiful : beautifulList){ %> <a href="/beautiful/cloth.do?beautifulClassId=<%=beautiful.getId() %>" class="main_title_nav_cur"> <%=beautiful.getBeautifulName() %> </a> <% } %>
注意:该种方式仅适用于小型且访问量不大的网站,因为static存储中内存中,且一份,多个用户同时访问时,就会出现等待的状况。
以上皆为本人总结所得,如果不妥之处,敬请提出,共同学习。。。
时间: 2024-09-11 16:52:55