实际本章教程开始之前,让我们看看由http://struts.apache.org给出的几个定义:

Term 描述
tag A small piece of code executed from within JSP, FreeMarker, or Velocity.
template A bit of code, usually written in FreeMarker, that can be rendered by certain tags (HTML tags).
theme A collection of templates packaged together to provide common functionality.

我也建议去通过Struts2本土化章节,因为我们将采取同样的例子,再次执行我们的练习。

当使用Struts 2 标签如<s:submit...>,<s:textfield...>等在网页中,Struts 2框架生成HTML代码与预先设定的样式和布局。 Struts 2自带内置的主题有三个:

Theme 描述
simple theme A minimal theme with no "bells and whistles". For example, the textfield tag renders the HTML <input/> tag without a label, validation, error reporting, or any other formatting or functionality.
xhtml theme This is the default theme used by Struts 2 and provides all the basics that the simple theme provides and adds several features like standard two-column table layout for the HTML, Labels for each of the HTML, Validation and error reporting etc.
css_xhtml theme This theme provides all the basics that the simple theme provides and adds several features like standard two-column CSS-based layout, using <div> for the HTML Struts Tags, Labels for each of the HTML Struts Tags, placed according to the CSS stylesheet.

正如上面所提到的,如果不指定一个主题,然后Struts2中会使用默认的XHTML主题。例如Struts 2中选择标签:

<s:textfield name="name" label="Name" />

生成HTML标记:

<tr>
<td class="tdLabel">
<label for="empinfo_name" class="label">Name:</label>
</td><td>
<input type="text" name="name" value="" id="empinfo_name"/>
</td>
</tr>

这里empinfo struts.xml文件中定义动作名称。

选择主题:

可以指定主题Struts 2每一个标签的基础上或指定的主题Struts 2使用,可以使用下列方法之一:

  • 主题属性的具体标签

  • 主题属性标签的周边表单标签

  • 页面范围的属性,名为“主题”

  • 请求范围属性名为“主题”

  • 会话作用域属性命名为“主题”

  • 应用程序作用域的属性命名为“主题”

  • 在struts.properties struts.ui.theme属性(默认为XHTML)

以下语法指定他们在标签级别,如果愿意为不同的标签使用不同的主题:

<s:textfield name="name" label="Name" theme="xhtml"/>

因为它不是非常实用,每个标签的基础上使用主题,所以干脆我们可以指定规则struts.properties文件中使用以下标签:

# Standard UI theme
struts.ui.theme=xhtml
# Directory where theme template resides
struts.ui.templateDir=template
# Sets the default template type. Either ftl, vm, or jsp
struts.ui.templateSuffix=ftl

以下的结果是,我们拿起从本地化章节里我们使用默认设置struts.ui.theme= XHTML的struts-default.properties文件中,默认情况下,在 struts2-core.xy.z.jar文件,这是由主题。

主题如何工作的?

对于一个给定的主题,每一个struts标签有关联的模板,如:s:textfield -> text.ftl 和 s:password -> password.ftl等,这些模板文件来压缩struts2-core.xy.z.jar文件。这些模板文件保持一个预先定义的HTML布局为每个标签。所以Struts2 框架生成最终的HTML标记代码使用Sturts标签和相关的模板。

Struts 2 tags + Associated template file = Final HTML markup code.

默认模板已经写在FreeMarker和他们有扩展名 .ftl。可以设计使用速度或JSP模板,并据此设置配置在使用struts.ui.templateSuffix 和 struts.ui.templateDir struts.properties。

创建新的主题:

最简单的方法来创建一个新的主题是复制现有的任何主题/模板文件,并做必要的修改。所以,让我们开始创建一个文件夹 WebContent/WEB-INF/classes 名为模板和子文件夹与我们新的主题的名称,例如WebContent/WEB-INF/classes/template/mytheme。从这里,可以从头开始构建模板,或者可以复制​​模板从Struts2分布和根据需要进行修改。

我们要修改现有的默认模板XHTML学习目的。所以,现在让,我们复制内容从 struts2-core-x.y.z.jar/template/xhtml 到我们的主题目录,并只修改WebContent/WEB-INF/classes/template/mytheme/control.ftl文件。当我们打开control.ftl 它将有下面几行:

<table class="${parameters.cssClass?default('wwFormTable')?html}"<#rt/>
<#if parameters.cssStyle??> style="${parameters.cssStyle?html}"<#rt/>
</#if>
>

让我们上述文件control.ftl改变有以下内容:

<table style="border:1px solid black;">

如果检查看 form.ftl 会发现,control.ftl 这个文件中,form.ftl这个文件是指从XHTML主题。因此,让我们改变如下:

<#include "/${parameters.templateDir}/xhtml/form-validate.ftl" />
<#include "/${parameters.templateDir}/simple/form-common.ftl" />
<#if (parameters.validate?default(false))>
onreset="${parameters.onreset?default('clearErrorMessages(this);
clearErrorLabels(this);')}"
<#else>
<#if parameters.onreset??>
onreset="${parameters.onreset?html}"
</#if>
</#if>
>
<#include "/${parameters.templateDir}/mytheme/control.ftl" />  

我假设不会有太多了解FreeMarker模板语言,仍然寻找FTL文件需要做什么,可以得到一个不错的主意。然而,让我们除上述变动外,并回到我们的本地化的例子,创建 WebContent/WEB-INF/classes/struts.properties 档案的以下内容:

# Customized them
struts.ui.theme=mytheme
# Directory where theme template resides
struts.ui.templateDir=template
# Sets the template type to ftl.
struts.ui.templateSuffix=ftl

现在这种变化后,右键点击项目名称,并单击Export > WAR File创建一个WAR文件。然后部署此WAR在Tomcat的webapps目录下。最后,启动Tomcat服务器和尝试访问URL http://localhost:8080/HelloWorldStruts2。这会给出以下画面:

XHTML主题复制后的变化,我们做了主题这是一个结果,可以看到一个表单组件周围的边框。 FreeMarker学习,如果你努力,那么将能够创建或修改主题很容易。至少现在,你必须有一个基本的了解Sturts2主题和模板。

Struts2 主题和模板的更多相关文章

  1. 10款最好的 Bootstrap 3.0 免费主题和模板

    Twitter Bootstrap 框架已经广为人知,用于加快网站,应用程序或主题的界面开发,并被公认为是迄今对于Web开发的最有实质性帮助的工具之一.在此之前的,各种各样的界面库伴随着高昂的维护成本 ...

  2. 30款最好的 Bootstrap 3.0 免费主题和模板

    Twitter Bootstrap 框架已经广为人知,用于加快网站,应用程序或主题的界面开发,并被公认为是迄今对于 Web 开发的最有实质性帮助的工具之一.在此之前的,各种各样的界面库伴随着高昂的维护 ...

  3. 11款扁平化设计的 Twitter Bootstrap 主题和模板

    扁平化设计和 Bootstrap 框架是2013年网页设计领域的两大设计潮流.把这两者集合起来不是件容易的事情,使用下面这些主题和模板将节省我们的开发时间,因为我们可以修改已有的基础代码,而不是从零开 ...

  4. 【今日推荐】10大流行的 Metro UI 风格的 Bootstrap 主题和模板

    1. BootMetro 基于 Twitter Bootstrap 的简单灵活的 HTML.CSS 和 Javascript 框架,Win8 风格,大爱啊! 立即下载     效果演示 2. Boot ...

  5. 从无到有开发自己的Wordpress博客主题---局部模板的准备

    毫无疑问,我们媒体页面都会有header和footer,这些用到的内容几乎是一样的. 从无到有,我们先不考虑后面可能用到的Search和Comment等的模板,后面的我会在文本最后面追加. 开始之前, ...

  6. jsp+struts2登录框架模板

    一.建立一个名叫jsp_struts2的项目 二.导入jar包 如上图:jar包导入在WebContent/WEB-INF/lib下 三.建立一个LoginAction类 LoginAction类的s ...

  7. 从无到有开发自己的Wordpress博客主题---主页模板

    在只做完成了header和footer的模板之后,我们首先在之前Hello World的基础上做一个最简单的调用测试 //修改index.php内容如下 <?php get_header(); ...

  8. 10 个免费的Bootstrap Admin 主题,模板收集

    In designing websites today, one of the must have frameworks is the twitter bootstrap. To those who ...

  9. Struts2的模板和主题theme及自定义theme的使用

    Struts2的模板和主题theme及自定义theme 标签: struts2 2016-03-29 11:22 190人阅读 评论(0) 收藏 举报  分类: javaweb(8)  Struts2 ...

随机推荐

  1. 全局流水ID号生成的几种方法

    这个问题源自于,我想找一个分布式下的ID生成器.  这个最简单的方案是,数据库自增ID.为啥不用咧?有这么几点原因,一是,会依赖于数据库的具体实现,比如,mysql有自增,oracle没有,得用序列, ...

  2. java通过Comparable接口实现字符串比较大小排序的简单实例

    /** * 对象比较大小compare的用法 字符串排序 * 练习代码, 给定字符串" nba" "cba" "ncaa" "wb ...

  3. 【jQuery】:not选择器的说明和:checked选择器的使用

    1.:not选择器的说明使用 先给出一下例子: $(".form1 :not(input[name='category'])") 这个 能实现 获取到from1表单中除了input ...

  4. inner join, left join ,right join 结果

    假设有两个表结构如下: 表table1 表 table 2 内连接: --内连接 select * from table1 inner join table2 on table1.ID = table ...

  5. css3的cursor

    1.cursor属性参考表 还有zoom-in/zoom-out 还有grab/grabbing 2.css (1)前面的基本上就 .xx { cursor: pointer; } (2)后面两个有兼 ...

  6. PostgreSQL配置文件--WAL

    3 WAL WRITE AHEAD LOG 3.1 Settings 3.1.1 fsync 字符串 默认: fsync = on 开启后强制把数据同步更新到磁盘,可以保证数据库将在OS或者硬件崩溃的 ...

  7. OneThink框架的文章详情页分页

    Application/Home/Controller/ArticleController.class.php的detail函数修改结果如下: /* 文档模型详情页 */public function ...

  8. window7访问虚拟机ubuntu中的mysql

    window7上面下载mysql很麻烦,不喜欢,所以改用虚拟机安装ubuntu系统,提供mysql服务. 第一步:下载vmware workstation12, 第二步:下载ubuntu镜像,我用的是 ...

  9. 在eclipse上配置tomcat

    Eclipse中Tomcat的配置及简单例子 Eclipse中Tomcat的配置是很简单的一个工作,作为一名刚刚起步的编程菜鸟,我将这个配置的过程和简单的例子写下来记录,也希望能给像我怎样的新手一些帮 ...

  10. 【OpenStack项目管理-CPU/内存/存储/网络 配额管理】

    参考资料: OpenStack如何管理项目和用户:http://os.51cto.com/art/201312/422010.htm Nova.Cinder.Neutron资源配额设置:http:// ...