封装baseControl
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的更多相关文章
- BaseControl按钮合集
BaseControl按钮合集 效果 源码 https://github.com/YouXianMing/Animations // // POPBaseControl.h // Animations ...
- 用UIControl封装Button
用UIControl封装Button 效果 说明 UIControl在处理超出触摸范围的触摸事件时有bug 源码 基础类 // // BaseControl.h // BaseControl // / ...
- 玩转控件:封装Dev的LabelControl和TextEdit
俗话说的好:"工欲善其事必先利其器",作为软件攻城狮也是同样道理,攻城狮开发的软件目的是简化客户的操作,让客户动动手指就可以完成很多事情,减少人力成本.这也是系统/软件存在的目的. ...
- [C#] 简单的 Helper 封装 -- RegularExpressionHelper
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...
- iOS开发之App间账号共享与SDK封装
上篇博客<iOS逆向工程之KeyChain与Snoop-it>中已经提到了,App间的数据共享可以使用KeyChian来实现.本篇博客就实战一下呢.开门见山,本篇博客会封装一个登录用的SD ...
- Ajax实现原理,代码封装
都知道实现页面的异步操作需要使用Ajax,那么Ajax到是怎么实现异步操作的呢? 首先需要认识一个对象 --> XMLHttpRequest 对象 --> Ajax的核心.它有许多的属性和 ...
- 用C语言封装OC对象(耐心阅读,非常重要)
用C语言封装OC对象(耐心阅读,非常重要) 本文的主要内容来自这里 前言 做iOS开发的朋友,对OC肯定非常了解,那么大家有没有想过OC中NSInteger,NSObject,NSString这些对象 ...
- 【知识必备】RxJava+Retrofit二次封装最佳结合体验,打造懒人封装框架~
一.写在前面 相信各位看官对retrofit和rxjava已经耳熟能详了,最近一直在学习retrofit+rxjava的各种封装姿势,也结合自己的理解,一步一步的做起来. 骚年,如果你还没有掌握ret ...
- 对百度WebUploader开源上传控件的二次封装,精简前端代码(两句代码搞定上传)
前言 首先声明一下,我这个是对WebUploader开源上传控件的二次封装,底层还是WebUploader实现的,只是为了更简洁的使用他而已. 下面先介绍一下WebUploader 简介: WebUp ...
随机推荐
- c# datetime 格式化大全与使用总结
//C# datetime 格式化 DateTime dt = DateTime.Now; Label1.Text = dt.ToString();//2005-11-5 13:21:25 Label ...
- 控制已经打开的Excel
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...
- 修改selinux出现setsebool: SELinux is disabled.的解决方法
1.vi /etc/vsftpd/vsftpd.conf # You may specify an explicit list of local users to chroot() to their ...
- WPF自定义控件之图形解锁控件 ScreenUnLock
ScreenUnLock 与智能手机上的图案解锁功能一样.通过绘制图形达到解锁或记忆图形的目的. 本人突发奇想,把手机上的图形解锁功能移植到WPF中.也应用到了公司的项目中. 在创建ScreenUnL ...
- cnpm 私服搭建(基于docker)
备注: 使用docker-compose 进行安装 1. 代码clone git clone https://github.com/cnpm/cnpmjs.org.git 2. docker bu ...
- Maven中配置生成单元测试报告配置
对junit单元测试的报告: 1. ------------------------------------------------------- 2. T E S T S 3. ------ ...
- CArray,CList,CMap如何实例化
1.定义一个CMap,向这个CMap中增加数据项(键-值对).CMap<CString, LPCTSTR, CString, LPCTSTR>m_ItemMap;CString strKe ...
- rails里面添加妹子ui
妹子ui看起来很不错,以为在rails里面添加自定义的css和js和平时一样,结果可想而知,不过弄完以后发现还是比较简单的,这里记录一下 妹子ui需要加载的css和js如下 http://cdn.am ...
- supervisor备忘
supervisor是把普通app变成deamon的工具,虽然没有erlang的supervise粒度那么细,但是已经非常方便了 安装 sudo apt-get install supervisor ...
- JVM调优总结(这个总结得比较全面)
堆大小设置 JVM 中最大堆大小有三方面限制:相关操作系统的数据模型(32-bt还是64-bit)限制:系统的可用虚拟内存限制:系统的可用物理内存限制.32位系统下,一般限制在1.5G~2G:64为操 ...