博客系统-静态化页面

思路分析:静态化的实现是把数据存入application中,由于初始化时候将查询到的数据放入application中,后来就直接在application中取数据了,不用在查询数据库,从而提高网站性能,以友情链接和博主信息为例

需要将数据在项目启动时候就放入application,创建一个监听器,需要使用applicationAwar接口以获得application,

ServletContext application=sce.getServletContext();

在执行过程中需要用到applicationContext以获得spring中的值所以需要serverletContext

然后把conponent注入进spring

最后在web.xml中配置监听

public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
this.applicationContext=applicationContext;
}

1.查询数据:

<?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.java1234.dao.BloggerDao">

<resultMap type="Blogger" id="BloggerResult">
<result property="id" column="id"/>
<result property="userName" column="userName"/>
<result property="password" column="password"/>
<result property="profile" column="profile"/>
<result property="nickName" column="nickName"/>
<result property="sign" column="sign"/>
<result property="imageName" column="imageName"/>
</resultMap>

<select id="getByUserName" parameterType="String" resultMap="BloggerResult">
select * from t_blogger where userName=#{userName}
</select>
</mapper>

2.查询接口:

package com.java1234.dao;

import com.java1234.entity.Blogger;

/**
* 博主Dao接口
* @author Administrator
*
*/
public interface BloggerDao {

/**
* 通过用户名查询用户
* @param userName
* @return
*/
public Blogger getByUserName(String userName);
}

3.service接口:

package com.java1234.service;

import com.java1234.entity.Blogger;

/**
* 博主Service接口
* @author Administrator
*
*/
public interface BloggerService {

/**
* 通过用户名查询用户
* @param userName
* @return
*/
public Blogger getByUserName(String userName);
}

4.service实现

package com.java1234.service.impl;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.java1234.dao.BloggerDao;
import com.java1234.entity.Blogger;
import com.java1234.service.BloggerService;

/**
* 博主Service实现类
* @author Administrator
*
*/
@Service("bloggerService")
public class BloggerServiceImpl implements BloggerService{

@Resource
private BloggerDao bloggerDao;

public Blogger getByUserName(String userName) {
return bloggerDao.getByUserName(userName);
}

}

5.创建component

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.Blogger;
import com.java1234.entity.Link;
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);
}

public void contextDestroyed(ServletContextEvent sce) {
// TODO Auto-generated method stub

}

public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
this.applicationContext=applicationContext;
}

}

6,在页面显示数据:

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link rel="stylesheet" href="${pageContext.request.contextPath}/static/bootstrap3/css/bootstrap.min.css">
<link rel="stylesheet" href="${pageContext.request.contextPath}/static/bootstrap3/css/bootstrap-theme.min.css">
<link rel="stylesheet" href="${pageContext.request.contextPath}/static/css/blog.css">
<script src="${pageContext.request.contextPath}/static/bootstrap3/js/jquery-1.11.2.min.js"></script>
<script src="${pageContext.request.contextPath}/static/bootstrap3/js/bootstrap.min.js"></script>
<title>博主主页</title>
<style type="text/css">
body{
padding-top: 10px;
padding-bottom: 40px;
}
</style>
</head>
<body>
<div class="container">
<div class="row">
<div class="col-md-4">
<img alt="java1234开源博客系统" src="${pageContext.request.contextPath}/static/images/logo.png">
</div>
<div class="col-md-8">
<iframe style="float: right;" width="420" scrolling="no" height="60" frameborder="0" allowtransparency="true" src="http://i.tianqi.com/index.php?c=code&id=12&icon=1&num=5"></iframe>
</div>
</div>

<div class="row">
<div class="col-md-12" style="padding-top: 10px">
<nav class="navbar navbar-default">
<div class="container-fluid">
<!-- Brand and toggle get grouped for better mobile display -->
<div class="navbar-header">
<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1" aria-expanded="false">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="#"><font color="black"><strong>首页</strong></font></a>
</div>

<!-- Collect the nav links, forms, and other content for toggling -->
<div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
<ul class="nav navbar-nav">
<li><a href="#"><font color="black"><strong>关于博主</strong></font></a></li>
<li><a href="#"><font color="black"><strong>本站源码下载</strong></font></a></li>
</ul>
<form class="navbar-form navbar-right" role="search">
<div class="form-group">
<input type="text" class="form-control" placeholder="请输入要查询的关键字...">
</div>
<button type="submit" class="btn btn-default">搜索</button>
</form>
</div><!-- /.navbar-collapse -->
</div><!-- /.container-fluid -->
</nav>
</div>
</div>

<div class="row">
<div class="col-md-9">
<div class="data_list">
<div class="data_list_title">
<img src="${pageContext.request.contextPath}/static/images/list_icon.png"/>
最新博客</div>

<div class="datas">
<ul>
<li style="margin-bottom: 30px">
<span class="date"><a href="${pageContext.request.contextPath}/blog/articles/52.html">2016年02月15日</a></span>
<span class="title"><a href="${pageContext.request.contextPath}/blog/articles/52.html">使用CXF开发WebService客户端</a></span>
<span class="summary">摘要: 前面一讲开发了webservice服务器端接口,今天的话,我们来开发webservice客户端,让大家来体验下过程;首先建一个Maven项目,项目名字,WS_Client;然后我们要用CXF给我们提供的工具wsdl2java 来根据请求的url生成客户端代码;wsdl2java工具在CXF开发包里;开发下载地...</span>
<span class="img">

<a href="/blog/articles/52.html"><img src="${pageContext.request.contextPath}/static/userImages/a.jpg" alt="1455539511890048174.jpg"></a>
&nbsp;&nbsp;

<a href="/blog/articles/52.html"><img alt="QQ鎴浘20160215203213.jpg" src="${pageContext.request.contextPath}/static/userImages/b.jpg" title="1455539700734093102.jpg" width="667" height="264" style="width: 667px; height: 264px;"></a>
&nbsp;&nbsp;

<a href="/blog/articles/52.html"><img alt="QQ鎴浘20160215203317.jpg" src="${pageContext.request.contextPath}/static/userImages/c.jpg" title="1455539761187019902.jpg"></a>
&nbsp;&nbsp;

</span>
<span class="info">发表于 2016-02-15 21:06 阅读(71) 评论(5) </span>

</li>
<hr style="height:5px;border:none;border-top:1px dashed gray;padding-bottom: 10px;" />

<li style="margin-bottom: 30px">
<span class="date"><a href="${pageContext.request.contextPath}/blog/articles/52.html">2016年02月15日</a></span>
<span class="title"><a href="${pageContext.request.contextPath}/blog/articles/52.html">使用CXF开发WebService客户端</a></span>
<span class="summary">摘要: 前面一讲开发了webservice服务器端接口,今天的话,我们来开发webservice客户端,让大家来体验下过程;首先建一个Maven项目,项目名字,WS_Client;然后我们要用CXF给我们提供的工具wsdl2java 来根据请求的url生成客户端代码;wsdl2java工具在CXF开发包里;开发下载地...</span>
<span class="img">

<a href="/blog/articles/52.html"><img src="${pageContext.request.contextPath}/static/userImages/a.jpg" alt="1455539511890048174.jpg"></a>
&nbsp;&nbsp;

<a href="/blog/articles/52.html"><img alt="QQ鎴浘20160215203213.jpg" src="${pageContext.request.contextPath}/static/userImages/b.jpg" title="1455539700734093102.jpg" width="667" height="264" style="width: 667px; height: 264px;"></a>
&nbsp;&nbsp;

<a href="/blog/articles/52.html"><img alt="QQ鎴浘20160215203317.jpg" src="${pageContext.request.contextPath}/static/userImages/c.jpg" title="1455539761187019902.jpg"></a>
&nbsp;&nbsp;

</span>
<span class="info">发表于 2016-02-15 21:06 阅读(71) 评论(5) </span>

</li>
<hr style="height:5px;border:none;border-top:1px dashed gray;padding-bottom: 10px;" />


<li style="margin-bottom: 30px">
<span class="date"><a href="${pageContext.request.contextPath}/blog/articles/52.html">2016年02月15日</a></span>
<span class="title"><a href="${pageContext.request.contextPath}/blog/articles/52.html">使用CXF开发WebService客户端</a></span>
<span class="summary">摘要: 前面一讲开发了webservice服务器端接口,今天的话,我们来开发webservice客户端,让大家来体验下过程;首先建一个Maven项目,项目名字,WS_Client;然后我们要用CXF给我们提供的工具wsdl2java 来根据请求的url生成客户端代码;wsdl2java工具在CXF开发包里;开发下载地...</span>
<span class="img">

<a href="/blog/articles/52.html"><img src="${pageContext.request.contextPath}/static/userImages/a.jpg" alt="1455539511890048174.jpg"></a>
&nbsp;&nbsp;

<a href="/blog/articles/52.html"><img alt="QQ鎴浘20160215203213.jpg" src="${pageContext.request.contextPath}/static/userImages/b.jpg" title="1455539700734093102.jpg" width="667" height="264" style="width: 667px; height: 264px;"></a>
&nbsp;&nbsp;

<a href="/blog/articles/52.html"><img alt="QQ鎴浘20160215203317.jpg" src="${pageContext.request.contextPath}/static/userImages/c.jpg" title="1455539761187019902.jpg"></a>
&nbsp;&nbsp;

</span>
<span class="info">发表于 2016-02-15 21:06 阅读(71) 评论(5) </span>

</li>
<hr style="height:5px;border:none;border-top:1px dashed gray;padding-bottom: 10px;" />

</ul>
</div>
</div>
</div>
<div class="col-md-3">
<div class="data_list">
<div class="data_list_title">
<img src="${pageContext.request.contextPath}/static/images/user_icon.png"/>
博主信息
</div>
<div class="user_image">
<img src="${pageContext.request.contextPath}/static/userImages/${blogger.imageName}"/>
</div>
<div class="nickName">${blogger.nickName}</div>
<div class="userSign">(${blogger.sign})</div>
</div>

<div class="data_list">
<div class="data_list_title">
<img src="${pageContext.request.contextPath}/static/images/byType_icon.png"/>
按日志类别
</div>
<div class="datas">
<ul>

<li><span><a href="/index.html?typeId=1">Java核心基础(1)</a></span></li>

<li><span><a href="/index.html?typeId=2">Mysql(1)</a></span></li>

<li><span><a href="/index.html?typeId=3">Tomcat(1)</a></span></li>

<li><span><a href="/index.html?typeId=10">jsoup(1)</a></span></li>

<li><span><a href="/index.html?typeId=7">shiro(1)</a></span></li>

<li><span><a href="/index.html?typeId=9">webservice(2)</a></span></li>

<li><span><a href="/index.html?typeId=4">IT之路(3)</a></span></li>

<li><span><a href="/index.html?typeId=5">随心生活(2)</a></span></li>

</ul>
</div>
</div>



<div class="data_list">
<div class="data_list_title">
<img src="${pageContext.request.contextPath}/static/images/byDate_icon.png"/>
按日志日期
</div>
<div class="datas">
<ul>

<li><span><a href="/index.html?releaseDateStr=2016年02月">2016年02月(11)</a></span></li>

<li><span><a href="/index.html?releaseDateStr=2016年01月">2016年01月(1)</a></span></li>

</ul>
</div>
</div>

<div class="data_list">
<div class="data_list_title">
<img 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>

</div>
</div>

<div class="row">
<div class="col-md-12" >
<div align="center" style="padding-top: 120px" >
<span style="float: left;">Powered by <a href="http://blog.java1234.com" target="_blank">Java1234</a> V1.0</span>
Copyright © 2012-2016 Java知识分享网 版权所有
</div>
</div>
</div>


</div>
</body>
</html>

 

posted @ 2017-03-13 23:47  小拽A  阅读(635)  评论(0编辑  收藏  举报