Spring+SpringMVC+mybatis+redis(三)

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

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;
    }
}

请求地址

Spring+SpringMVC+mybatis+redis(三)_第1张图片
2.png

控制台

Spring+SpringMVC+mybatis+redis(三)_第2张图片
1.png

redis客户端看看

Spring+SpringMVC+mybatis+redis(三)_第3张图片
3.png

OK 测试完毕。最后贴上工程目录结构

Spring+SpringMVC+mybatis+redis(三)_第4张图片
4.png

你可能感兴趣的:(Spring+SpringMVC+mybatis+redis(三))