春天 + Tiles2 - 来自模型的放置属性?

发布于 2024-12-24 20:55:20 字数 1733 浏览 2 评论 0原文

tl;dr - 我想根据传递到图块中视图的单个模型在图块定义中设置放置属性

我正在尝试创建一个页面来使用 spring 和图块显示用户。现在我的页面可以很好地获取模型,但我希望页面的标题包含用户名(必须从模型中获取)。我在下面包含了我的代码的一些摘录:

tiles.xml:

<definition name="baseLayout" template="/WEB-INF/jsp/layout/layout.jsp">
    <put-attribute name="title" value="FitterBlog" />
    <put-attribute name="header" value="/WEB-INF/jsp/layout/header.jsp" />
    <put-attribute name="nav" value="/WEB-INF/jsp/layout/nav.jsp" />
    <put-attribute name="body" value="" />
    <put-attribute name="footer" value="/WEB-INF/jsp/layout/footer.jsp" />
    <put-attribute name="ads" value="/WEB-INF/jsp/layout/ads.jsp" />
    <put-attribute name="css-layout" value="/FitterBlog/resources/css/layout.css" />
</definition>
<definition name="user/display" extends="baseLayout">
    <put-attribute name="title" value="FitterBlog - ${user.username}" />
    <put-attribute name="body" value="/WEB-INF/jsp/user/display.jsp" />
</definition>

如您所见,我尝试使用与在 jsp 中相同的语法来显示用户名。 IE。我尝试使用 ${user.username} 显示用户名,但这不起作用,我只是在标题中显示文本“${user.username}”而不是实际的用户名。

display.jsp:

//output the username from the user model, this works fine
${user.username}

UserController.java

@RequestMapping(value="display/**")
public ModelAndView displayUser(@ModelAttribute("user") User user, BindingResult result) {
    //TODO
    //retrieve user number from the URL
    //retrieve user from database
    //display user details
    user.setUsername("Awesome username!");
    return new ModelAndView("user/display", "user", user);
}

如您所见,我仍在大力开发中,目前自己在用户对象上设置用户名(而不是从数据库中获取),但这不是重点。

tl;dr - I want to set a put-attribute in my tiles definitions based on the individual model passed to a view in tiles

I'm trying to create a page to display a user using spring and tiles. Right now my page gets the model fine, but I want the title of the page to include the username (which it would have to get from the model). I've included some excerpts from my code below:

tiles.xml:

<definition name="baseLayout" template="/WEB-INF/jsp/layout/layout.jsp">
    <put-attribute name="title" value="FitterBlog" />
    <put-attribute name="header" value="/WEB-INF/jsp/layout/header.jsp" />
    <put-attribute name="nav" value="/WEB-INF/jsp/layout/nav.jsp" />
    <put-attribute name="body" value="" />
    <put-attribute name="footer" value="/WEB-INF/jsp/layout/footer.jsp" />
    <put-attribute name="ads" value="/WEB-INF/jsp/layout/ads.jsp" />
    <put-attribute name="css-layout" value="/FitterBlog/resources/css/layout.css" />
</definition>
<definition name="user/display" extends="baseLayout">
    <put-attribute name="title" value="FitterBlog - ${user.username}" />
    <put-attribute name="body" value="/WEB-INF/jsp/user/display.jsp" />
</definition>

As you can see, I've tried using the same syntax as I would in a jsp to display the username. ie. I tried using ${user.username} to display the username, however that doesn't work and I just get the text "${user.username}" displayed in the title instead of the actual username.

display.jsp:

//output the username from the user model, this works fine
${user.username}

UserController.java

@RequestMapping(value="display/**")
public ModelAndView displayUser(@ModelAttribute("user") User user, BindingResult result) {
    //TODO
    //retrieve user number from the URL
    //retrieve user from database
    //display user details
    user.setUsername("Awesome username!");
    return new ModelAndView("user/display", "user", user);
}

as you can see, I'm still in heavy development and currently set the username on the user object myself (instead of getting it from the database), but that's beside the point.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

神魇的王 2024-12-31 20:55:20

在阅读了几个不同的网站后(另外,感谢拉尔夫的链接,它给了我一个开始的地方),我找到了一个解决方案。

基本上,我需要做的是编辑 DTD 以使用版本 2.1,并使用表达式放置属性而不是值放置属性。这是tiles.xml 文件的相关部分:

<!DOCTYPE tiles-definitions PUBLIC
   "-//Apache Software Foundation//DTD Tiles Configuration 2.1//EN"
   "http://tiles.apache.org/dtds/tiles-config_2_1.dtd">

<definition name="user/view" extends="baseLayout">
    <put-attribute name="title" expression="FitterBlog | ${user.username}" />
    <put-attribute name="body" value="/WEB-INF/jsp/user/view.jsp" />
</definition>

After reading a few different sites (also, thanks for the link Ralph, it gave me a place to start) I have found a solution.

Basically what I needed to do was edit my DTD to use version 2.1 and also to use an expression put-attribute instead of a value put-attribute. Here is the relevant part to the tiles.xml file:

<!DOCTYPE tiles-definitions PUBLIC
   "-//Apache Software Foundation//DTD Tiles Configuration 2.1//EN"
   "http://tiles.apache.org/dtds/tiles-config_2_1.dtd">

<definition name="user/view" extends="baseLayout">
    <put-attribute name="title" expression="FitterBlog | ${user.username}" />
    <put-attribute name="body" value="/WEB-INF/jsp/user/view.jsp" />
</definition>
葬花如无物 2024-12-31 20:55:20

请参阅图块文档的这一部分: 表达式语言支持

它看起来就像您需要先启用 EL-Support 一样。

我知道这不是一个完整的解决方案,但我希望这个提示有助于解决问题。 -- 如果有帮助,请发布您找到的解决方案。

See this part of the tiles documentation: Expression Language support

It looks like you need to enable the EL-Support first.

I know this is not a complete solution, but I hope this hint helps to resolve the problem. -- If it helps, please post the solution you found.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文