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. python3 的字符串格式判断

    在python编程中,我们经常要面临将字符串进行转换的情况,那么字符串是否符合转换的要求呢?python中内置了字符串类的方法供我们使用进行字符串格式的判断. 1.isalnum() 所有字符都是数字 ...

  2. {Notes}{Latex}{multirow}

    这个文章写的真的太牛比了! ============================================================ 在latex文件最前面用这个包\usepackag ...

  3. 【DUBBO】Dubbo原理解析-服务发布

    转载:http://blog.csdn.net/quhongwei_zhanqiu/article/details/41651205 服务发布方在spring的配置文件中配置如下: <bean ...

  4. UVA11137 Ingenuous Cubrency

    题意 PDF 分析 考虑dp. 用\(d(i,j)\)表示用不超过i的立方凑成j的方案数. \(d(i,j)=d(i-1,j)+d(i,j-i^3)\) 时间复杂度\(O(IN+T)\) 代码 #in ...

  5. matlab 中的function定义. 用最简单的例子说明.

    function y=myfunction(a,b)其中a,b是输入函数的参数,y是函数返回的值.当需要返回多个值时,可以将y看作一个数组,或者直接将函数的开头写成如function [x,y]=my ...

  6. vs2013环境下boost配置

    编译boost库的过程这里暂时不写.  先写在vs2013下的boost配置. 新建一个工程, 1, 属性->C/C++,在附加包含目录添加或编辑Boost的文件路径, D:\boost_1_5 ...

  7. @Autowired & @Resource 区别 & 解读@Bean

    一样     Autowired & @Resource 都可以用来Bean的注入,可以写在属性(字段)上.也可以写在setter方法上 不一样 1.来源不一样 @Autowired 由Spr ...

  8. Spring Boot 入门之缓存和 NoSQL 篇(四)

    原文地址:Spring Boot 入门之缓存和 NoSQL 篇(四) 博客地址:http://www.extlight.com 一.前言 当系统的访问量增大时,相应的数据库的性能就逐渐下降.但是,大多 ...

  9. 初学者手册-Sublime Text常用快捷键

    Alt + F3 :找出当前文档中所有被划选的词语,若文档很大的话,可能会导致Sublime Text崩溃. Ctrl + kkk :删除当前行光标至行尾的所有内容. End: 光标跳至行尾. Hom ...

  10. 013:Rank、视图、触发器、MySQL内建函数

    一. Rank 给出不同的用户的分数,然后根据分数计算排名 (gcdb@localhost) 09:34:47 [mytest]> create table t_rank(id int,scor ...