牛骨文教育服务平台(让学习变的简单)

环境上下文对象(WebContext)


为了让开发人员能够随时随地获取和使用Request、Response、Session等Web容器对象,YMP框架在WebMVC模块中提供了一个名叫WebContext的Web环境上下文封装类,简单又实用,先了解一下提供的方法:

直接获取Web容器对象:

  • 获取ServletContext对象:

      WebContext.getServletContext();
  • 获取HttpServletRequest对象:

      WebContext.getRequest();
  • 获取HttpServletResponse对象:

      WebContext.getResponse();
  • 获取PageContext对象:

      WebContext.getPageContext();

获取WebMVC容器对象:

  • 获取IRequestContext对象:

      WebContext.getRequestContext();

WebMVC请求上下文接口,主要用于分析请求路径及存储相关参数;

  • 获取WebContext对象实例:

      WebContext.getContext();

WebContext将Application、Session、Request等Web容器对象的属性转换成Map映射存储,同时向Map的赋值也将自动同步至对象的Web容器对象中,起初的目的是为了能够方便代码移植并脱离Web环境依赖进行开发测试(功能参考Struts2):

  • WebContext.getContext().getApplication();

  • WebContext.getContext().getSession();

  • WebContext.getContext().getAttribute(Type.Context.REQUEST);

原本可以通过WebContext.getContext().getRequest方法直接获取的,但由于设计上的失误,方法名已被WebContext.getRequest()占用,若变更方法名受影响的项目太多,所以只好委屈它了:D,后面会介绍更多的辅助方法来操作Request属性,所以可以忽略它的存在!

  • WebContext.getContext().getAttributes();

  • WebContext.getContext().getLocale();

  • WebContext.getContext().getOwner();

  • WebContext.getContext().getParameters();

WebContext操作Application的辅助方法:

  • boolean getApplicationAttributeToBoolean(String name);

  • int getApplicationAttributeToInt(String name);

  • long getApplicationAttributeToLong(String name);

  • String getApplicationAttributeToString(String name);

  • <T> T getApplicationAttributeToObject(String name);

  • WebContext addApplicationAttribute(String name, Object value)

WebContext操作Session的辅助方法:

  • boolean getSessionAttributeToBoolean(String name);

  • int getSessionAttributeToInt(String name);

  • long getSessionAttributeToLong(String name);

  • String getSessionAttributeToString(String name);

  • <T> T getSessionAttributeToObject(String name);

  • WebContext addSessionAttribute(String name, Object value)

WebContext操作Request的辅助方法:

  • boolean getRequestAttributeToBoolean(String name);

  • int getRequestAttributeToInt(String name);

  • long getRequestAttributeToLong(String name);

  • String getRequestAttributeToString(String name);

  • <T> T getRequestAttributeToObject(String name);

  • WebContext addRequestAttribute(String name, Object value)

WebContext操作Parameter的辅助方法:

  • boolean getParameterToBoolean(String name);

  • int getParameterToInt(String name)

  • long getParameterToLong(String name);

  • String getParameterToString(String name);

WebContext操作Attribute的辅助方法:

  • <T> T getAttribute(String name);

  • WebContext addAttribute(String name, Object value);

WebContext获取IUploadFileWrapper上传文件包装器:

  • IUploadFileWrapper getUploadFile(String name);

  • IUploadFileWrapper[] getUploadFiles(String name);