cms-帖子静态化

实现帖子静态化和实现友情链接静态化一致,

1.首先建立帖子类别的实体类:

  

package com.open1111.entity;

/**
* 帖子类别实体
* @author user
*
*/
public class ArcType {

private Integer id; // 编号
private String typeName; // 类别名称
private Integer sortNo; // 排列序号
private String keywords; // 关键字 页面seo用到
private String description; // 类别描述 页面seo用到

public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getTypeName() {
return typeName;
}
public void setTypeName(String typeName) {
this.typeName = typeName;
}
public Integer getSortNo() {
return sortNo;
}
public void setSortNo(Integer sortNo) {
this.sortNo = sortNo;
}
public String getKeywords() {
return keywords;
}
public void setKeywords(String keywords) {
this.keywords = keywords;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
}

2.然后编写帖子对应的mapper

<?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.open1111.dao.ArcTypeDao">

<resultMap type="ArcType" id="ArcTypeResult">
<result property="id" column="id"/>
<result property="typeName" column="typeName"/>
<result property="sortNo" column="sortNo"/>
<result property="keywords" column="keywords"/>
<result property="description" column="description"/>
</resultMap>

<select id="list" parameterType="Map" resultMap="ArcTypeResult">
select * from t_arcType
order by sortNo asc
</select>

</mapper>

3.编写mapper对应的dao

package com.open1111.dao;

import java.util.List;
import java.util.Map;

import com.open1111.entity.ArcType;

/**
* 帖子类别Dao接口
* @author user
*
*/
public interface ArcTypeDao {

/**
* 根据条件分页查询帖子类别集合
* @param map
* @return
*/
public List<ArcType> list(Map<String,Object> map);
}

4.编写帖子类别的service

package com.open1111.service;

import java.util.List;
import java.util.Map;

import com.open1111.entity.ArcType;

/**
* 帖子类别Service接口
* @author user
*
*/
public interface ArcTypeService {

/**
* 根据条件分页查询帖子类别集合
* @param map
* @return
*/
public List<ArcType> list(Map<String,Object> map);
}

5.编写daoImpl

package com.open1111.service.impl;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.open1111.dao.ArcTypeDao;
import com.open1111.entity.ArcType;
import com.open1111.service.ArcTypeService;

/**
* 帖子类别Service实现类
* @author user
*
*/
@Service("arcTypeService")
public class ArcTypeServiceImpl implements ArcTypeService{

@Resource
private ArcTypeDao arcTypeDao;

public List<ArcType> list(Map<String, Object> map) {
return arcTypeDao.list(map);
}

}

6.把查询的数据放入application中:

package com.open1111.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.open1111.entity.ArcType;
import com.open1111.entity.Link;
import com.open1111.service.ArcTypeService;
import com.open1111.service.LinkService;

/**
* 初始化组件
* @author user
*
*/
@Component("initComponet")
public class InitComponet implements ApplicationContextAware,ServletContextListener{

private static ApplicationContext applicationContext;

public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
// TODO Auto-generated method stub
this.applicationContext=applicationContext;
System.out.println(this);
}

/**
* 刷新application缓存方法
* @param application
*/
public void refreshSystem(ServletContext application){
LinkService linkService=(LinkService) applicationContext.getBean("linkService");
List<Link> linkList=linkService.list(null);
application.setAttribute("linkList", linkList);

ArcTypeService arcTypeService=(ArcTypeService) applicationContext.getBean("arcTypeService");
List<ArcType> arcTypeList=arcTypeService.list(null);
application.setAttribute("arcTypeList", arcTypeList);
}

public void contextInitialized(ServletContextEvent sce) {
// TODO Auto-generated method stub
ServletContext application=sce.getServletContext();
refreshSystem(application);
}

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

}

}

7.在页面把类别显示出来

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>


<div class="menu">
<div class="w960 m960">
<ul>
<li class="first"><a href="/index.jsp" >首页</a></li>

<c:forEach var="arcType" items="${arcTypeList }">
<li><a href="${pageContext.request.contextPath}/arcType/${arcType.id }.html">${arcType.typeName }</a></li>
</c:forEach>
</ul>
</div>
</div>

 

posted @ 2017-03-08 21:48  小拽A  阅读(231)  评论(0编辑  收藏  举报