手册

目录

环境上下文对象(WebContext)

收藏129

阅读11911

更新时间2022-04-11

为了让开发人员能够随时随地获取和使用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 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 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 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 getAttribute(String name);

  • WebContext addAttribute(String name, Object value);

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

  • IUploadFileWrapper getUploadFile(String name);

  • IUploadFileWrapper[] getUploadFiles(String name);

科技资讯

更多

精选课程

更多
前端入门_HTML5
前端入门_HTML5

共29课时

61.7万人学习

CSS视频教程-玉女心经版
CSS视频教程-玉女心经版

共25课时

39.3万人学习

JavaScript极速入门_玉女心经系列
JavaScript极速入门_玉女心经系列

共43课时

71万人学习

独孤九贱(1)_HTML5视频教程
独孤九贱(1)_HTML5视频教程

共25课时

61.6万人学习

独孤九贱(2)_CSS视频教程
独孤九贱(2)_CSS视频教程

共22课时

23万人学习

独孤九贱(3)_JavaScript视频教程
独孤九贱(3)_JavaScript视频教程

共28课时

33.9万人学习

独孤九贱(4)_PHP视频教程
独孤九贱(4)_PHP视频教程

共89课时

125万人学习

关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号