使用tiles2和spring mvc动态设置页面标题
我已经问自己这个问题很长一段时间了,但我还没有在网上找到一个很好的解决方案。
所以我使用 Tiles2 和 Spring MVC,我想在主体图块中动态设置页面标题。有办法吗?
<definition name="mainTemplate" template="/WEB-INF/template/main.jsp">
<put-attribute name="header" value="/WEB-INF/template/header.jsp" />
<put-attribute name="footer" value="/WEB-INF/template/footer.jsp" />
<put-attribute name="body" value="/WEB-INF/template/blank.jsp" />
</definition>
<definition name="list" extends="mainTemplate">
<put-attribute name="body" value="/WEB-INF/jsp/list.jsp" />
</definition>
我当前的解决方案是在控制器中设置标题
model.addAttribute("pageTitle", "blubb");
并在模板中执行 ac:out
I've been asking myself this question for quite some time and I haven't found a nice solution for this on the web.
So I am using Tiles2 and Spring MVC and I'd like to set the page title dynamically within the body tile. Is there a way?
<definition name="mainTemplate" template="/WEB-INF/template/main.jsp">
<put-attribute name="header" value="/WEB-INF/template/header.jsp" />
<put-attribute name="footer" value="/WEB-INF/template/footer.jsp" />
<put-attribute name="body" value="/WEB-INF/template/blank.jsp" />
</definition>
<definition name="list" extends="mainTemplate">
<put-attribute name="body" value="/WEB-INF/jsp/list.jsp" />
</definition>
my current solution is setting the title within the controller
model.addAttribute("pageTitle", "blubb");
and the doing a c:out in the template
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
平铺技术
如果“我想动态设置页面标题”的意思是“我想根据正在显示的平铺设置页面标题,并且我想使用平铺功能来完成此操作”,那么:
变量技术
执行此技术的最简单方法是向模型添加属性并使用 el 表达式引用该属性。例如,
您可以在控制器中执行此操作:
然后在页面中引用“PageTitle”属性,如下所示:
您可以像这样使用 c:out:
Tiles Technique
If by "I want to set the page title dynamically" you mean "I want to set the page title based on the tile that is being displayed and I want to use a tiles feature to do it" then:
Variable Technique
The simplest way to do this technique is to add an attribute to the model and to reference said attribute with an el expression. For example,
You might do this in your controller:
Then reference the "PageTitle" attribute in your page like this:
You can use c:out like this:
这对我有用。难道是有什么问题吗?
瓷砖:
JSP:
This is working for me. Is there anything wrong with it?
TILES:
JSP:
您还可以结合 DwB 的两个答案,这样您就可以两全其美:
当您希望某些页面具有静态标题时有用(因此您只需在tiles.xml 文件中设置它),某些页面具有完全动态标题(不要在tiles.xml中设置任何内容,只需将pageTitle添加到您的模型对象中)或两者兼而有之(我最喜欢),其中您有一个静态的前半部分和动态的后半部分。
You can also combine DwB's two answers so you get the best of both worlds:
Useful when you want some pages to have static titles (so you only need to set it in the tiles.xml file), some pages to have fully dynamic titles (don't set anything in tiles.xml, just add pageTitle to your model object) or a bit of both (my favourite) where you have a static first half and a dynamic second half.
tiles.xml:
JSP:
但是,如果您的应用程序只有一种语言,那么这只是一个好的解决方案。
tiles.xml:
JSP:
But this is only a good solution if your application has only one language.