UI标签是用来生成Web界面,或者为Web界面提供某些功能支持的标签,比如:表单标签就是把各种途径获取的需要展示的数据,通过动态生成HTML的形式展示到界面上。

UI标签分成如下几种:

  • Form Tags:表单标签,包含所有可以用在Form表单里面的标签
  • Non-Form UI Tags:非表单标签,主要包含错误展示、组件等
  • Ajax Tags:用来支持Ajax的标签,这个在后面有专门的章节来学习

8.4.2 模板和主题

在进入具体的UI标签学习之前,有必要先理解主题和模板的概念,这对使用Struts2的UI标签非常重要,因为Struts2的UI标签的展示和实现是基于主题和模板的。

1:模板(Template)

所谓模板,就是一些代码,在Struts2中通常是用FreeMarker来编写的,标签使用这些代码能渲染生成相应的HTML代码。

一个标签需要知道自己显示什么数据,以及最终生成什么样的HTML代码。其中,显示什么数据往往是通过用户指定的OGNL表达式去值栈取;而最终生成什么样的HTML代码,就由一组FreeMarker的模板来定义,每个标签都会有自己对应的FreeMarker模板。这组模板在Struts2核心jar包(struts2-core-2.1.8.1.jar)的template包中。

2:主题(Theme)

所谓主题,就是一系列模板的集合。通常情况下,这一系列模板会有相同或类似的风格,这样能保证功能或视觉效果的一致性。

前面讲到,Struts2标签是使用一个模板来生成最终的html代码,这也就意味着,如果使用不同的模板,那么同一个标签所生成的HTML代码并不一样,也意味着不同的标签所生成的HTML代码的风格也可能不一样。

这就带来一个麻烦,一个页面会使用很多标签,如果每个标签所生成的HTML代码的风格不一样的话,这个页面会很杂乱,那么怎么统一这多个标签的功能或者风格呢?

答案自然就是主题,每一个主题包含一系列的模板,这些模板就会有相同或类似的风格,从而解决上面的问题。这也意味着,在Struts2里面,可以通过切换主题来切换标签成成的HTML的风格。

Template Loading

Templates ae loaded first by searching the application and then by searching the classpath. If a template needs to be overridden, an edited copy can be placed in the application, so that is found first.

主题的加载会首先搜索应用程序,然后会才会到classpath下寻找。

One for all
FreeMarker is the default templating engine. The FreeMarker templates are used regardless of what format the view may use. Internally, the JSP, FTL, Velocity tags are all rendered using FreeMarker.

FreeMarker是默认模板引擎,不管用到什么可能的视图显示,FreeMarker模板都可以被使用。

Template and Themes

Templates are loaded based the template directory and theme name (see Selecting Themes). The template directory is defined by the struts.ui.templateDir property in struts.properties (defaults to template). If a tag is using the ajax theme, the following two locations will be searched (in this order):

模板加载是基于模板路径和名字,路径定义于struts.ui.templateDir。

In the application /template/ajax/template.ftl
In the classpath /template/ajax/template.ftl

基于效率考虑,优先选择第一个路径

For performance reasons, you may want to prefer the first location, although the second one is more flexible. See Performance Tuning for a discussion on this topic.

Overriding Templates

The default templates provided in the struts-core.jar should suit the needs of many applications. However, if a template needs to be modified, it's easy to plug in a new version. Extract the template you need to change from the struts-core.jar, make the modifications, and save the updated copy to /template/$theme/$template.ftl. If you are using the xhmtl theme and need to change how the select tags render, edit that template and save it to /template/xhtml/select.ftl.

It is easier and better to edit and override an existing template than provide a new one of your own.

struts-core.jar里提供的默认模板应该适用于大多数应用程序的需要。但是,如果一个模板需要被修改,应该很容易插入一个新的版本,从struts-core.jar找到你要修改的模板,修改,然后保存,得到到/template/$theme/$template.ftl。

Altering Template Loading Behavior

It is possible to load template from other locations, like the file system or a URL. Loading templates from alternate locations can be useful not only for tags, but for custom results. For details, see the FreeMarker documentation and consult the section on extending the FreeMarkerManager.

可能会从其他地方加载模板,比如文件系统或者一个URL。从其他地方加载模板不仅能够使用标签,而且自定义结果,详情请看FreeMarker documentation。

Alternative Template Engines

The framework provides for template rendering engines other than FreeMarker. (Though, there is rarely a need to use another system!)

Don't try this at home!
Alternative template engines are best left to advanced users with special needs!

The framework supports three template engines, which can be controlled by the struts.ui.templateSuffix in struts.properties.

ftl (default) FreeMarker-based template engine
vm Velocity-based template engine
jsp JSP-based template engine

The only set of templates and themes provided in the distribution is for FreeMarker. In order to use another template engine, you must provide your own template and theme for that engine.

Stay the course
Don't feel that you need to rewrite the templates to match your view format. If you need to customize the template, try copying and modifying the FreeMarker template first. Most changes should be obvious.

Selecting Template Directory

Template directory can be selected using several different rules, in this order:

设置主题方法:  优先级也是下面的顺序

  1. The templateDir attribute on the specific tag
  2. The page-scoped attribute named templateDir
  3. The request-scoped attribute named templateDir
  4. The session-scoped attribute named templateDir
  5. The application-scoped attribute named templateDir
  6. The struts.ui.templateDir property in struts.properties (defaults to template)

1.通过设定特定UI标签上的tehme属性来指定主题
2.通过设定特定UI标签外围的Form标签的theme属性来指定主题
3.通过驱动的page会话范围内以theme为名称的属性来确定主题
4.通过取得request范围内以theme为名称的属性来确定主题
5.通过取得session范围内以theme为名称的属性来确定主题
6.通过取得application范围内以theme为名称的属性来确定主题
7.通过取得名为struts.ui.theme的常量(默认值是xhtml)来确定主题,该变量可以在struts.properties文件或者struts.xml文件中确定。

Tips

  • To support user-selected "shttp://confluence.twdata.org/pages/editpage.action?pageId=723#
    Wiki Markupkins", override the template directory in the user's session.
  • To change the template directory for the entire application, modify the struts.properties.

内建主题:
simple(默认主题)   ,   xhtml   ,   css_xhtml    ,    ajax
xhtml   ,css_xhtml主题都是对simple主题的包装和扩展
simple主题,每个UI标签只生成一个简单的html元素,不会生成其它额外的内容

xhtml主题,Strutss的默认主题,对simple进行了扩展,在该主题的基础增加如下附加特性
1.针对html标签使用标准的两列表格布局
2.每个html标签的label,既可以出现在html元素的左边,也可以出现上边,取决于lableposition属性的设置
3.自动输出校验错误信息
4. 输出javascript的客户端校验

css_html   对xhtml主题上加入css样式控制

ajax对xhtml主题上为每个标签提供了额外的ajax支持。ajax支持是以dojo和dwr为基础的
增加如下功能
1.增加ajax方式的客户端校验
2.支持远程表单的异步提交(最好和submit标签一起使用)
3.提供高级的div标签,允许实现局部更新html的功能
4.提供高级的a标签,允许动态加载并执行远端的javaScript代码
5.提供支持ajax的tabbedPanel
6.提供富客户端模型的pub-sub事件模型

Struts2 官方教程之Struts Tags(八)——UI Tags(Themes and Templates)相关推荐

  1. Struts2 官方教程之Struts Tags(六)——Generic Tags(Control Tags )

    开头要说的:在早期的应用开发中,表现层Jsp页面主要使用Jsp脚本来控制输出.这样,在Jsp中嵌套了java脚本,这种方式不管是可读性还是可维护性都很差,几乎使Jsp成为Java的子集.从Jsp1.1 ...

  2. Oracle官方教程之Fork/Join

    原文链接,译文链接,译者:Zach,校对:郑旭东 fork/join框架是ExecutorService接口的一种具体实现,目的是为了帮助你更好地利用多处理器带来的好处.它是为那些能够被递归地拆解成子 ...

  3. Firefly官方教程之Netconnect使用文档

    1.distributed说明 该模块包含了服务端与客户端通信的一些处理方法,包括发送数据的封装,协议头的封装,tcp通信时进行分包,处理粘包问题. 2.结构解析 LiberateFactory,协议 ...

  4. android 布局可大可小,UI设计教程之:ios与android ui适配(将IOS UI转换成Android经验畅谈)...

    内容提要:这是UI设计系列教程之ios与android ui适配经验畅谈.文章作者介绍了自己将IOS UI转换成Android经验,包括:不要直接转换.了解单位和组件缩放格式.屏幕尺寸DP和像素的换算 ...

  5. w3cschool教程之jQuery UI 教程概述

    w3cschool教程之jQuery UI 教程概述 链接地址: http://www.w3cschool.cc/jqueryui/jqueryui-tutorial.html w3cschool教程 ...

  6. Docker最全教程之MySQL容器化 (二十五)

    前言 MySQL是目前最流行的开源的关系型数据库,MySQL的容器化之前有朋友投稿并且写过此块,本篇仅从笔者角度进行总结和编写. 目录 镜像说明  运行MySQL容器镜像  1.运行MySQL容器  ...

  7. Docker最全教程之MySQL容器化 (二十四)

    Docker最全教程之MySQL容器化 (二十四) 原文:Docker最全教程之MySQL容器化 (二十四) 前言 MySQL是目前最流行的开源的关系型数据库,MySQL的容器化之前有朋友投稿并且写过 ...

  8. Docker最全教程之Python爬网实战(二十二)

    Python目前是流行度增长最快的主流编程语言,也是第二大最受开发者喜爱的语言(参考Stack Overflow 2019开发者调查报告发布).笔者建议.NET.Java开发人员可以将Python发展 ...

  9. Docker最全教程之Go实战,墙裂推荐(十九)

    前言 与其他语言相比,Go非常值得推荐和学习,真香!为什么?主要是可以直接编译成机器代码(性能优越,体积非常小,可达10来M,见实践教程图片)而且设计良好,上手门槛低.本篇主要侧重于讲解了Go语言的优 ...

最新文章

  1. rfid商品管理软件_哪个RFID固定资产管理系统比较好用?
  2. html5 拖拽的简要介绍
  3. 谨记!怎么样的编程会让你进监狱?
  4. AC日记——行程长度编码 openjudge 1.7 32
  5. 获得ABAP report里定义的所有变量及type - GET_GLOBAL_SYMBOLS
  6. validate+jquery+ajax表单验证
  7. easyui树形菜单实现
  8. jquery常用事件——幕布
  9. hp服务器引导驱动器,windows-server-2008 – 在没有SmartStart的HP Proliant服务器上安装Windows...
  10. 时间序列预测 | Python实现GAN时间序列数据生成建模
  11. android文件恢复功能,终于找到了安卓手机删除的文件的恢复方法值得一看
  12. MATLAB线性方程组的两种求解,matlab求解线性方程组
  13. idea设置主题风格
  14. win32 WaitCommEvent
  15. 简单演示程序序列号的破解
  16. Angular5.x多环境配置(开发环境、生产环境、测试环境)
  17. 一些关于网页设计的优秀网站
  18. 20_ecplise安装与简单使用汉化
  19. go语言channel学习与总结(一)channel的一些基本用法
  20. 自动控制原理->一些内容的概括了解

热门文章

  1. android 高德地图设置不能旋转_地图导航哪家强?
  2. 【xinfanqie】解决网络出现“正在连接”的问题
  3. Artanis: 工作日志自动收发系统
  4. 浅谈数据中心主备电并网运行方式
  5. replace()方法
  6. Threejs实现全景图功能
  7. Hadoop安装及hdfs操作
  8. OpenSSL s_client
  9. 腾讯 Matrix 增量编译 bug 解决之路,PR 已通过
  10. Android面试老生常谈的 View 事件分发机制,看这一篇就够了