放假了,首先祝大家端午节快乐,今天下雨,没事干研究了下ehcache,从ehcache官网查看了一番后,看了看网上别人的blog,没有ehcache的详细用法,所以自己小试了一下,第一次使用ehcache可能有些地方不正确,希望大家帮我纠正,共同进步。
我采用struts2+ehcache。代码如下:
1、index.jsp
view plaincopy to clipboardprint?
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css" mce_href="styles.css">
-->
</head>
<body>
<a href="colorAction.action" mce_href="colorAction.action">查看所有颜色</a>
</body>
</html>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css" mce_href="styles.css">
-->
</head>
<body>
<a href="colorAction.action" mce_href="colorAction.action">查看所有颜色</a>
</body>
</html>
2、ColorAction.java
view plaincopy to clipboardprint?
package com.ehcache.servlet;
import java.awt.Color;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import net.sf.ehcache.Cache;
import net.sf.ehcache.CacheManager;
import net.sf.ehcache.Element;
import com.ehcache.database.ColorDatabase;
import com.ehcache.util.EhcacheUtil;
import com.opensymphony.xwork2.ActionSupport;
@SuppressWarnings("serial")
public class ColorAction extends ActionSupport {
private List<String> list = null;
@SuppressWarnings("unchecked")
public String allColor() throws Exception {
//获取CacheManager
CacheManager cacheManager = EhcacheUtil.getCacheManager();
list = new ArrayList<String>();
//用配置文件中配置的colorcache创建cache缓存
Cache cache = cacheManager.getCache("colorcache");
//查看cache中是否存在allColor的缓存
Element element = cache.get("allColor");
//如果不存在,从数据库中查询
if (element == null) {
Map<String, Color> maps = ColorDatabase.getAllColor();
java.util.Iterator<String> iter = maps.keySet().iterator();
while (iter.hasNext()) {
Color color = maps.get(iter.next());
list.add(color.toString());
}
cache.put(new Element("allColor", list));
} else {//如果存在,从缓存中加载
list = (List<String>) element.getValue();
}
return "allColor";
}
public List<String> getList() {
return list;
}
public void setList(List<String> list) {
this.list = list;
}
}
package com.ehcache.servlet;
import java.awt.Color;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import net.sf.ehcache.Cache;
import net.sf.ehcache.CacheManager;
import net.sf.ehcache.Element;
import com.ehcache.database.ColorDatabase;
import com.ehcache.util.EhcacheUtil;
import com.opensymphony.xwork2.ActionSupport;
@SuppressWarnings("serial")
public class ColorAction extends ActionSupport {
private List<String> list = null;
@SuppressWarnings("unchecked")
public String allColor() throws Exception {
//获取CacheManager
CacheManager cacheManager = EhcacheUtil.getCacheManager();
list = new ArrayList<String>();
//用配置文件中配置的colorcache创建cache缓存
Cache cache = cacheManager.getCache("colorcache");
//查看cache中是否存在allColor的缓存
Element element = cache.get("allColor");
//如果不存在,从数据库中查询
if (element == null) {
Map<String, Color> maps = ColorDatabase.getAllColor();
java.util.Iterator<String> iter = maps.keySet().iterator();
while (iter.hasNext()) {
Color color = maps.get(iter.next());
list.add(color.toString());
}
cache.put(new Element("allColor", list));
} else {//如果存在,从缓存中加载
list = (List<String>) element.getValue();
}
return "allColor";
}
public List<String> getList() {
return list;
}
public void setList(List<String> list) {
this.list = list;
}
}
3、ColorDatabase.java 代替数据库。
view plaincopy to clipboardprint?
package com.ehcache.database;
import java.awt.Color;
import java.util.HashMap;
import java.util.Map;
/**
* 在ehcache-2.1.0中的samples\colorcache 的例子中修改了一下,这个代替了数据库。
*/
public class ColorDatabase {
private static Map<String,Color> colorMap = new HashMap<String,Color>();
static{
colorMap.put("red", Color.red);
colorMap.put("blue", Color.blue);
colorMap.put("green", Color.green);
colorMap.put("white", Color.white);
colorMap.put("black", Color.black);
colorMap.put("lightGray", Color.lightGray);
colorMap.put("gray", Color.gray);
colorMap.put("darkGray", Color.darkGray);
colorMap.put("pink", Color.pink);
colorMap.put("orange", Color.orange);
colorMap.put("yellow", Color.yellow);
colorMap.put("magenta", Color.magenta);
colorMap.put("cyan", Color.cyan);
}
public static Map<String,Color> getAllColor(){
return colorMap;
}
}
package com.ehcache.database;
import java.awt.Color;
import java.util.HashMap;
import java.util.Map;
/**
* 在ehcache-2.1.0中的samples\colorcache 的例子中修改了一下,这个代替了数据库。
*/
public class ColorDatabase {
private static Map<String,Color> colorMap = new HashMap<String,Color>();
static{
colorMap.put("red", Color.red);
colorMap.put("blue", Color.blue);
colorMap.put("green", Color.green);
colorMap.put("white", Color.white);
colorMap.put("black", Color.black);
colorMap.put("lightGray", Color.lightGray);
colorMap.put("gray", Color.gray);
colorMap.put("darkGray", Color.darkGray);
colorMap.put("pink", Color.pink);
colorMap.put("orange", Color.orange);
colorMap.put("yellow", Color.yellow);
colorMap.put("magenta", Color.magenta);
colorMap.put("cyan", Color.cyan);
}
public static Map<String,Color> getAllColor(){
return colorMap;
}
}
4、allColor.jsp
view plaincopy to clipboardprint?
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@taglib prefix="s" uri="/struts-tags"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'allColor.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css" mce_href="styles.css">
-->
</head>
<body>
<s:iterator value="list">
<s:property/><br>
</s:iterator>
</body>
</html>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@taglib prefix="s" uri="/struts-tags"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'allColor.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css" mce_href="styles.css">
-->
</head>
<body>
<s:iterator value="list">
<s:property/><br>
</s:iterator>
</body>
</html>
5、ehcache.xml
view plaincopy to clipboardprint?
<ehcache>
<diskStore path="java.io.tmpdir" />
<defaultCache maxElementsInMemory="10000" eternal="false"
timeToIdleSeconds="120" timeToLiveSeconds="120" overflowToDisk="true" />
<cache name="colorcache" maxElementsInMemory="10000" eternal="false"
timeToIdleSeconds="300" timeToLiveSeconds="600" overflowToDisk="true" />
</ehcache>
<ehcache>
<diskStore path="java.io.tmpdir" />
<defaultCache maxElementsInMemory="10000" eternal="false"
timeToIdleSeconds="120" timeToLiveSeconds="120" overflowToDisk="true" />
<cache name="colorcache" maxElementsInMemory="10000" eternal="false"
timeToIdleSeconds="300" timeToLiveSeconds="600" overflowToDisk="true" />
</ehcache>
6、struts.xml
view plaincopy to clipboardprint?
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<package name="struts" extends="struts-default">
<action name="colorAction" class="com.ehcache.servlet.ColorAction" method="allColor">
<result name="allColor">/allColor.jsp</result>
</action>
</package>
</struts>
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<package name="struts" extends="struts-default">
<action name="colorAction" class="com.ehcache.servlet.ColorAction" method="allColor">
<result name="allColor">/allColor.jsp</result>
</action>
</package>
</struts>
7、EhcacheUtil.java
view plaincopy to clipboardprint?
package com.ehcache.util;
import net.sf.ehcache.CacheManager;
public class EhcacheUtil {
private EhcacheUtil(){}
private static CacheManager cacheManager = null;
static{
cacheManager = CacheManager.create();
}
public static CacheManager getCacheManager(){
return cacheManager;
}
}
分享到:
相关推荐
2.Action里通过struts2-spring-plugin.jar插件自动根据名字注入。 3.Ajax无刷新异步调用Struts2,返回Json数据,以用户注册为例。 4.在服务端分页查询功能,优点:实时性:跳页才查询。数据量小:只加载当前页的记录...
NULL 博文链接:https://dreamoftch.iteye.com/blog/2208828
同时使用了Struts2、Spring4、Hibernate4、log4j、slf4j、junit4、ehcache等库或框架,搭建一个最基本的项目原型。 三、 三大框架最新版本下载:截止2014-10-01 Struts2.3.6:发布于2014-05-03,目前的最新版本。...
spring3+hibernate4+struts2+dbcp+mysql+json+ehcache+dom4j 合集包
struts2+spring+hibernate集成例子,包含所有jar包,ehcache二级缓存,mysql数据,需要自己创建
struts2+hibernate+ehcache二级缓存的 步骤和需要的代码及jar包
整合S2SH+Freemarker+oscache,后台用Spring管理各个bean,Hibernate做数据库持久化,viewer用Freemarker。整合中对Struts2,Hibernate,Spring都采用Annotation进行注解类。
ssh,struts+hibernate+spring+ehcache集成
1、 同时使用了Struts2、Spring4、Hibernate4、Quartz、JUnit4、Log4j、SLF4J、Ehcache、Gson等库或框架,搭建一个最基本的项目原型。 2、 Spring配置提供了多种可选整合方案:详见applicationContext.xml的配置 (1...
spring版本有2.0,2.5的,hibernate版本较多些至3.2,首先选版本就选择最优的,struts2没的选只有2.1.6版的,所以先导入struts2支持,然后是spring选的是2.0,问题就出在struts2中spring的插件上了,没有从MyEclipse...
这是一个实际使用中的项目,可访问 http://cdbke.cuit.edu.cn 可查看效果及功能,后台需要登录后才能使用(出于安全性考虑这里就不给大家介绍了,另外还有一个struts1版本的,如有需要可以与我联系)。 此项目整合...
这是一个实际使用中的项目,可访问 http://cdbke.cuit.edu.cn 可查看效果及功能,后台需要登录后才能使用(出于安全性考虑这里就不给大家介绍了,另外还有一个struts1版本的,如有需要可以与我联系)。 此项目整合...
这是一个实际使用中的项目,可访问 http://cdbke.cuit.edu.cn 可查看效果及功能,后台需要登录后才能使用(出于安全性考虑这里就不给大家介绍了,另外还有一个struts1版本的,如有需要可以与我联系)。 此项目整合...
J2EE基础平台,包括Spring管理Bean,Struts管理Action,数据库链接池Proxool,内存数据库,页面缓存等与一体的基础开发环境
EHCache缓存的说明文档是到处找来的内容,都有参考链接指向原地址。有三个测试项目也在里面:一个整合了Struts2或Hibernate,一个整合了MyBatis3,这两个是我做的;另一个ehcachetest是下载了別人的。
ehcache缓存教程 ehcache j2ee java spring struts
这是自己整合的Spring 3.0+Struts2+Mybatis 3 + p6spy +ehcache的平台框架,内含一点示例代码,目前ehcache没有使用。直接编译后发布就能用 测试环境基于JDK1.6+Tomcat 6.0. 大家拿到后请根据实际情况修改 ...
自己整合的Spring+Hibernate+Ehcache 整合的项目 需要struts的自己添加。 里面带有注释,而且带Jar.写的很清楚,良心分。
一、 项目名称:ssh 项目原型:Struts2.3.20 + Spring4.1.6 + Hibernate4.3.9 ...同时使用了Struts2、Spring4、Hibernate4、log4j、slf4j、junit4、ehcache等库或框架,搭建一个最基本的项目原型。
本例使用struts2.3.16 spring4.07 hibernate4.37 前端使用extjs4.2.1 同时集成ehcache