redis模板类(RedisClientTemplate)方法有点多,字数到了。新建一章
其实最重要的都贴出来完了。
10.ParameterMap(一个参数拦截器,我觉得挺好用的)
package com.lrs.util;
import java.io.UnsupportedEncodingException;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import javax.servlet.http.HttpServletRequest;
/**
* 拦截所有参数
* @author Administrator
*
*/
@SuppressWarnings("rawtypes")
public class ParameterMap extends HashMap implements Map{
/**
*
*/
private static final long serialVersionUID = 1L;
Map map = null;
HttpServletRequest request;
public ParameterMap(){
map=new HashMap();
}
@SuppressWarnings("unchecked")
public ParameterMap(HttpServletRequest request){
this.request=request;
try {
request.setCharacterEncoding("utf-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
Map Proerties = request.getParameterMap();
Map returnMap = new HashMap();
Iterator entries = Proerties.entrySet().iterator();
Map.Entry entry;
while(entries.hasNext()){
String name="";
String value="";
entry = (Entry) entries.next();
name = (String) entry.getKey();
Object valueObject = entry.getValue();
if(valueObject == null){
value="";
}else if(valueObject instanceof String[]){
String[] values = (String[]) valueObject;
for(int i=0;i<values.length;i++){
value = values[i]+","+value;
}
value=value.substring(0,value.length()-1);
}else{
value=valueObject.toString();
}
returnMap.put(name, value);
}
returnMap.put("SessionID", request.getSession().getId());
map=returnMap;
}
@Override
public Object get(Object key) {
Object obj = null;
if(map.get(key) instanceof Object[]){
Object[] arr = (Object[]) map.get(key);
obj = request==null?arr:(request.getParameter((String) key) == null?arr:arr[0]);
}else{
obj = map.get(key);
}
return obj;
}
public String getString(Object key){
String str = "";
if(get(key) != null){
str = String.valueOf(get(key));
}
return str;
}
@SuppressWarnings("unchecked")
@Override
public Object put(Object key, Object value) {
if(value == null){
return map.put(key, "");
}
return map.put(key, value);
}
@SuppressWarnings("unchecked")
public Object putObject(Object key,Object value){
return map.put(key, value);
}
@Override
public Object remove(Object key) {
return map.remove(key);
}
@Override
public void clear() {
map.clear();
}
@Override
public boolean containsKey(Object key) {
return map.containsKey(key);
}
@Override
public boolean containsValue(Object value) {
return map.containsValue(value);
}
@Override
public Set entrySet() {
return map.entrySet();
}
@Override
public boolean isEmpty() {
return map.isEmpty();
}
@Override
public Set keySet() {
return map.keySet();
}
@SuppressWarnings("unchecked")
@Override
public void putAll(Map m) {
map.putAll(m);
}
@Override
public int size() {
return map.size();
}
@Override
public Collection values() {
return map.values();
}
}
11.BaseController(所有控制器的父类,好的地方是过滤参数的方法)
package com.lrs.controller;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.springframework.web.context.ContextLoader;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.servlet.ModelAndView;
import com.tyro.util.MyLogger;
import com.tyro.util.ParameterMap;
public class BaseController {
protected MyLogger logger = MyLogger.getLogger(this.getClass());
/**
* springMVC 获取requset
*
* @return
*/
public HttpServletRequest getRequest() {
HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes())
.getRequest();
return request;
}
/**
* 获取session
* @return
*/
public HttpSession getSession(){
HttpSession session = this.getRequest().getSession();
return session;
}
/**
* 获取ServletContext
* @return
*/
public ServletContext getServletContent() {
// ServletContext application= request.getServletContext();
WebApplicationContext webApplicationContext = ContextLoader.getCurrentWebApplicationContext();
ServletContext servletContext = webApplicationContext.getServletContext();
return servletContext;
}
/**
* 获取ModelAndView
* @return
*/
public ModelAndView getModelAndView(){
return new ModelAndView();
}
/**
* 过滤参数
*
* @return
*/
public ParameterMap getParameterMap() {
return new ParameterMap(this.getRequest());
}
/**
* 获取ip
*
* @return
*/
public String getRemortIP() {
HttpServletRequest request = this.getRequest();
String ip = "";
if (request.getHeader("x-forwarded-for") == null) {
ip = request.getRemoteAddr();
} else {
ip = request.getHeader("x-forwarded-for");
}
return ip;
}
/**
* 获取ip
* @param request
* @return
*/
public String getIpAddr() {
HttpServletRequest request = this.getRequest();
String ip = request.getHeader("x-forwarded-for");
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getHeader("Proxy-Client-IP");
}
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getHeader("WL-Proxy-Client-IP");
}
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getRemoteAddr();
}
return ip;
}
public static void printLogger(MyLogger logger,String message){
logger.info(message);
}
}
11.测试类
package com.tyro.controller;
import javax.annotation.Resource;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.tyro.redis.RedisClientTemplate;
import com.tyro.util.ParameterMap;
@RequestMapping(value="/redis")
@Controller
public class RedisTestController extends BaseController{
@Resource(name="redisClientTemplate")
RedisClientTemplate redisClient;
/**
*
* @return
*/
@RequestMapping("/test")
@ResponseBody
public Object test(){
ParameterMap pm = this.getParameterMap();
if(pm != null && pm.size() > 0)
for(Object key: pm.keySet()){
//传什么上来就缓存什么
redisClient.set(key.toString(),pm.getString(key));
}
//打印
for(Object key: pm.keySet()){
//传什么上来就缓存什么
System.out.println("获取缓存key"+key.toString()+"="+redisClient.get(key.toString()));
}
return pm;
}
}
请求地址
控制台
redis客户端看看
OK 测试完毕。最后贴上工程目录结构