Java Apache Tiles 的更好替代品
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9748481/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Better alternative to Apache Tiles
提问by Fixus
I'm looking for a framework that is better and easier to use than Apache Tiles (which so far, I have used a couple of times).
我正在寻找一个比 Apache Tiles(到目前为止,我已经使用过几次)更好、更容易使用的框架。
With Tiles, it seems that when I have 100 actions I need to creates 100 jsp files and create 100 definitions in tiles.xml
.
使用 Tiles,似乎当我有 100 个动作时,我需要创建 100 个 jsp 文件并在tiles.xml
.
Is there a better framework to manage my templates? I want to create, for example, 2 templates:
有没有更好的框架来管理我的模板?例如,我想创建 2 个模板:
a) menu and column for content
b) menu, column for content, right column with banner
a) 菜单和内容栏
b) 菜单、内容栏、带有横幅的右栏
In both templates the menu is constant. In template b
, the right column is constant, so only the content column is different. For this simple example I don't want todefine each JSP file that extends the template a
(just to provide a body). Thats lame imo. Or maybe I`m lame and I can define a DEFAULT template in Apache Tiles and I'm just not using it right. In anycase, all help appreciated.
在这两个模板中,菜单是不变的。在 中template b
,右列是常量,因此只有内容列不同。对于这个简单的例子,我不想定义每个扩展template a
(只是为了提供一个主体)的JSP 文件。那是蹩脚的imo。或者也许我很蹩脚,我可以在 Apache Tiles 中定义一个 DEFAULT 模板,但我只是没有正确使用它。无论如何,所有帮助表示赞赏。
采纳答案by Fixus
I ended up using JSF + Facelets. I`ve combined them with Spring MVC and it works like a charm.
我最终使用了 JSF + Facelets。我已经将它们与 Spring MVC 结合起来,它就像一个魅力。
回答by Ralph
An other approach is Sitemesh. It was designed to mesh views where you can not modify the original, so it is more a html transformation/decoration framework than a templating framework like Tiles.
另一种方法是Sitemesh。它旨在对无法修改原始视图的视图进行网格化,因此它更像是一个 html 转换/装饰框架,而不是像 Tiles 这样的模板框架。
In my personal opinion Tiles is the better approach for appliations, and I would try to implement some kind of resolver (based on some naming conventions) that makes the xml files obsolete, but this was not the question.
在我个人看来,Tiles 是应用程序的更好方法,我会尝试实现某种使 xml 文件过时的解析器(基于某些命名约定),但这不是问题。
@See: This old introductionsshows how SiteMesh works.
@See:这个旧的介绍展示了 SiteMesh 是如何工作的。
回答by archetype
Based on experience, I strongly recommended Apache Wicket.
根据经验,我强烈推荐Apache Wicket。
回答by mck
(similar to this)
(类似于这个)
You don't need a definition for every action.
您不需要为每个动作定义一个定义。
This boilerplate configuration is a hang-up from tiles-1 days. It really isn't necessary with tiles-2 when wildcards were introduced, and especially with tiles-3 along with the OptionsRenderer.
这个样板配置是 tile-1 天的中断。当引入通配符时,tile-2 确实没有必要,尤其是tile-3 和OptionsRenderer。
Here's a tutorialthat will help you with
这是一个教程,可以帮助您
- spring to tiles integration,
- definitions with wildcards,
- implementing a fallback pattern using the OptionsRenderer, and
- definitions composition.
- 春天到瓷砖集成,
- 带通配符的定义,
- 使用 OptionsRenderer 实现回退模式,以及
- 定义组成。
回答by Neil McGuigan
Overall, I would recommend SiteMeshover Tiles.
总的来说,我会推荐SiteMesh 而不是 Tiles。
Here'show to setup SiteMesh 3
这是设置 SiteMesh 3 的方法
You can use Tiles for in-page templates, but use SiteMesh for site-wide template. Nevertheless...
您可以将 Tiles 用于页面内模板,但将 SiteMesh 用于站点范围的模板。尽管如此...
How to make Tiles suck less:
如何让瓷砖少吸:
Use convention over configuration. For example, put your definitions in
webapp/WEB-INF/tiles.xml
and there's no need to tell tiles where it is.Use wildcards:
使用约定优于配置。例如,输入您的定义,
webapp/WEB-INF/tiles.xml
无需告诉磁贴它在哪里。使用通配符:
<definition name="default" template="/WEB-INF/templates/default.jsp">
<put-attribute name="titleKey" value=""/>
<put-attribute name="body" value=""/>
</definition>
<definition name="*" extends="default">
<put-attribute name="titleKey" value="{1}.title"/>
<put-attribute name="body" value="/WEB-INF/views/{1}.jsp" />
</definition>
If your controller returns view name index
, it will match the definition *
, and use the JSP file /WEB-INF/views/index.jsp
for the body, and use the message property index.title
.
如果您的控制器返回视图名称index
,它将匹配定义*
,并使用 JSP 文件/WEB-INF/views/index.jsp
作为主体,并使用消息属性index.title
。
If your controller returns view name contact-us
, it will match the definition *
, and use the JSP file /WEB-INF/views/contact-us.jsp
for the body, and use the message property contact-us.title
如果您的控制器返回视图名称contact-us
,它将匹配定义*
,并使用 JSP 文件/WEB-INF/views/contact-us.jsp
作为主体,并使用消息属性contact-us.title
In your template, add:
在您的模板中,添加:
<c:set var="titleKey"><tiles:getAsString name="titleKey" /></c:set>
and
和
<title><spring:message code="${titleKey}"/></title>
Add ReloadableResourceBundleMessageSource
bean to your servlet application context.
将ReloadableResourceBundleMessageSource
bean添加到您的 servlet 应用程序上下文。
Make a file /src/main/resources/messages.properties
, with content like:
制作一个文件/src/main/resources/messages.properties
,内容如下:
index.title = Welcome to Acme, Inc.
contact-us.title = Contact Us