导航栏,博主信息,分类信息,天气等不变的信息单独加载,
需要在web.xml中设置监听器
<!-- Spring监听器 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<listener>
<listener-class>com.java1234.service.impl.InitComponent</listener-class>
</listener>
在Impl包中加入InitComponent类
主要接入了ServletContextListener,ApplicationContextAware这两个类
设置全局application将数据放入其中
package com.java1234.service.impl;
import java.util.List;
import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;
import com.java1234.entity.Blog;
import com.java1234.entity.BlogType;
import com.java1234.entity.Blogger;
import com.java1234.entity.Link;
import com.java1234.service.BlogService;
import com.java1234.service.BlogTypeService;
import com.java1234.service.BloggerService;
import com.java1234.service.LinkService;
@Component
public class InitComponent implements ServletContextListener,ApplicationContextAware{
private static ApplicationContext applicationContext;
public void contextInitialized(ServletContextEvent sce) {
ServletContext application=sce.getServletContext();
BloggerService bloggerService=(BloggerService) applicationContext.getBean("bloggerService");
Blogger blogger=bloggerService.find(); // 获取博主信息
blogger.setPassword(null);
application.setAttribute("blogger", blogger);
LinkService linkService=(LinkService) applicationContext.getBean("linkService");
List<Link> linkList=linkService.list(null); // 查询所有的友情链接信息
application.setAttribute("linkList", linkList);
BlogTypeService blogTypeService=(BlogTypeService) applicationContext.getBean("blogTypeService");
List<BlogType> blogTypeCountList=blogTypeService.countList();
application.setAttribute("blogTypeCountList", blogTypeCountList);
BlogService blogService =(BlogService) applicationContext.getBean("blogService");
List<Blog> blogCountList=blogService.countList();
application.setAttribute("blogCountList",blogCountList);
}
public void contextDestroyed(ServletContextEvent sce) {
// TODO Auto-generated method stub
}
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
this.applicationContext=applicationContext;
}
}
页面的显示index.jsp
<div class="data_list">
<div class="data_list_title">
<img alt="友情链接" src="${pageContext.request.contextPath }/static/images/link_icon.png">
友情链接
</div>
<div class="datas">
<ul>
<c:forEach var="link" items="${linkList }">(动态加载,从数据库中取值载入)
<li><span><a href="${link.linkUrl }" target="_blank">${link.linkName }</a></span></li>
</c:forEach>
</ul>
</div>
</div>