package com.huawei.base;

import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
* @author Administrator
*
*/
public abstract class BaseController extends HttpServlet{

/**
*
*/
private static final long serialVersionUID = 4874135853046529162L;

protected HttpServletRequest request;

protected HttpServletResponse response;
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
this.doPost(req, resp);
}

@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String _method = req.getParameter("_method");
_method = (_method == null)?"":_method;
/**
* 做一个反射的中转站
*
* 根据传入的_method 动态的去调用 所对应的方法
*
*
* eg:_method :delete
*/
//得到 目标class
Class<?> clazz = this.getClass();
this.request = req;
this.response = resp;
try {
//得到 指定的方法
Method method = clazz.getDeclaredMethod(_method,new Class[]{HttpServletRequest.class,HttpServletResponse.class});
//执行对应的方法
boolean access = method.isAccessible();
method.setAccessible(true);
method.invoke(this, req,resp);
method.setAccessible(access);
} catch (SecurityException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
this.findAll(req, resp);
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
}

protected void findAll(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
String _method = this.getParameter("_method");
if(_method == null || _method.trim()==""){
response.sendError(HttpServletResponse.SC_METHOD_NOT_ALLOWED, "缺少方法名!");
}else{
response.sendError(HttpServletResponse.SC_METHOD_NOT_ALLOWED, "该"+_method+"没有被实现!");
}
}

protected String getParameter(String name){
return this.request.getParameter(name);
}

protected String[] getParameterValues(String name) {
return this.request.getParameterValues(name);
}

protected PrintWriter getWriter() {
try {
return this.response.getWriter();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}

protected OutputStream getOutputStream() {
try {
return this.response.getOutputStream();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}

protected void write(String msg){
this.getWriter().write(msg);
}

}

封装baseControl的更多相关文章

  1. BaseControl按钮合集

    BaseControl按钮合集 效果 源码 https://github.com/YouXianMing/Animations // // POPBaseControl.h // Animations ...

  2. 用UIControl封装Button

    用UIControl封装Button 效果 说明 UIControl在处理超出触摸范围的触摸事件时有bug 源码 基础类 // // BaseControl.h // BaseControl // / ...

  3. 玩转控件:封装Dev的LabelControl和TextEdit

    俗话说的好:"工欲善其事必先利其器",作为软件攻城狮也是同样道理,攻城狮开发的软件目的是简化客户的操作,让客户动动手指就可以完成很多事情,减少人力成本.这也是系统/软件存在的目的. ...

  4. [C#] 简单的 Helper 封装 -- RegularExpressionHelper

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...

  5. iOS开发之App间账号共享与SDK封装

    上篇博客<iOS逆向工程之KeyChain与Snoop-it>中已经提到了,App间的数据共享可以使用KeyChian来实现.本篇博客就实战一下呢.开门见山,本篇博客会封装一个登录用的SD ...

  6. Ajax实现原理,代码封装

    都知道实现页面的异步操作需要使用Ajax,那么Ajax到是怎么实现异步操作的呢? 首先需要认识一个对象 --> XMLHttpRequest 对象 --> Ajax的核心.它有许多的属性和 ...

  7. 用C语言封装OC对象(耐心阅读,非常重要)

    用C语言封装OC对象(耐心阅读,非常重要) 本文的主要内容来自这里 前言 做iOS开发的朋友,对OC肯定非常了解,那么大家有没有想过OC中NSInteger,NSObject,NSString这些对象 ...

  8. 【知识必备】RxJava+Retrofit二次封装最佳结合体验,打造懒人封装框架~

    一.写在前面 相信各位看官对retrofit和rxjava已经耳熟能详了,最近一直在学习retrofit+rxjava的各种封装姿势,也结合自己的理解,一步一步的做起来. 骚年,如果你还没有掌握ret ...

  9. 对百度WebUploader开源上传控件的二次封装,精简前端代码(两句代码搞定上传)

    前言 首先声明一下,我这个是对WebUploader开源上传控件的二次封装,底层还是WebUploader实现的,只是为了更简洁的使用他而已. 下面先介绍一下WebUploader 简介: WebUp ...

随机推荐

  1. SEO SEM

    SEO:搜索引擎优化SEM:搜索引擎营销 SEO排名机制:搜索引擎蜘蛛 权重 算法 排名规则 搜索引擎提交入口: 1.百度搜索网站登入口 2.Google网站登入口 3.360搜索引擎登入入口 4.搜 ...

  2. openresty 几个插件使用

    1. jwt    opm get SkyLothar/lua-resty-jwt   2. cookie   opm get p0pr0ck5/lua-resty-cookie   3. http ...

  3. 部署coredns

      1 前提:     1.1不依赖kubeadm的方式,适用于不是使用kubeadm创建的k8s集群,或者kubeadm初始化集群之后,删除了dns相关部署.      1.2 DNS IP :10 ...

  4. 使用 Excel 可以很方便的做程序原型

    使用 Excel 可以很方便的做程序原型 比如计算 单片机的端口模式,可以使用 Excel 很方便的计算出来,花了 15 分钟做好. 还可以使用函数自动根据二进制计算出 十六进制. 然后如果再使用软件 ...

  5. oralce之 10046对Hash Join分析

    前两天解决了一个优化SQL的case,SQL语句如下,big_table为150G大小,small_table很小,9000多条记录,不到1M大小,hash_area_size, sort_area_ ...

  6. bzoj1503[NOI2004]郁闷的出纳员——Splay

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1503 好奇怪呀!为什么而TLE? 各种修改终于卡时过了.可是大家比我快多了呀?难道是因为自己 ...

  7. Spring Boot 入门之持久层篇(三)

    原文地址:Spring Boot 入门之持久层篇(三) 博客地址:http://www.extlight.com 一.前言 上一篇<Spring Boot 入门之 Web 篇(二)>介绍了 ...

  8. jeecg选择按钮带入其他单据值

    前端的标签 <input class="inputxt" id="fshimian" name="fshimian" ignore=& ...

  9. Unit08: Spring集成mybatis

    Unit08: Spring集成mybatis 1. Spring集成mybatis (1)方式一 step1. 导包. spring-webmvc,mybatis,mybatis-spring, o ...

  10. bzoj 1023: [SHOI2008]cactus仙人掌图 2125: 最短路 4728: 挪威的森林 静态仙人掌上路径长度的维护系列

    %%% http://immortalco.blog.uoj.ac/blog/1955 一个通用的写法是建树,对每个环建一个新点,去掉环上的边,原先环上每个点到新点连边,边权为点到环根的最短/长路长度 ...