弹簧和布局视图
我是 Spring(不是 Java)的新手,我无法理解所有的瓷砖内容。我想做的是有一个带有 JavaScript 占位符的布局。 在.Net 中,我可以定义一个包含一些内容占位符的母版页。
Layout.Master
<%@ Master Language="C#" Inherits="System.Web.Mvc.ViewMasterPage" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<asp:ContentPlaceHolder ID="javascripts" runat="server" />
</head>
<body>
<asp:ContentPlaceHolder ID="content" runat="server" />
</body>
</html>
在操作视图中,我可以定义如何替换占位符。
index.aspx
<asp:Content ID="Content" ContentPlaceHolderID="content" runat="server">
This is the body content
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="javascripts" runat="server">
<script src="foo.js" type="text/javascript" charset="utf-8"></script>
</asp:Content>
我怎样才能在Spring中做到这一点?我已经看到 VelocityLayoutView 具有类似的功能,但我无法弄清楚如何在图块内定义占位符的内容。
I'm new to Spring (not to Java) and I just can't wrap my head around all the tiles stuff. What I want to do is to have a layout with a placeholder for JavaScript includes.
In .Net I can define a master page which contains some content placeholders.
Layout.Master
<%@ Master Language="C#" Inherits="System.Web.Mvc.ViewMasterPage" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<asp:ContentPlaceHolder ID="javascripts" runat="server" />
</head>
<body>
<asp:ContentPlaceHolder ID="content" runat="server" />
</body>
</html>
In the view for the action I can then define how the placeholder should be replaced.
index.aspx
<asp:Content ID="Content" ContentPlaceHolderID="content" runat="server">
This is the body content
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="javascripts" runat="server">
<script src="foo.js" type="text/javascript" charset="utf-8"></script>
</asp:Content>
How can I do that in Spring? I have seen that VelocityLayoutView has a similar functionality but I couldn't figure out how to define content for placeholders from within a tile.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您正在寻找的是一个支持模板继承和定义任意占位符的模板引擎。
我不知道有多少这样做的(在java世界中存在对模板组合的偏见,这就是普通的jsp:include、tiles等所做的)。
StringTemplate 支持继承,并且有一个 spring 插件 添加了对 ST 的支持,但我不知道该插件是否支持模板继承和 ST 继承 AFAIK 不允许您定义占位符。
另一个选择是 Jangod
看起来它可以满足您的需求。
例如(取自他们的网站):
基本模板
Template,它扩展了基本模板:
它支持 Spring MVC< /a>.
What you're looking for is a templating engine with support for template inheritance and defining arbitrary placeholders.
I don't know of many that do (in java world there's a bias towards template composition and that's what plain jsp:include, tiles etc do).
StringTemplate supports inheritance and there is a plugin for spring that adds support for ST, but I don't know if the plugin supports template inheritance and ST inheritance AFAIK doesn't let you define placeholders.
Another option is Jangod
Looks like it does what you're looking for.
For example (taken from their website):
Base template
Template, which extends base template:
It has support for Spring MVC.
我正在使用 Apache Tiles 来解决同样的问题,它的使用非常简单......
只需谷歌 Apachetiles + Spring 看看如何将它们连接在一起(一个简单的 xml 配置),
然后
只需使用您的主布局创建一个 Layout.jsp ,包括您提到的内容。
之后设置一个tiles.xml,其中包含映射以用一些有效页面替换包含内容,并将页眉和页脚保留为默认值(我这样做),
就像这样,
您只需为Spring提供该JSP的字符串(如果它被调用)
您在这里有完整的教程 Apache Tiles
I am using Apache Tiles for the same problem and it's really simple to use...
just google Apache tiles + Spring to see how to wire them together ( a simple xml config )
after that
Just create a Layout.jsp with your main layout, the includes you mentioned.
After that setup a tiles.xml with the mapping to substitute the includes with some valid pages and leave the header and footer as default( I do that what )
like this
the you just need to give Spring the String for that JSP, if it's called
you have a full tutorial here Apache Tiles