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

来源:互联网 发布:天盛网络 编辑:程序博客网 时间:2024/05/15 08:22

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 thestruts.ui.templateDir property in struts.properties (defaults to template). If a tag is using theajax theme, the following two locations will be searched (in this order):

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

In the application/template/ajax/template.ftlIn 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 thestruts-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 theFreeMarker 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 enginevmVelocity-based template enginejspJSP-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事件模型

 

原创粉丝点击