java Action 请求封装(一)

     该框架是用Spring MVC 3.0实现,首先定义一个Action类,然后把init()方法给封装,然后在把?以后的所有数据全部封装到Map集合,实现方法如下:

Java代码   收藏代码
  1. /* 
  2.  
  3.  *Action 请求参数封装,不区分get和post请求 
  4.  
  5.  *zhouguohui 
  6.  
  7.  */  
  8.   
  9. public class Action {  
  10.   
  11.     private HttpServletRequest request;      
  12.     private HttpServletResponse response;  
  13.     private ActionValues values; //参数封装到Map集合里面  
  14.     private MultipartFile[] files;  //多个文件上传的路径地址  
  15.      
  16.     /** 
  17.      * 初始化Action 
  18.      * @param request 
  19.      * @param response 
  20.      * @param model 
  21.      * @param fils 
  22.      */  
  23.     @ModelAttribute  
  24.     public void init(HttpServletRequest request,HttpServletResponse response,@RequestParam(required=false) MultipartFile[] fils,Model model){  
  25.         this.request=request;  
  26.         this.response=response;  
  27.         this.values=new ActionValues(request);  
  28.          
  29.         if(null!=fils&&fils.length>0){  
  30.             this.files=fils;  
  31.         }  
  32.          
  33.         model.addAttribute("values", values);  
  34.         response.setContentType("text/html;charset=UTF-8");  
  35.          
  36.     }  
  37.      
  38.     /** 
  39.      * 输出字符串 
  40.      * @param msg 
  41.      */  
  42.     protected void print(Object msg){  
  43.         PrintWriter out=null;  
  44.         try{  
  45.             out=response.getWriter();  
  46.             out.print(msg);  
  47.         }catch(Exception e){  
  48.             e.printStackTrace();  
  49.         }finally{  
  50.             out.close();  
  51.         }  
  52.     }  
  53.      
  54.     /** 
  55.      * 以Json格式输出参数 
  56.      * @param obj 
  57.      */  
  58.     protected void printJSON(Object obj){  
  59.         PrintWriter out=null;  
  60.         try{  
  61.             out=response.getWriter();  
  62.             out.print(JsonUtils.toJsonStr(obj));  
  63.         }catch(Exception e){  
  64.             e.printStackTrace();  
  65.         }finally{  
  66.             out.close();  
  67.         }  
  68.     }  
  69.      
  70.     /** 
  71.      * 以keyName为键名,以obj为键值,返回Json对象 
  72.      * @param keyName 
  73.      * @param obj 
  74.      */  
  75.     protected void printJSON(String keyName,Object obj){  
  76.         PrintWriter out=null;  
  77.         try{  
  78.             out=response.getWriter();  
  79.             out.print(JsonUtils.toJsonStr(keyName, obj));  
  80.         }catch(Exception e){  
  81.             e.printStackTrace();  
  82.         }finally{  
  83.             out.close();  
  84.         }  
  85.     }  
  86.      
  87.     public HttpServletRequest getRequest() {  
  88.         return request;  
  89.     }  
  90.      
  91.     public HttpServletResponse getResponse() {  
  92.         return response;  
  93.     }  
  94.      
  95.     /** 
  96.      * 得到前台参数对象 
  97.      * @param page 
  98.      * @return 
  99.      */  
  100.     public ActionValues getValues() {  
  101.         return values;  
  102.     }  
  103.      
  104.     /** 
  105.      * 得到前台参数对象 
  106.      * @param page 
  107.      * @return 
  108.      */  
  109.     public ActionValues getValues(boolean page) {  
  110.         if(!page){  
  111.             values.offPage();  
  112.         }  
  113.         return values;  
  114.     }  
  115.      
  116.     /** 
  117.      * 得到上传文件 
  118.      * @return 
  119.      */  
  120.     public MultipartFile[] getFiles() {  
  121.         return files;  
  122.     }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值