jsp动态导航栏
站点页面的导航栏是从数据库中生成出来的,所以在界面上展示导航栏时,要从数据库中读取出来,但不能每次显示一个页面都从数据库中读。这样就非常浪费性能。应该考虑把导航栏放到一个缓存中。如: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存储中内存中。且一份,多个用户同一时候訪问时,就会出现等待的状况。
以上皆为本人总结所得。假设不妥之处,敬请提出,共同学习。。。